未调用Android RecyclerView Adapter方法

问题描述 投票:0回答:1

当我登录(使用谷歌邮件)时,LoginFragment正在被包含DrinkCategoryFragmentRecyclerView取代。

这些项目是从Firebase数据库中正确获取的,但布局不存在,我无法理解原因。

RecyclerViewDrinkCategoriesViewMvcImpl.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>

为什么会这样? (project on Github

android android-recyclerview recycler-adapter recyclerview-layout
1个回答
2
投票

您需要检查您的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();
    }
© www.soinside.com 2019 - 2024. All rights reserved.