gpt4 book ai didi

java - recyclerview notifyDataSetChanged() 不工作

转载 作者:行者123 更新时间:2023-11-29 08:32:10 25 4
gpt4 key购买 nike

我正在尝试使用 updateList() 中的 notifyDataSetchanged() 更新 myadapter。但不适用于 recyclerview。我可以在 updateList() 中看到列表大小及其显示的实际结果,但只有 reclerview 没有更新。此类是在 MVVM 设计模式中设计的。

   public class TasksAdapter extends RecyclerView.Adapter<TasksAdapter.TasksAdapterViewHolder> {
private static final String TAG = TasksAdapter.class.getSimpleName();
static ArrayList<ItemModel> list;
static Context mContext;
private Fragment fragment;
private int row_index = -1;
private SessionManager session;
// SelectColorVM colorVM;

public TasksAdapter(Fragment fragment, Context context, ArrayList<ItemModel> itemList) {
this.mContext = context;
this.list = itemList;
this.fragment = fragment;
session = new SessionManager(fragment.getContext());
}

public void updateList(ArrayList<ItemModel> itemList){
list.clear();
this.list = itemList;
notifyDataSetChanged();
}

@Override
public TasksAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
TasksItemBinding binding =
DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.tasks_item,
parent, false);
return new TasksAdapter.TasksAdapterViewHolder(binding);
}

@Override
public void onBindViewHolder(TasksAdapterViewHolder holder, final int position) {
holder.binding.setViewModel(list.get(position));
if(position == 0 && list.get(position).btnText.get().toString().length() > 0)
holder.binding.btnUpdate.setVisibility(View.VISIBLE);
else
holder.binding.btnUpdate.setVisibility(View.INVISIBLE);
holder.binding.layoutItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// row_index = position;
// notifyDataSetChanged();
Fragment fragment = new DetailFragment();
FragmentManager manager = ((FragmentActivity) mContext).getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
Bundle bundle = new Bundle();
ItemModel item = list.get(position);
bundle.putSerializable("item", item);
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.id_frame, fragment, "details");
fragmentTransaction.addToBackStack("details");
fragmentTransaction.commit();
}
});
holder.binding.btnUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
HashMap<String, String> user = session.getUserDetails();
if(list.get(position).btnText.get().equals("Task Start")){
LandingEngine.updateStatusAndSetStartTimeForRequest(fragment, user.get(SessionManager.KEY_CLEANER_ID), list.get(position).sid.get().toString());
} else if(list.get(position).btnText.get().equals("Task Complete")){
LandingEngine.updateStatusAndSetCompleteTimeForRequest(fragment, user.get(SessionManager.KEY_CLEANER_ID), list.get(position).sid.get().toString());
}
}
});
}

@Override
public int getItemCount() {
return list.size();
}

public static class TasksAdapterViewHolder extends RecyclerView.ViewHolder {
TasksItemBinding binding;

public TasksAdapterViewHolder(TasksItemBinding rowbinding) {
super(rowbinding.getRoot());
this.binding = rowbinding;
}

}
@Override
public long getItemId(int position) {
return position;
}

@Override
public int getItemViewType(int position) {
return position;
}


}

我正在从我的 fragment 中调用适配器,如下所示

if(list.size() > 0) {
if(adapter == null) {
view.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(fragment.getContext());
view.setLayoutManager(mLayoutManager);
adapter = new TasksAdapter(fragment, fragment.getContext(), list);
view.setAdapter(adapter);
view.addItemDecoration(new DividerItemDecoration(fragment.getContext()));
} else {
adapter.updateList(list);
// view.getAdapter().notifyDataSetChanged();
// view.invalidate();
}
}

请检查一下,让我知道我哪里做错了。在此先感谢..

最佳答案

一旦从 volley 库获得 API 响应,最终它会通过从 fragment 类调用此方法来工作。

我在 ViewModel 类中编写了以下方法。

public void updateList(ArrayList<Class> list){
if(adapter != null) {
adapter.updateList(list);
}
}

然后从 fragment 类中调用此方法,如下所示。

ArrayList<StatisticsModel> list = (ArrayList) data;
viewModel.updateList(list);

因此在适配器类中更新方法如下。

public void updateList(ArrayList<StatisticsModel> itemList){
list.clear();
// this.list = itemList;
// list.addAll(itemList);
// notifyItemInserted(list.size());
// notifyDataSetChanged();
this.list.addAll(itemList);
// notifyItemInserted(list.size());
notifyDataSetChanged();
}

关于java - recyclerview notifyDataSetChanged() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47009273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com