我正在尝试使用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;
}
}
我正在调用我的片段中的适配器,如下所示
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();
}
}
请检查一下,让我知道我在哪里做错了。谢谢。
答案 0 :(得分:2)
最后,它通过从排序类中调用此方法,一旦从排球库中获取API响应。
我在ViewModel类中编写了以下方法。
public void updateList(ArrayList<Class> list){
if(adapter != null) {
adapter.updateList(list);
}
}
从片段类调用此方法,如下所示。
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();
}
答案 1 :(得分:1)
我遇到了同样的问题,你在这里犯了错误,将列表更改为itemList:
@Override
public int getItemCount() {
return itemList.size();
}
答案 2 :(得分:0)
不要
public void updateList(ArrayList<ItemModel> itemList)
{
list.clear();
this.list = itemList;
notifyDataSetChanged();
}
list.clear();
清除所有值。clear()方法用于从列表中删除所有元素。
<强> DO 强>
public void updateList(ArrayList<ItemModel> itemList)
{
this.list.addAll(itemList);
notifyDataSetChanged();
}
答案 3 :(得分:0)
尝试从您的 list.clear();
方法移除 updateList
public void updateList(ArrayList<ItemModel> itemList){
this.list.addAll(itemList);
notifyItemInserted(list.size());
notifyDataSetChanged();
}
notifyItemInserted(int position)
通知任何已登记的观察员已经新插入了位置反映的项目。
也试试这个
list.addAll(your new list);
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 {
view.notifyItemInserted(list.size());
view.notifyDataSetChanged();
}
答案 4 :(得分:0)
尝试在notifyDataSetChanged()
课程中添加Fragment
,例如......
adapter.notifyDataSetChanged();
答案 5 :(得分:0)
试试这个
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
view.adapter.notifyDataSetChanged();
}
});
答案 6 :(得分:0)
有一个相同的问题。就我而言,logcat RecyclerView中出现错误消息:未附加布局管理器;跳过布局
我已经用以下代码修复了
drop(event) {
event.preventDefault();
var data = document.getElementById(event.dataTransfer.getData("Text"));
data.style.position="relative";
data.style.left = event.pageX - data.clientWidth / 2 + "px";
data.style.top = event.pageY - data.clientHeight / 2 + "px";
}
然后notifyDataSetChanged开始工作