首页 > 解决方案 > RecyclerView 不显示任何项目并且不调用任何自定义适配器的函数

问题描述

我有一个 RecyclerView 应该使用自定义适配器显示自定义项目。我在我的应用程序的不同活动中有类似的逻辑,它工作正常,现在我尝试做一些非常相似的事情,但它不起作用。

我可以看到 deal_list(用于在适配器和活动代码中保存项目的列表)构建良好,并且在适配器和活动中都进行了更新。

奇怪的是,在调用 mAdapter.notifyDataSetChanged() 之后没有调用来自适配器的函数 - 不是 getItemCount 或 onCreateViewHolder 而不是 onBindViewHolder。该列表已正确构建,我更新了适配器,然后什么也没有发生!

我已经检查/尝试过的事情:

请帮忙!经过几天的调试和在网上搜索解决方案后,我真的很绝望......

片段中的相关代码:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root_view      = inflater.inflate(R.layout.fragment_live_sales, container, false);
        recyclerView        = root_view.findViewById(R.id.sales_RecyclerView);


        setupAdapter();
        loadOffersFromDb();

        return inflater.inflate(R.layout.fragment_live_sales, container, false);
    }

    private void setupAdapter() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter            = new LiveSaleAdapter(deals_list);
        recyclerView.setAdapter(mAdapter);
    }

    public void loadOffersFromDb() {

                ... // creation and update of deals_list using deals_list.add and never creating a new list

                mAdapter.notifyDataSetChanged(); // after this, nothing happens. deals_list contains the items expected, at this point.
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });



    }

来自适配器的相关代码:

public class LiveSaleAdapter extends android.support.v7.widget.RecyclerView.Adapter<LiveSaleAdapter.SaleViewHolder> {
    private ArrayList<Deal> mDeals;
    private Context mContext;

    public LiveSaleAdapter(ArrayList<Deal> deals) {
        mDeals = deals;
    }

    @NonNull
    @Override
    public SaleViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        mContext                     = viewGroup.getContext();
        int             layout_id    = R.layout.item_cust_offer;
        LayoutInflater  inflater     = LayoutInflater.from(mContext);
        View            view         = inflater.inflate(layout_id, viewGroup, false);

        return new SaleViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final SaleViewHolder holder, int position) {
        Deal current_deal = mDeals.get(position);

        // updating view (a lot of calls to setText etc.)
...
    }

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

    class SaleViewHolder extends android.support.v7.widget.RecyclerView.ViewHolder {
        // Offer Details
...

        // Shop Details
        ...

        public SaleViewHolder(@NonNull View itemView) {
            super(itemView);

            // Init all views (lots of calls to findViewById)
            ...
        }
    }
}

片段 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".CustomerApp.LiveSales.LiveSalesFragment">

    <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/sales_RecyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        tools:itemCount="3"
        tools:listitem="@layout/item_cust_offer"
        tools:orientation="vertical"
        tools:scrollbars="horizontal"
        tools:spanCount="1"
        android:nestedScrollingEnabled="false"
        android:isScrollContainer="false"
        >

    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

部分项目 XML:

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

    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="400dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:layout_marginBottom="20dp"
        card_view:cardCornerRadius="10dp">

        ...


    </android.support.v7.widget.CardView>
</LinearLayout>

标签: javaandroidandroid-recyclerviewrecyclerview-layout

解决方案


推荐阅读