适配器类
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.MyHolder> {
private RecyclerviewListener mlistener;
private Context context;
private ArrayList<ItemModel> data = new ArrayList<>();
public ItemAdapter(Context context, ArrayList<ItemModel> data, RecyclerviewListener mlistener) {
this.mlistener = mlistener;
this.context = context;
this.data = data;
}
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v;
LayoutInflater inflater = LayoutInflater.from(context);
v = inflater.inflate(R.layout.item_adapter, parent, false);
final ItemAdapter.MyHolder myHolder = new ItemAdapter.MyHolder(v);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mlistener.onClick(view, myHolder.getAdapterPosition());
}
});
return myHolder;
}
@Override
public void onBindViewHolder(@NonNull final MyHolder holder, int position) {
ItemModel i = data.get(position);
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.image_holder2);
// make sure Glide doesn't load anything into this view until told otherwise
// remove the placeholder (optional); read comments below
holder.imageView.setImageDrawable(null);Glide.with(context).setDefaultRequestOptions(requestOptions).load(i.getImage1()).into(holder.imageView); }
@Override
public int getItemCount() {
return data.size();
}
class MyHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public MyHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
}
}}
*每当我快速向下滚动时。Recyclerview两次显示相同的项目,我尝试了许多解决方案,但没有人在工作*
首先用于检索数据并获取最后检索到的文档的方法,即lastVisible变量
private void getData() {
Query data = firestore.collection("Madhya Pradesh").document(cityname).collection("Product").document(category).collection("View").limit(3);
data.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
QuerySnapshot d = task.getResult();
lastVisible = d.getDocuments()
.get(d.size() - 1);
for (DocumentSnapshot doc : task.getResult()) {
ItemModel item = new ItemModel();
if (doc.getString("Image1") != null) {
item.setImage1(doc.getString("Image1"));
} else {
item.setImage1("null");
}
rdata.add(item);
itemAdapter.notifyDataSetChanged();
}
} else {
// error from firebase handle it;
}
}
});
}
查询方法(Firebase),用于在滚动时检索数据
private void scroll() {
itemRView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
isScrolling = true;
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
currentitem = manager.getChildCount();
totalitem = manager.getItemCount();
scrollOutitem = manager.findFirstVisibleItemPosition();
if (isScrolling && (currentitem + scrollOutitem == totalitem)) {
progressBar.setVisibility(View.VISIBLE);
isScrolling = false;
final Query data = firestore.collection("State").document(cityname).collection("Product").document(category).collection("View").startAfter(lastVisible).limit(2);
data.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
QuerySnapshot d = task.getResult();
lastVisible = d.getDocuments()
.get(d.size() - 1);
if (task.isSuccessful() && !task.getResult().isEmpty()) {
progressBar.setVisibility(View.GONE);
for (DocumentSnapshot doc : task.getResult()) {
ItemModel item = new ItemModel();
if (doc.getString("Image1") != null) {
item.setImage1(doc.getString("Image1"));
} else {
item.setImage1("null");
}
rdata.add(item);
itemAdapter.notifyDataSetChanged();
}
}
}
});
}
}
});
}
答案 0 :(得分:0)
问题可能出在滚动过程中。 在滚动RecyclerView时,将重新绑定Holder并执行“ onBindViewHolder(..)”,因此,如果您只想在滚动过程中加载新的可见行(不做任何特殊操作),则可以删除滚动过程。>