android - 未调用 Android 的 RecyclerView 适配器方法
问题描述
当我登录(使用谷歌邮件)时, LoginFragment
它被替换为DrinkCategoryFragment
包含RecyclerView
.
这些项目是从 Firebase 数据库中正确获取的,但布局不存在,我不知道为什么。
在RecyclerView
内部初始化DrinkCategoriesViewMvcImpl.java
:
public class DrinkCategoriesViewMvcImpl implements DrinkCategoriesViewMvc {
private View rootView;
private RecyclerView recyclerView;
private FloatingActionButton shareButton;
private DrinkCategoriesAdapter adapter;
public DrinkCategoriesViewMvcImpl(LayoutInflater inflater, ViewGroup parent) {
initializeViews(inflater, parent);
}
@Override
public View getRootView() {
return rootView;
}
@Override
public void setOnDrinkCategoryClickListener(OnDrinkCategoryClickListener listener) {
}
@Override
public void bindDrinkCategories(List<DrinkCategory> drinkCategories) {
adapter.addDrinkCategories(drinkCategories);
adapter.notifyDataSetChanged();
}
private void initializeViews(LayoutInflater inflater, ViewGroup parent) {
rootView = inflater.inflate(R.layout.activity_main_list, parent, false);
shareButton = rootView.findViewById(R.id.fab_share);
setupRecyclerView();
}
private void setupRecyclerView() {
recyclerView = rootView.findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(rootView.getContext(),LinearLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
adapter = new DrinkCategoriesAdapter(rootView.getContext());
recyclerView.setAdapter(adapter);
}
这是适配器类:
public class DrinkCategoriesAdapter extends RecyclerView.Adapter<DrinkCategoriesAdapter.CategoryItemHolder> {
private Context context;
private List<DrinkCategory> drinkCategories;
public DrinkCategoriesAdapter(Context context) {
this.context = context;
}
public void addDrinkCategories(List<DrinkCategory> drinkCategories) {
this.drinkCategories = drinkCategories;
}
@NonNull
@Override
public CategoryItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(context).inflate(R.layout.activity_main_list_item, parent, false);
return new CategoryItemHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull CategoryItemHolder holder, int position) {
if (drinkCategories != null && drinkCategories.size() > 0) {
holder.bindViews(drinkCategories.get(position));
}
}
@Override
public int getItemCount() {
if (drinkCategories != null) {
return drinkCategories.size();
}
return 0;
}
class CategoryItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView title, subtitle;
private ImageView_3_2 poster;
public CategoryItemHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
title = itemView.findViewById(R.id.textview_title);
subtitle = itemView.findViewById(R.id.textview_subtitle);
poster = itemView.findViewById(R.id.imageview_background);
}
@Override
public void onClick(View v) {
}
public void bindViews(DrinkCategory drinkCategory) {
title.setText(drinkCategory.getTitle());
// TODO: subtitle will be set from sharedpreferences
}
}
}
这里还有布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:background="@color/colorPrimary">
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="16dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:src="@drawable/ic_share_black_24dp"
app:fabSize="normal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>
为什么会这样?(Github 上的项目)
解决方案
您需要检查您的DrinkCategoryFragment类。
由于您不返回要膨胀的视图
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
viewMvc = new DrinkCategoriesViewMvcImpl(inflater, container);
// Not return the view you want to inflate
return super.onCreateView(inflater, container, savedInstanceState);
}
试试这个它对你有用
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
viewMvc = new DrinkCategoriesViewMvcImpl(inflater, container);
return viewMvc.getRootView();
}
推荐阅读
- python - 字节数组转换,python3上需要整数错误
- wordpress - 如何获取多站点网络中特定站点的已注册帖子类型列表?
- spring-boot - spring boot restcontroller 测试返回 null
- angular - ng build 后排除 /assets 文件夹中的文件
- svg - 在 GIMP 中使用 svg 和外部字体
- angular - 将动态 FormGroup 值复制到 Angular [反应式表单] 中的数据类
- gitlab - 自定义 GitLab 通知电子邮件
- angular - 我有这个错误:错误TS2339:类型上不存在属性
- php - 如何将前端图像代理到后端图像?
- r - 在 R 中导入多张发票 (.PDF)。将它们从字符串转换为 tibble