How to add recycler view or card view in fragments? - android

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

Related

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

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.

Displaying more than 5columns in recycler view with horizontal scroll view

I'm using recycler view with a layout that consists of 4 text views but I want to display more than 5 columns in the layout with horizontal scroll view? Is it feasible and possible? If yes, can anyone share any link or code?
Thank you.
For Horizontal scroll view, set your RecylerView layoutManager as follows,
LinearLayoutManager linearLayoutManager
= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView recyclerView=(RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(linearLayoutManager);
In RecyclerView ,
android:layout_width="match_parent"
android:layout_height="0dp"
In RecyclerView item layout,
android:layout_width="wrap_content"
android:layout_height="wrap_content"

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!

Android SwipeRefreshLayout on RecyclerView with reverse layout

I'm using a RecyclerView inside a SwipeRefreshLayout in Android.
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/mySwipeRefresh"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<RecyclerView
android:id="#+id/myRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
In this RecyclerView i'm using a LinearLayoutManager with reverse layout and stack from end:
LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
myRecyclerView.setLayoutManager(layoutManager);
So, the scrolling content starts from bottom and items are added by adapter from bottom to top.
With this configuration, i'd like to show SwipeRefreshLayout when RecyclerView is at the bottom and user try scrolling more down. Instead, the results is that SwipeRefreshLayout has the standard behavior and is shown from top to bottom animation when user scrolls to top and RecyclerView is at the top.
I didn't find apis to do this, how can i solve this issue?
Thanks
I resolved using this library since SwipeRefreshLayout doesn't support reverse layout
Refresh layout bottom

Categories

Resources