notifyDataSetChanged() 在适配器中不起作用
notifyDataSetChange() Is not working in adapter
我正在使用 Android 的新 RecyclerView,但每当我调用其中一种“通知”方法时,我的自定义适配器都无法刷新。
我试过调用 notifyDataSetChanged、notifyItemRangeInserted 和 notifyItemInserted,其中 none 似乎有效。
我得到的列表大小是正确的,但是 当我滚动页面时,recycleview 只设置了 10 个项目 api 是 called.upadted 列表大小是 20 但不是显示其他 10 items.i 想显示列表中的所有项目请帮助我....
这是我的适配器 :
public class NotificationAdapter extends RecyclerView.Adapter<NotificationAdapter.ViewHolder> {
Context context;
List<NotificationDataItem> mData;
public NotificationAdapter(Context context, List<NotificationDataItem> mData) {
this.context=context;
this.mData=mData;
}
@NotNull
@Override
public NotificationAdapter.ViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
AdapterNotificationBinding adapterNotificationBinding = DataBindingUtil.inflate(
LayoutInflater.from(parent.getContext()), R.layout.adapter_notification,
parent, false);
return new NotificationAdapter.ViewHolder(adapterNotificationBinding);
}
@Override
public void onBindViewHolder(@NotNull NotificationAdapter.ViewHolder holder, int position) {
if (mData.get(position).getName()!=null&&!mData.get(position).getName().isEmpty()){
holder.binding.tvUserNotification.setText(mData.get(position).getName());
}
if (mData.get(position).getBody()!=null&&!mData.get(position).getBody().isEmpty()){
holder.binding.tvUserNotificationBody.setText(mData.get(position).getBody());
}
if (mData.get(position).getCreatedAt()!=null&&!mData.get(position).getCreatedAt().isEmpty()){
holder.binding.tvTime.setText(mData.get(position).getCreatedAt());
}
if (mData.get(position).getAvatar()!=null){
Glide.with(context)
.load(mData.get(position).getAvatar())
.circleCrop()
.placeholder(R.drawable.ic_white_speaker)
.error(R.drawable.ic_white_speaker)
.into(holder.binding.ivUser);
}
/*type => 0 = comment, 1 = like, 2 = unlike, 3 = comment reply
*/
if (mData.get(position).getType()!=null){
switch (mData.get(position).getType()) {
case "0":case "3":
holder.binding.ivEditProfileImage.setColorFilter(ContextCompat.getColor(context, R.color.colorgray), android.graphics.PorterDuff.Mode.SRC_IN);
holder.binding.ivEditProfileImage.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_comment_filled));
break;
case "1":
holder.binding.ivEditProfileImage.setColorFilter(ContextCompat.getColor(context, R.color.colorgray), android.graphics.PorterDuff.Mode.SRC_IN);
holder.binding.ivEditProfileImage.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_like_filled));
break;
case "2":
holder.binding.ivEditProfileImage.setRotation(180);
holder.binding.ivEditProfileImage.setColorFilter(ContextCompat.getColor(context, R.color.colorgray), android.graphics.PorterDuff.Mode.SRC_IN);
holder.binding.ivEditProfileImage.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_like_filled));
break;
/*case "3":
holder.binding.ivEditProfileImage.setColorFilter(ContextCompat.getColor(context, R.color.colorgray), android.graphics.PorterDuff.Mode.SRC_IN);
holder.binding.ivEditProfileImage.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_comment_filled));
break;*/
}
}
holder.binding.llNotification.setOnClickListener(v->{
Intent intent = new Intent(context, PostDetailsActivity.class);
intent.putExtra("postId", mData.get(position).getPostId());
context.startActivity(intent);
});
holder.binding.ivUser.setOnClickListener(v->{
Intent intent = new Intent(context, OtherUserProfileActivity.class);
intent.putExtra("userId", mData.get(position).getUserId());
context.startActivity(intent);
});
}
@Override
public int getItemCount() {
return mData.size();
}
public void updateList(List<NotificationDataItem> mNotification) {
this.mData.addAll(mNotification);
Log.d("updateList", "updateList:"+mData.size());
notifyDataSetChanged();
Log.d("updateList123", "updateList123:"+mData.size());
}
public static class ViewHolder extends RecyclerView.ViewHolder {
AdapterNotificationBinding binding;
public ViewHolder(AdapterNotificationBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
}
这是我附加的方式:
adapter = new NotificationAdapter( getActivity(),response.body().getData().getData() );
if (page == 1) {
binding.rvNotification.setAdapter(adapter);
} else {
adapter.updateList(response.body().getData().getData());
}
我在调用第 2 页时得到了解决方案我初始化了新的适配器所以我在 recyclerview 中得到了相同的数据所以我试试这个:
if (page == 1) {
adapter =new NotificationAdapter(getActivity(),response.body().getData().getData());
binding.rvNotification.setAdapter(adapter);
} else {
adapter.updateList(response.body().getData().getData());
}
您每次都在创建新的 adapter
实例,而不是更新已创建并附加到 RecyclerView
的实例。将您的创建代码移动到 if
中,仅创建和附加适配器一次,然后在 else
中您可以更新它(相同的引用,相同的对象)
if (page == 1) {
adapter = new NotificationAdapter(getActivity(), response.body().getData().getData());
binding.rvNotification.setAdapter(adapter);
} else {
adapter.updateList(response.body().getData().getData());
}
另一种方法是每次都创建新的适配器,但您还必须调用 setAdapter(adapter);
(新适配器)。更新现有的是更好的方法
我正在使用 Android 的新 RecyclerView,但每当我调用其中一种“通知”方法时,我的自定义适配器都无法刷新。
我试过调用 notifyDataSetChanged、notifyItemRangeInserted 和 notifyItemInserted,其中 none 似乎有效。
我得到的列表大小是正确的,但是 当我滚动页面时,recycleview 只设置了 10 个项目 api 是 called.upadted 列表大小是 20 但不是显示其他 10 items.i 想显示列表中的所有项目请帮助我....
这是我的适配器 :
public class NotificationAdapter extends RecyclerView.Adapter<NotificationAdapter.ViewHolder> {
Context context;
List<NotificationDataItem> mData;
public NotificationAdapter(Context context, List<NotificationDataItem> mData) {
this.context=context;
this.mData=mData;
}
@NotNull
@Override
public NotificationAdapter.ViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
AdapterNotificationBinding adapterNotificationBinding = DataBindingUtil.inflate(
LayoutInflater.from(parent.getContext()), R.layout.adapter_notification,
parent, false);
return new NotificationAdapter.ViewHolder(adapterNotificationBinding);
}
@Override
public void onBindViewHolder(@NotNull NotificationAdapter.ViewHolder holder, int position) {
if (mData.get(position).getName()!=null&&!mData.get(position).getName().isEmpty()){
holder.binding.tvUserNotification.setText(mData.get(position).getName());
}
if (mData.get(position).getBody()!=null&&!mData.get(position).getBody().isEmpty()){
holder.binding.tvUserNotificationBody.setText(mData.get(position).getBody());
}
if (mData.get(position).getCreatedAt()!=null&&!mData.get(position).getCreatedAt().isEmpty()){
holder.binding.tvTime.setText(mData.get(position).getCreatedAt());
}
if (mData.get(position).getAvatar()!=null){
Glide.with(context)
.load(mData.get(position).getAvatar())
.circleCrop()
.placeholder(R.drawable.ic_white_speaker)
.error(R.drawable.ic_white_speaker)
.into(holder.binding.ivUser);
}
/*type => 0 = comment, 1 = like, 2 = unlike, 3 = comment reply
*/
if (mData.get(position).getType()!=null){
switch (mData.get(position).getType()) {
case "0":case "3":
holder.binding.ivEditProfileImage.setColorFilter(ContextCompat.getColor(context, R.color.colorgray), android.graphics.PorterDuff.Mode.SRC_IN);
holder.binding.ivEditProfileImage.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_comment_filled));
break;
case "1":
holder.binding.ivEditProfileImage.setColorFilter(ContextCompat.getColor(context, R.color.colorgray), android.graphics.PorterDuff.Mode.SRC_IN);
holder.binding.ivEditProfileImage.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_like_filled));
break;
case "2":
holder.binding.ivEditProfileImage.setRotation(180);
holder.binding.ivEditProfileImage.setColorFilter(ContextCompat.getColor(context, R.color.colorgray), android.graphics.PorterDuff.Mode.SRC_IN);
holder.binding.ivEditProfileImage.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_like_filled));
break;
/*case "3":
holder.binding.ivEditProfileImage.setColorFilter(ContextCompat.getColor(context, R.color.colorgray), android.graphics.PorterDuff.Mode.SRC_IN);
holder.binding.ivEditProfileImage.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_comment_filled));
break;*/
}
}
holder.binding.llNotification.setOnClickListener(v->{
Intent intent = new Intent(context, PostDetailsActivity.class);
intent.putExtra("postId", mData.get(position).getPostId());
context.startActivity(intent);
});
holder.binding.ivUser.setOnClickListener(v->{
Intent intent = new Intent(context, OtherUserProfileActivity.class);
intent.putExtra("userId", mData.get(position).getUserId());
context.startActivity(intent);
});
}
@Override
public int getItemCount() {
return mData.size();
}
public void updateList(List<NotificationDataItem> mNotification) {
this.mData.addAll(mNotification);
Log.d("updateList", "updateList:"+mData.size());
notifyDataSetChanged();
Log.d("updateList123", "updateList123:"+mData.size());
}
public static class ViewHolder extends RecyclerView.ViewHolder {
AdapterNotificationBinding binding;
public ViewHolder(AdapterNotificationBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
}
这是我附加的方式:
adapter = new NotificationAdapter( getActivity(),response.body().getData().getData() );
if (page == 1) {
binding.rvNotification.setAdapter(adapter);
} else {
adapter.updateList(response.body().getData().getData());
}
我在调用第 2 页时得到了解决方案我初始化了新的适配器所以我在 recyclerview 中得到了相同的数据所以我试试这个:
if (page == 1) {
adapter =new NotificationAdapter(getActivity(),response.body().getData().getData());
binding.rvNotification.setAdapter(adapter);
} else {
adapter.updateList(response.body().getData().getData());
}
您每次都在创建新的 adapter
实例,而不是更新已创建并附加到 RecyclerView
的实例。将您的创建代码移动到 if
中,仅创建和附加适配器一次,然后在 else
中您可以更新它(相同的引用,相同的对象)
if (page == 1) {
adapter = new NotificationAdapter(getActivity(), response.body().getData().getData());
binding.rvNotification.setAdapter(adapter);
} else {
adapter.updateList(response.body().getData().getData());
}
另一种方法是每次都创建新的适配器,但您还必须调用 setAdapter(adapter);
(新适配器)。更新现有的是更好的方法