Android notifyDataSetChange 不适用于 RecyclerView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24495542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
notifyDataSetChange not working on RecyclerView
提问by wmora
I'm working with Android's new RecyclerView but I can't get my custom adapter to refresh whenever I call one of the "notify" methods.
我正在使用 Android 的新 RecyclerView,但是每当我调用“通知”方法之一时,我都无法刷新我的自定义适配器。
I've tried calling notifyDataSetChanged, notifyItemRangeInserted and notifyItemInserted and none of them seem to work.
我试过调用 notifyDataSetChanged、notifyItemRangeInserted 和 notifyItemInserted,但它们似乎都不起作用。
Here's the code for my custom adapter. I'm basically trying to refresh a list of Strings:
这是我的自定义适配器的代码。我基本上是在尝试刷新字符串列表:
package com.mycompany.myapp.adapters;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.mycompany.myapp.R;
import java.util.List;
public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.ViewHolder> {
private List<String> mDataset;
public FeedAdapter(List<String> dataset) {
super();
mDataset = dataset;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
LinearLayout v = (LinearLayout) LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_feed, parent, false);
v.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.setText(mDataset.get(position));
}
@Override
public int getItemCount() {
return mDataset.size();
}
public void setDataset(List<Status> dataset) {
mDataset = dataset;
// This isn't working
notifyItemRangeInserted(0, mDataset.size());
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView mFeedText;
public ViewHolder(View v) {
super(v);
mFeedText = (TextView) v.findViewById(R.id.feed_text);
}
private void setText(String text) {
mFeedText.setText(text);
}
}
}
Anyone else having this issue?
还有谁有相同的问题吗?
Thanks!
谢谢!
采纳答案by wmora
回答by kosiara - Bartosz Kosarzycki
I was trying to update RecycleView with notifyDataSetChanged() methodin response to com.google.common.eventbus.Subscribe.
我试图用notifyDataSetChanged() 方法更新 RecycleView 以响应com.google.common.eventbus.Subscribe。
Like @wmora mentioned the problem was that the notify method was not called in the main UI thread.
就像@wmora 提到的那样,问题是没有在主 UI 线程中调用通知方法。
I resolved it with AndroidAnnotations' @UiThread
我用 AndroidAnnotations 的 @UiThread 解决了这个问题
@UiThread
protected void dataSetChanged() {
notifyDataSetChanged();
}
which is equivalent to:
这相当于:
final Adapter adapter = this;
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
}
});
note: just separate new Handler into class private field
注意:只需将 new Handler 分成类私有字段