Recyclerview 适配器用空数组初始化,随后用值填充
Recyclerview adapter initialised with empty array and subsequently populated with values
我在我的 onCreate 方法中用一个空列表创建了一个 recyclerview 适配器。这样做是必要的,因为标记到 mAdapter 上的是一个滚动侦听器,如果我向上滚动,它允许我从数据库服务器获取另一个页面(为了简洁起见,我没有复制和粘贴该代码)。
public class MainActivity extends ActionBarActivity {
ArrayList<Post> totalPost = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
mAdapter = new MyAdapter(totalPost, mRecyclerView);
mRecyclerView.setAdapter(mAdapter); }
我在 onResume 中调用我的网络操作,onResume 发生在 onCreate 之后。
一旦我的网络操作成功,我就会这样做,但没有任何反应:
totalPost = Lists.newArrayList(result.getPosts()); //saving the network results into an array
mAdapter.notifyDataSetChanged();
我也试过这个:
totalPost = Lists.newArrayList(result.getPosts()); //saving the network results into an array
mAdapter.notifyItemRangeChanged(0, totalPost.size());
我也试过这个并且它有效但它并不理想。我在这里创建了一个新的适配器,我也失去了我的滚动监听器。我也不能在引用我的网络操作的方法中引用我的滚动侦听器,因为那将是 'circular reference':
totalPost = Lists.newArrayList(result.getPosts()); //saving the network results into an array
mAdapter = new MyAdapter(totalPost, mRecyclerView);
mRecyclerView.setAdapter(mAdapter);
为什么适配器不工作?
我认为您的适配器没有接受新值。尝试创建一个在您的适配器中定义的方法 class 并使用传入的新值调用该方法。像这样:
totalPost = Lists.newArrayList(result.getPosts());
mAdapter.update(totalPost, mRecyclerView)
// 适配器 Class
public void update(ArrayList<Post> totalP, RecyclerView recycler) {
totalPost = totalP;
mRecyclerView = recycler;
this.notifyDataSetChanged();
您应该使用添加功能扩展您的适配器。
创建适配器并将其分配给 recyclerview 后,您应该 'communicate' 通过适配器使用 recyclerview。
将类似的东西添加到您的适配器中。
其中 mItems 是适配器中的内部支持数组。
public void addAll(int location, Collection<T> items) {
mItems.addAll(location, items);
notifyItemRangeInserted(location, items.size());
}
public void add(T item, int position) {
mItems.add(position, item);
notifyItemInserted(position);
}
我在我的 onCreate 方法中用一个空列表创建了一个 recyclerview 适配器。这样做是必要的,因为标记到 mAdapter 上的是一个滚动侦听器,如果我向上滚动,它允许我从数据库服务器获取另一个页面(为了简洁起见,我没有复制和粘贴该代码)。
public class MainActivity extends ActionBarActivity {
ArrayList<Post> totalPost = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
mAdapter = new MyAdapter(totalPost, mRecyclerView);
mRecyclerView.setAdapter(mAdapter); }
我在 onResume 中调用我的网络操作,onResume 发生在 onCreate 之后。
一旦我的网络操作成功,我就会这样做,但没有任何反应:
totalPost = Lists.newArrayList(result.getPosts()); //saving the network results into an array
mAdapter.notifyDataSetChanged();
我也试过这个:
totalPost = Lists.newArrayList(result.getPosts()); //saving the network results into an array
mAdapter.notifyItemRangeChanged(0, totalPost.size());
我也试过这个并且它有效但它并不理想。我在这里创建了一个新的适配器,我也失去了我的滚动监听器。我也不能在引用我的网络操作的方法中引用我的滚动侦听器,因为那将是 'circular reference':
totalPost = Lists.newArrayList(result.getPosts()); //saving the network results into an array
mAdapter = new MyAdapter(totalPost, mRecyclerView);
mRecyclerView.setAdapter(mAdapter);
为什么适配器不工作?
我认为您的适配器没有接受新值。尝试创建一个在您的适配器中定义的方法 class 并使用传入的新值调用该方法。像这样:
totalPost = Lists.newArrayList(result.getPosts());
mAdapter.update(totalPost, mRecyclerView)
// 适配器 Class
public void update(ArrayList<Post> totalP, RecyclerView recycler) {
totalPost = totalP;
mRecyclerView = recycler;
this.notifyDataSetChanged();
您应该使用添加功能扩展您的适配器。 创建适配器并将其分配给 recyclerview 后,您应该 'communicate' 通过适配器使用 recyclerview。 将类似的东西添加到您的适配器中。 其中 mItems 是适配器中的内部支持数组。
public void addAll(int location, Collection<T> items) {
mItems.addAll(location, items);
notifyItemRangeInserted(location, items.size());
}
public void add(T item, int position) {
mItems.add(position, item);
notifyItemInserted(position);
}