我使用FirebaseRecyclerAdapter從Firebase數據庫中檢索數據。我正在使用GridLayout來顯示數據。我的數據是通過firebase數據庫中的push()鍵排序的,我想以相反的順序檢索這些數據。我一直在使用如何在FirebaseRecyclerAdapter中正確實現override getItem()方法
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
不幸的是,setStackFromEnd()
不支持網格佈局嘗試。如果沒有setStackFromEnd()
,setReverseLayout()
會顛倒數據順序,但會在頂部留下間隙,這是不可接受的。
不過,我已經找到答案this
@Override
public Item getItem(int pos) {
return super.getItem(getCount() - 1 - pos);
}
從另一個問題(我認爲這可能工作),並試圖實現它。但是,出現錯誤:'無法解析符號項目','嘗試對getItem(int)'使用不兼容的返回類型。我不知道如何實施getItem()
。如果有人知道如何解決這個問題,請幫忙!提前致謝!
目標:實現getItem()代碼,試圖反轉從firebase數據庫中平穩檢索的push()數據(頂部沒有任何間隙)。假設鏈接中的代碼有效。
代碼:
public class Browse extends Fragment {
private FirebaseRecyclerAdapter<CategoryInformation, CategoryHolder> mFirebaseAdapter;
private GridLayoutManager mLayoutManager;
private RecyclerView rv;
private DatabaseReference mDatabase;
public Browse() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_browse, container, false);
rv = rootView.findViewById(R.id.rv_recycler_view_browse);
mLayoutManager = new GridLayoutManager(getContext(), 2);
rv.setLayoutManager(mLayoutManager);
mDatabase = FirebaseDatabase.getInstance().getReference().child("new");
return rootView;
}
@Override
public void onStart() {
super.onStart();
mFirebaseAdapter = new FirebaseRecyclerAdapter<CategoryInformation, CategoryHolder>(
CategoryInformation.class,
R.layout.card_item_browse,
CategoryHolder.class,
mDatabase) {
@Override
public Item getItem(int pos) {
return super.getItem(getCount() - 1 - pos);
}
@Override
public void populateViewHolder(CategoryHolder holder, CategoryInformation chat, final int position) {
Picasso.with(getContext()).load(chat.getImage()).into(holder.feed_image);
holder.rating.setText(chat.getRating());
}
};
rv.setAdapter(mFirebaseAdapter);
}
//ViewHolder for our Firebase UI
public static class CategoryHolder extends RecyclerView.ViewHolder{
ImageView feed_image;
TextView rating;
View mView;
public CategoryHolder(View v) {
super(v);
feed_image = v.findViewById(R.id.feed_image);
rating = v.findViewById(R.id.rating);
mView = v;
}
}
}