如何在Android上的RecyclerView中设置LayoutManager

时间:2017-08-08 18:39:35

标签: android android-recyclerview

在我的申请表中,我希望展示一些RecyclerViews 我希望连续显示3项,我的应用应该使用 4 recyclerView,因为每个recyclerVews每个数据其他 API

RecyclerView其中一个中,我可以在一行中显示3个项目,并且我应该在此recyclerViews上方显示另外3个RecyclerView

我想要显示此图片:enter link description here

但请告诉我如下图片:enter link description here

我在Java中编写以下代码:

if (movieResponse.getData().getStars().size() > 0) {
                            starsModel.clear();
                            starsModel.addAll(movieResponse.getData().getStars());
                            castAdapter = new CastAdapter(context, starsModel);
                            infoEpisodeFrag_CastRecyclerView.setLayoutManager(new GridLayoutManager(context, 3));
                            infoEpisodeFrag_CastRecyclerView.setHasFixedSize(true);
                            infoEpisodeFrag_CastRecyclerView.setNestedScrollingEnabled(false);
                            infoEpisodeFrag_CastRecyclerView.setAdapter(castAdapter);
                        } else {
                            goneView(infoEpisodeFrag_CastRecyclerView);
                        }
                        //Get Directors
                        if (movieResponse.getData().getDirectors().size() > 0) {
                            directorsModel.clear();
                            directorsModel.add(movieResponse.getData().getDirectors().get(0));
                            directorAdapter = new DirectorAdapter(context, directorsModel);
                            infoEpisodeFrag_DirectorRecyclerView.setLayoutManager(new GridLayoutManager(context, 1));
                            infoEpisodeFrag_DirectorRecyclerView.setHasFixedSize(true);
                            infoEpisodeFrag_DirectorRecyclerView.setNestedScrollingEnabled(false);
                            infoEpisodeFrag_DirectorRecyclerView.setAdapter(directorAdapter);
                        } else {
                            goneView(infoEpisodeFrag_DirectorRecyclerView);
                        }
                        //Get Writers
                        if (movieResponse.getData().getWriters().size() > 0) {
                            writersModel.clear();
                            writersModel.add(movieResponse.getData().getWriters().get(0));
                            writerAdapter = new WriterAdapter(context, writersModel);
                            infoEpisodeFrag_WriterRecyclerView.setLayoutManager(new GridLayoutManager(context, 1));
                            infoEpisodeFrag_WriterRecyclerView.setHasFixedSize(true);
                            infoEpisodeFrag_WriterRecyclerView.setNestedScrollingEnabled(false);
                            infoEpisodeFrag_WriterRecyclerView.setAdapter(writerAdapter);
                        } else {
                            invisibleView(infoEpisodeFrag_WriterRecyclerView);
                        }
                        //Get Writers2
                        if (movieResponse.getData().getWriters().size() > 0) {
                            writersModel.clear();
                            writersModel.add(movieResponse.getData().getWriters().get(0));
                            writerAdapter = new WriterAdapter(context, writersModel);
                            infoEpisodeFrag_WriterRecyclerView2.setLayoutManager(new GridLayoutManager(context, 1));
                            infoEpisodeFrag_WriterRecyclerView2.setHasFixedSize(true);
                            infoEpisodeFrag_WriterRecyclerView2.setNestedScrollingEnabled(false);
                            infoEpisodeFrag_WriterRecyclerView2.setAdapter(writerAdapter);
                        }

我的XML代码:

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/infoEpisodeFrag_DirectorRecyclerView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/infoEpisodeFrag_CastLay"
                    android:layout_marginBottom="@dimen/padding5" />

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/infoEpisodeFrag_WriterRecyclerView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/infoEpisodeFrag_CastLay"
                    android:layout_marginBottom="@dimen/padding5"
                    android:layout_toRightOf="@+id/infoEpisodeFrag_DirectorRecyclerView" />

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/infoEpisodeFrag_WriterRecyclerView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/infoEpisodeFrag_CastLay"
                    android:layout_marginBottom="@dimen/padding5"
                    android:layout_toRightOf="@+id/infoEpisodeFrag_WriterRecyclerView"
                    android:visibility="visible" />

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/infoEpisodeFrag_CastRecyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/infoEpisodeFrag_WriterRecyclerView"
                    android:layout_marginBottom="@dimen/padding5" />

如何在recyclerview's下显示3个recyclerView对齐

1 个答案:

答案 0 :(得分:1)

  

这意味着3列

                            infoEpisodeFrag_DirectorRecyclerView.setLayoutManager(new GridLayoutManager(context, 3));

  

并设置项目的宽度以匹配通过适配器

膨胀的自定义布局的父级