代码之家  ›  专栏  ›  技术社区  ›  Themelis

未调用Android的RecyclerView适配器方法

  •  0
  • Themelis  · 技术社区  · 8 年前

    当我(用googlemail)登录时 LoginFragment DrinkCategoryFragment 包含一个 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>
    

    为什么会这样?( project on Github

    1 回复  |  直到 8 年前
        1
  •  2
  •   Sunny    8 年前

    你需要检查一下你的身体

    因为您不返回要充气的视图

     @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();
        }
    
    推荐文章