java - RecyclerView 不显示任何项目并且不调用任何自定义适配器的函数
问题描述
我有一个 RecyclerView 应该使用自定义适配器显示自定义项目。我在我的应用程序的不同活动中有类似的逻辑,它工作正常,现在我尝试做一些非常相似的事情,但它不起作用。
我可以看到 deal_list(用于在适配器和活动代码中保存项目的列表)构建良好,并且在适配器和活动中都进行了更新。
奇怪的是,在调用 mAdapter.notifyDataSetChanged() 之后没有调用来自适配器的函数 - 不是 getItemCount 或 onCreateViewHolder 而不是 onBindViewHolder。该列表已正确构建,我更新了适配器,然后什么也没有发生!
我已经检查/尝试过的事情:
getItemCount 不返回 0,而是返回 deal_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>
解决方案
推荐阅读
- elasticsearch - Logstash - 向 Elasticsearch 发送批量请求时出现未知错误
- css - 背景位置:x%如何与背景图像一起使用?
- xpath - Orbeon:为重复网格中的每一行添加部分
- python - 如何更改 LDA 中的默认 number_words
- javafx - SelectionMode.MULTIPLE 与 TAB 导航到下一个单元格
- css - Bootstrap 4:列固定宽度+卡片页脚中的其余行
- asp.net - 使用 asp.net 的批量电子邮件发送问题
- tensorflow - stride=2 的 2x2 deconv 内核如何工作?
- javascript - 如何转换从时间戳创建的日期为 yyyy-MM-dd 格式
- ibm-cloud - 在 Watson IoT 中使用 IBM Cloud 获取其他设备令牌值