【问题标题】:How to set LayoutManager for RecyclerView in Android如何在 Android 中为 RecyclerView 设置 LayoutManager
【发布时间】:2017-08-08 09:21:27
【问题描述】:

在我的应用程序中,我想显示一些 RecyclerViews
我想在一行中显示 3 个项目,我的应用程序应该使用 4 recyclerView,因为每个 recyclerVews 相互数据API

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

请看这张图片来理解我的意思:

我用 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下方显示3个recyclerview's项目对齐

【问题讨论】:

    标签: android android-recyclerview layout-manager


    【解决方案1】:

    使用 Horizontal RecyclerView 而不是 Grid 并且使用 starsModel.subList(0,3); 仅传递 3 项

    castAdapter = new CastAdapter(context, starsModel.subList(0,3));
    LinearLayoutManager layoutManager= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    infoEpisodeFrag_DirectorRecyclerView.setLayoutManager(layoutManager);
    

    【讨论】:

    • 我要显示 Director 并编写 recycperView,例如 Cast recyclerView。使用你的代码?
    • 你是什么意思?我没听懂你
    • 对于导演和作家,我初始化了另一个 RecyclerView。我想通过 Cast RecyclerView 显示这个 RecyclerView 对齐
    • 你能把你的Skype ID发给我吗?
    • 您可以在我的个人资料中找到我的 fb 帐户,最好在这里交谈,以便其他人可以帮助并找到解决问题的方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多