notifyDataSetChange不工作的RecyclerView [英] notifyDataSetChange not working on RecyclerView
问题描述
我正与Android的新RecyclerView,但我不能让我的自定义适配器刷新每当我称之为的通知的方法之一。
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.
我已经打过电话notifyDataSetChanged,notifyItemRangeInserted和notifyItemInserted和他们都不工作。
I've tried calling notifyDataSetChanged, notifyItemRangeInserted and notifyItemInserted and none of them seem to work.
这里的code我的自定义适配器。我基本上是试图刷新字符串列表:
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?
谢谢!
推荐答案
我的问题是,我没有通知主线程上的变化,所以变化是不可见的时候了。这是同样的问题,指出<一href="http://stackoverflow.com/questions/2919778/update-listview-in-the-main-thread-from-another-thread">here.
My issue was that I was not notifying the change on the main thread, therefore the change was not visible right away. It is the same issue pointed out here.
这篇关于notifyDataSetChange不工作的RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!