我正在制作衣服应用程序,我需要制作无休止的recyclerview ..我可以获取所有数据,但无法进行分页,我尝试了许多解决方案,但没有任何效果,这就是我用于页面和图像的代码用于recyclerview数据的Firebase数据库。
代码:
private View mMainView;
private String user_id;
private RecyclerView mUserList;
private DatabaseReference mUserDatabase;
private List<ItemsRecyclerView> mItems;
private ItemsAdapter itemsAdapter;
private GridLayoutManager gridLayoutManager;
private String oldestkey;
public Items() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mMainView = inflater.inflate(R.layout.fragment_items, container, false);
mUserList = (RecyclerView) mMainView.findViewById(R.id.items_recyclerview);
mItems = new ArrayList<>();
gridLayoutManager = new GridLayoutManager(getActivity(),3);
mUserList.setLayoutManager(gridLayoutManager);
mUserList.setHasFixedSize(true);
mUserList.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
Boolean reachedBottom = !recyclerView.canScrollVertically(1);
if (reachedBottom){
loadMoreMessages();
}
}
});
itemsAdapter = new ItemsAdapter(mItems);
ViewCompat.setNestedScrollingEnabled(mUserList, false);
ProfileAdmin activity = (ProfileAdmin) getActivity();
user_id = activity.getMyData();
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Items").child(user_id);
mUserDatabase.keepSynced(true);
loadItems();
return mMainView;
}
private void loadMoreMessages() {
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Items").child(user_id);
Query itemsRef = mUserDatabase.orderByKey().startAt(oldestkey).limitToLast(10);
itemsRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
ItemsRecyclerView data = dataSnapshot.getValue(ItemsRecyclerView.class);
oldestkey = dataSnapshot.getKey();
mItems.add(data);
itemsAdapter.notifyDataSetChanged();
gridLayoutManager.scrollToPositionWithOffset(7,0);
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
public void loadItems(){
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Items").child(user_id);
Query query = mUserDatabase.orderByKey().limitToLast(10);
query.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
ItemsRecyclerView data = dataSnapshot.getValue(ItemsRecyclerView.class);
for (DataSnapshot child : dataSnapshot.getChildren()){
oldestkey = child.getKey();
itemsAdapter.notifyDataSetChanged();
//now add the list
mItems.add(data);
//now add the adapter
mUserList.setAdapter(itemsAdapter);
}
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
我希望有人能理解我并找到解决该问题的方法。
答案 0 :(得分:0)
尝试将此滚动侦听器与recyclerview一起使用
在加载更多项目时,放置逻辑以加载更多项目
如果没有要加载的项目,isLastPage将返回true
在获取数据时isLoading为true,在获取数据时为false
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
/**
* Pagination class to add more items to the list when reach the last item.
*/
public abstract class PaginationScrollListener extends RecyclerView.OnScrollListener {
LinearLayoutManager layoutManager;
/**
* Supporting only LinearLayoutManager for now.
*
* @param layoutManager
*/
public PaginationScrollListener(LinearLayoutManager layoutManager) {
this.layoutManager = layoutManager;
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = layoutManager.getItemCount();
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
if (!isLoading() && !isLastPage()) {
if (visibleItemCount + firstVisibleItemPosition >= totalItemCount
&& firstVisibleItemPosition >= 0
// && totalItemCount >= ClothesFragment.itemsCount
) {
loadMoreItems();
}
}
}
protected abstract void loadMoreItems();
public abstract boolean isLastPage();
public abstract boolean isLoading();
}
将此添加到您的回收站视图
public boolean isLastPage;
public boolean isLoading;
recyclerView.addOnScrollListener(new PaginationScrollListener(your layoutManager) {
@Override
protected void loadMoreItems() {
isLoading=true;
loadMoreItems();
}
@Override
public boolean isLastPage() {
return isLastPage;
}
@Override
public boolean isLoading() {
return isLoading;
}
});
void loadMoreItems() {
isLoading=false;
after fetching your data assuming you have fetched list in your recyclerview
adapter assuming your recyclerview adapter is rvAdapter
rvAdapter.addData(list);
}
现在在您的recyclerview适配器中添加以下方法
此列表是提供您recyclerview的列表
void addData(ArrayList<Object> list) {
int size = this.list.size();
this.list.addAll(list);
int sizeNew = this.list.size();
notifyItemRangeChanged(size, sizeNew);
}
答案 1 :(得分:0)
我建议您使用此lib XRecyclerView,它支持顶部加载视图和无穷的回收利用
ERROR in Module build failed:
var colors = require('./colors');
^Invalid CSS after "v": expected 1 selector or at-rule, was "var colors = requir"
in /documents/project/node_modules/colors/lib/index.js
对于Firebase分页,您可以一次加载所有数据,并拥有两个保存数据xRecyclerView.setLoadingListener(this);
@Override
public void onRefresh() {
}
@Override
public void onLoadMore() {
}
和List<Data> allData;
的数组列表
假设您每次加载仅显示16个项目,因此在您的List<Data> adapterData;
onLoadMore() method
lastIndex是适配器中的一个变量,用于保存上次加载的索引