user8434375
user8434375

Reputation:

How to set LayoutManager in RecyclerView on Android

In my application I want show some RecyclerViews.
I want show 3 item in one row, I my application should use 4 recyclerView because each recyclerVews each data other API.

In one of RecyclerView I can show 3 Item in one row, and I should show another 3 recyclerViews above of this RecyclerView.

I want show such as this image : enter link description here

but show me such as below image : enter link description here

I write below code in 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);
                        }

My XML code:

                <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" />

How to show 3 up recyclerview's items align by under recyclerView?

Upvotes: 1

Views: 253

Answers (1)

akshay_shahane
akshay_shahane

Reputation: 4643

this means 3 columns

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

and set item's width to match parent that is custom layout you inflate via adapter

Upvotes: 1

Related Questions