RecyclerView不显示任何项目,并且不调用任何自定义适配器的功能

时间:2019-05-13 09:43:27

标签: java android android-recyclerview recycler-adapter recyclerview-layout

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

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

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

我已经检查/尝试过的东西:

  • getItemCount返回的不是0,而是Deals_list.size()。

  • setLayoutManager在设置适配器之前和之后-均不起作用。

  • 检查了XML中的常见错误,而在我的错误中却没有发现。

请帮助!经过数天的调试和在网络上寻找解决方案之后,我真的很绝望...

片段中的相关代码:

@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>

0 个答案:

没有答案