我有一个RecyclerView
,它可以无限滚动。当片段打开时,它将前十个项目加载到RecyclerView
中,一切都很好。但是,当它加载接下来的十个项目时,它不会将它们添加到兴奋项目中,而是删除旧项目,只显示新项目。
所以我的问题是:为什么当我加载更多时它不保留旧项目
我从这里开始使用无尽滚动。
Fragment.java
public class twoFragment extends Fragment implements LifecycleOwner {
private twoViewModel twoViewModel;
private RecyclerView recyclerView;
private RecyclerViewAdapter recyclerViewAdapter;
private EndlessRecyclerViewScrollListener scrollListener;
WebRequest webRequest = new WebRequest();
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
System.out.println("************ onCreateView ***********");
System.out.println("************ ViewModelProvider ***********");
twoViewModel = new ViewModelProvider(this).get(twoViewModel.class);
View root = inflater.inflate(R.layout.two, container, false);
recyclerView = root.findViewById(R.id.rv_main);
recyclerView.setHasFixedSize(true);
recyclerViewAdapter = new RecyclerViewAdapter(requireActivity());
final int columns = getResources().getInteger(R.integer.gallery_columns);
GridLayoutManager gridLayoutManager = new GridLayoutManager(requireActivity(), columns);
recyclerView.setLayoutManager(gridLayoutManager);
// Endless scrolling
scrollListener = new EndlessRecyclerViewScrollListener(gridLayoutManager) {
@Override
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
// Triggered only when new data needs to be appended to the list
// Add whatever code is needed to append new items to the bottom of the list
System.out.println("************ onLoadMore >>> EndlessRecyclerViewScrollListener ***********");
System.out.println("************ onLoadMore >>> page ***********" + page);
webRequest.MakeRequest("BROWSE_REQUEST", page +1, "", twoViewModel.getUserMutableLiveData());
}
};
// Adds the scroll listener to RecyclerView
recyclerView.addOnScrollListener(scrollListener);
twoViewModel.getUserMutableLiveData().observe(getViewLifecycleOwner(),userListUpdateObserver);
return root;
};
private Observer<ArrayList<User>> userListUpdateObserver = new Observer<ArrayList<User>>() {
@Override
public void onChanged(ArrayList<User> userArrayList) {
System.out.println("************ onChanged ***********");
recyclerView.setAdapter(recyclerViewAdapter);
((RecyclerViewAdapter)recyclerView.getAdapter()).setData(userArrayList);
}
};
}
RecyclerViewAdapter.java
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Activity context;
private ArrayList<User> userArrayList;
public RecyclerViewAdapter(Activity context) {
this.context = context;
this.userArrayList = userArrayList;
}
public void setData(ArrayList<User> userArrayListOne){
userArrayList = userArrayListOne;
notifyDataSetChanged();
System.out.println("%% %% notifyDataSetChanged %% %%");
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
System.out.println("************ onCreateViewHolder ***********");
View rootView = LayoutInflater.from(context).inflate(R.layout.item,parent,false);
return new RecyclerViewViewHolder(rootView);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
System.out.println("************ onBindViewHolder ***********");
User user = userArrayList.get(position);
RecyclerViewViewHolder viewHolder= (RecyclerViewViewHolder) holder;
// Set title as HTML code on view
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
viewHolder.txtView_title.setText(Html.fromHtml(user.getTitle(), Html.FROM_HTML_MODE_COMPACT));
} else {
viewHolder.txtView_title.setText(Html.fromHtml(user.getTitle()));
}
viewHolder.txtView_description.setText(user.getDescription()); // TODO add error png
Glide.with(context).load(user.getImagePath()).centerInside().error(R.drawable.yahushuas_commands_pt__2).into(viewHolder.imgView_icon);
}
@Override
public int getItemCount() {
return userArrayList.size();
}
class RecyclerViewViewHolder extends RecyclerView.ViewHolder {
ImageView imgView_icon;
TextView txtView_title;
TextView txtView_description;
public RecyclerViewViewHolder(@NonNull View itemView) {
super(itemView);
imgView_icon = itemView.findViewById(R.id.imgView_icon);
txtView_title = itemView.findViewById(R.id.txtView_title);
txtView_description = itemView.findViewById(R.id.txtView_description);
}
}
}
在RecyclerViewAdapter.java
的setData
方法中,您通过每次分配新的ArrayList
来更新userArrayList
。因此,旧数据正在被替换。您需要将新数据添加到现有列表中。
private ArrayList<User> userArrayLis = new ArrayList<User>(); // Initialise the arraylist
public void setData(ArrayList<User> userArrayListOne){
userArrayList.addAll(userArrayListOne);
notifyDataSetChanged();
}