In Recycler View, converting from a LinearLayoutManger to GridLayoutManager makes no difference - android

I have an Android app using a recyclerView and a LinearLayoutManager. I was under the impression that changing the LinearLayoutManager to GridLayoutManager (see the commented line below and the following line) will simply give me a grid. But nothing changes and I still see the linear list, one item per row, and one column.
recyclerView = view.findViewById(R.id.item_setup_recycler_view);
recyclerView.setHasFixedSize(true);
//recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
adapter = new ItemSetupRecyclerViewAdapter(getActivity(), kids);
recyclerView.setAdapter(adapter);
Can someone please let me know if there are other areas I need to change before I see the grid?
Here is the xml for the recyclerView:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/feed_recycle_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:clipToPadding="false"
android:paddingBottom="74dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView8"
app:layout_constraintVertical_bias="0.0">
</androidx.recyclerview.widget.RecyclerView>
Thanks in advance

Use this like this.
In your .xml
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
In your code.
GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),3);
gridLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
then give this to your recyclerView.
Or you can do with one line:
recyclerView.setLayoutManager( new GridLayoutManager(getApplicationContext(),3,LinearLayoutManager.HORIZONTAL,false);)

you need to add orientation and direction of the list
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2, RecyclerView.VERTICAL, false));

Thanks to the comment from #Công Hải I realized the change I was doing was the only change needed. The issue was I made the change on the wrong recycleView in my app. Once I modified the right recycleView everything worked as expected.

Related

Immediately snap Recyclerview

My app uses a RecyclerView with a LinearLayoutManager and a LinearSnapHelper.
At the start of my app I want my RecyclerView to immediately scroll to a snap position without any animation.
I know of RecyclerView.smoothScrollToPosition() but that animates the scroll. Any ideas how to achieve this? Thanks! :)
Code
Set up the RecyclerView:
mRecyclerRooms = (RecyclerView) mView.findViewById(R.id.recyclerRooms);
mRoomsLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
mRecyclerRooms.setLayoutManager(mRoomsLayoutManager);
mSnapHelperRooms = new LinearSnapHelper();
mSnapHelperRooms.attachToRecyclerView(mRecyclerRooms);
mRecyclerRooms.addOnScrollListener(mRoomScrollListener);
XML for RecyclerView:
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerRooms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:clipToPadding="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
mRoomScrollListener() is used to calculate the distance to snap via float distanceFromSnap = abs(mSnapHelperRooms.calculateDistanceToFinalSnap(mRecyclerRooms.getLayoutManager(), view)[0]); and then scale the item accordingly. The idea is to make items get bigger and have more alpha when they are near the snap position which should be the center of the RecyclerView.
Use this
recyclerViewList.scrollToPosition(position);
http://www.devexchanges.info/2016/09/android-tip-recyclerview-snapping-with.html?m=1
This might help. But I am worried that Linear snap helper is enough for snapping at center.

RecyclerView inside HorizontalScrollView Android

I am using a Recycler view inside a Horizontal Scroll View to display threaded comments like below:
Comment1
Comment1Child1
Comment1Child2
Comment1Child2Child1
Comment1Child3 Comment2
etc.
I have done this with the following XML:
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbarSize="2dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/commentRV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginLeft="47dp"
android:minWidth="200dp" />
</HorizontalScrollView>
You'll see I set a min width of 200dp so that the comments never get too small and the deeper down the thread they go the further off screen it will be pushed.
It is displaying perfectly but I am unable to scroll horizontally. I can see the scroll bars but I cannot scroll horizontally.
I have fiddled with disabling Touch and nestedScrollingEnabled in the RV but not sure what the real solution is. Nothing seems to work?
Any help would be much appreciated! Thank you
Remove HorizontalScrollView you can give LinearLayoutManager with horizontal orientation like this
LinearLayoutManager layoutManager= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
reclyclerView.setLayoutManager(layoutManager);
//recyclerView.setMinimumWidth(200); optional for width if u need

RecyclerView in NestedScrollView with horizontal-scolling

I want to result like this:
which can do horizontal-scrolling.
But in fact, like this below:
Which can't display all item and not horizontal-layout.
who can help me ? thanks. other way can reach my wanted result here?
You need to add Layout manager to RecylerView.
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
recylcerView.setLayoutManager(layoutManager);
This will make the RecylerView horizontal.
To set recyclerView as horizontal scrollView you can use LinearLayoutManager. by default it sets vertical scroll behaviour.
LinearLayoutManager layout = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);//0 Horizontal , 1 vertical
recyclerView.setLayoutManager(layout);
in layout.xml
<android.support.v7.widget.RecyclerView
android:id="#+id/yourRecyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/layout_past_round_header"
android:layout_centerInParent="true"
android:overScrollMode="never" />
in your NestedScrollView add this
android:fillViewport="true"

How to add listView inside cardView android?

Forgive me for any mistakes. I'm a beginner.
Can anyone please explain how to create a listview inside a cardview layout in android. Example settings app in android 6.0
I wanna create a scrollable cardview layout with listview items in each cardview layout.
I have searched enough online and nothing seems to help me.
If you have any solution this it would be helpful for me.
The best way to do this is using a RecyclerView with a vertical LinearLayoutManager (which will look the the same as a ListView but with better performance) and a fixed size inside your CardView. The xml for your CardView will look something like this:
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v7.widget.CardView>
and then programmatically set fixed size on your RecyclerView to true, set the LayoutManager and create a custom RecyclerView.Adapter to fill the RecyclerView's rows:
RecyclerView recyclerView = parentView.findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
MyCustomAdapter adapter = new MyCustomAdapter(context, dataSet);
recyclerView.setAdapter(adapter);
The cardView is a backgroud of the listView. So the item of this will like:
enter image description here
Hope to help you!

How to add recycler view or card view in fragments?

I have created the recyclerView and CardView in Android. Now i want to add it to one of three fragments that will be displayed on clicking the respective tabs from the navigation drawer.So how to add it in fragment.
Can anybody help me out to do so. Thank you very much!
First Add support v7 library in your project then in xml file put this.
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
Then in your fragment put these lines.
scheduler_day_view_horizontal_recyclerview = (RecyclerView) v.findViewById(R.id.my_recycler_view);
layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
scheduler_day_view_horizontal_recyclerview.setLayoutManager(layoutManager);
scheduler_day_view_horizontal_recyclerview.setItemAnimator(new DefaultItemAnimator());
scheduler_day_view_horizontal_recyclerview.setHasFixedSize(true);
Then set adapter to the RecyclerView.
For more detail about cardview and Recycler view Please visit here

Categories

Resources