RecyclerView 仅在添加更多项目时显示新项目
RecyclerView only shows new items when more items are added
我有一个 RecyclerView
无限滚动。当片段打开时,它将前十个项目加载到 RecyclerView
,一切都很好。但是当它加载接下来的十个项目时,它不会将它们添加到现有项目中,而是删除旧项目并只显示新项目。
所以我的问题是:为什么当我加载更多时它不保留旧项目?
我正在使用来自 here 的无尽滚动。
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();
}
我有一个 RecyclerView
无限滚动。当片段打开时,它将前十个项目加载到 RecyclerView
,一切都很好。但是当它加载接下来的十个项目时,它不会将它们添加到现有项目中,而是删除旧项目并只显示新项目。
所以我的问题是:为什么当我加载更多时它不保留旧项目?
我正在使用来自 here 的无尽滚动。
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();
}