如何更新 RecyclerView 適配器數據 - How to update RecyclerView Adapter Data

語言: CN / TW / HK

問題:

I am trying to figure out what is the issue with updating RecyclerView 's Adapter.我想弄清楚更新RecyclerView的適配器有什麼問題。

After I get a new List of products, I tried to:在獲得新的產品列表後,我嘗試:

  1. Update the ArrayList from the fragment where recyclerView is created, set new data to adapter, and then call adapter.notifyDataSetChanged() ;從創建recyclerView的fragment更新ArrayList ,設置新數據到adapter,然後調用adapter.notifyDataSetChanged() it did not work.它不起作用。

  2. Create a new adapter, as others did, and it worked for them, but there wasn't any change for me: recyclerView.setAdapter(new RecyclerViewAdapter(newArrayList))像其他人一樣創建一個新的適配器,它對他們有用,但對我來説沒有任何變化: recyclerView.setAdapter(new RecyclerViewAdapter(newArrayList))

  3. Create a method in Adapter which updates the data as follows:Adapter創建一個更新數據的方法,如下所示:

     public void updateData(ArrayList<ViewModel> viewModels) { items.clear(); items.addAll(viewModels); notifyDataSetChanged(); }

    Then I call this method whenever I want to update the data list;然後每當我想更新數據列表時我都會調用這個方法; it did not work.它不起作用。

  4. To check if I can modify the recyclerView in any way, and I tried to remove at least an item:為了檢查我是否可以以任何方式修改 recyclerView,我嘗試刪除至少一個項目:

     public void removeItem(int position) { items.remove(position); notifyItemRemoved(position); }

Everything remained as it was.一切都保持原樣。

Here is my Adapter:這是我的適配器:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> implements View.OnClickListener {

    private ArrayList<ViewModel> items;
    private OnItemClickListener onItemClickListener;

    public RecyclerViewAdapter(ArrayList<ViewModel> items) {
        this.items = items;
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler, parent, false);
        v.setOnClickListener(this);
        return new ViewHolder(v);
    }

    public void updateData(ArrayList<ViewModel> viewModels) {
        items.clear();
        items.addAll(viewModels);
        notifyDataSetChanged();
    }
    public void addItem(int position, ViewModel viewModel) {
        items.add(position, viewModel);
        notifyItemInserted(position);
    }

    public void removeItem(int position) {
        items.remove(position);
        notifyItemRemoved(position);
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ViewModel item = items.get(position);
        holder.title.setText(item.getTitle());
        Picasso.with(holder.image.getContext()).load(item.getImage()).into(holder.image);
        holder.price.setText(item.getPrice());
        holder.credit.setText(item.getCredit());
        holder.description.setText(item.getDescription());

        holder.itemView.setTag(item);
    }


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


    @Override
    public void onClick(final View v) {
        // Give some time to the ripple to finish the effect
        if (onItemClickListener != null) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    onItemClickListener.onItemClick(v, (ViewModel) v.getTag());
                }
            }, 0);
        }
    }

    protected static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView image;
        public TextView price, credit, title, description;

        public ViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.image);
            price = (TextView) itemView.findViewById(R.id.price);
            credit = (TextView) itemView.findViewById(R.id.credit);
            title = (TextView) itemView.findViewById(R.id.title);
            description = (TextView) itemView.findViewById(R.id.description);
        }
    }

    public interface OnItemClickListener {

        void onItemClick(View view, ViewModel viewModel);

    }
}

And I initiate RecyclerView as follows:我啟動RecyclerView如下:

recyclerView = (RecyclerView) view.findViewById(R.id.recycler);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 5));
adapter = new RecyclerViewAdapter(items);
adapter.setOnItemClickListener(this);
recyclerView.setAdapter(adapter);

So, how do I actually update adapter data in order to display newly received items?那麼,我如何實際更新適配器數據以顯示新收到的項目?


The issue was that the layout where gridView was looked as follows:問題是 gridView 的佈局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:tag="catalog_fragment"
    android:layout_height="match_parent">

    <FrameLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"/>

        <ImageButton
            android:id="@+id/fab"
            android:layout_gravity="top|end"
            style="@style/FabStyle"/>

    </FrameLayout>
</LinearLayout>

Then I just removed LinearLayout and made FrameLayout as the parent layout.然後我只是刪除了LinearLayout並將FrameLayout作為父佈局。


解決方案:

參考一: http://stackoom.com/question/27c91
參考二: How to update RecyclerView Adapter Data
「其他文章」