I'm trying to make a horizontal recyclerview in my Android application so I see a of tutorials to make it, its very complicated because I don't want to add images or on click listeners I just have a card and textView inside it and I want to add an id to every single item I have some background about that below
This is my activity of recyclerview items:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="43dp"
app:cardBackgroundColor="#000"
app:cardCornerRadius="20dp"
app:cardElevation="10dp">
<TextView
android:id="#+id/horizontal_data_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="Test"
android:textColor="#38ef7d"
android:textSize="20sp"
android:textStyle="bold"
android:paddingStart="18dp"
android:paddingEnd="18dp"
tools:ignore="HardcodedText"/>
</androidx.cardview.widget.CardView>
</RelativeLayout>
And I created a class named RecyclerViewAdapter.
I will add this RecyclerView to a named activity called SettingsActivity.java
Any ideas?
Assuming you use LinearLayoutManager in your RecyclerView, then you can pass true as to third argument in the LinearLayoutManager constructor.
For example:
mRecyclerView.setLayoutManager(
LinearLayoutManager(
this,
LinearLayoutManager.HORIZONTAL,
true
)
)
change RelativeLayout width and height
android:layout_width="wrap_content"
android:layout_height="wrap_content"
and Remove this line android:orientation="vertical"
and
mRecyclerView.setLayoutManager(
LinearLayoutManager(
this,
LinearLayoutManager.HORIZONTAL,
true
)
)
For horizontal scrolling in recycler view set the layout manager for recyclerview as:
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));
Hope this should help.
Related
I have two RecyclerView, both have a vertical orientation, I need to scroll one of them so that the second scrolls, that is, their scrolling is synchronous, I thought that it is possible to apply one LinearLayoutManager to these two RecyclerView and then it will be work, but in this log, the error LinearLayoutManager is already attached to a RecyclerView will be generated, so I don't know how to be, help me find a solution, I need two independent RecyclerView with different adapters, but which scroll synchronously, so do not write about GridLayoutManager, thanks.
xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:clipToPadding="false"
android:orientation="vertical"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:clipToPadding="false"
android:orientation="vertical"/>
</LinearLayout>
Cod
val RLM_0 = LinearLayoutManager(context)
rv_0.setHasFixedSize(false)
rv_0.isNestedScrollingEnabled = false
rv_0.layoutManager = RLM_0
adapter_0 = Adapter_0(itemTasks, requireActivity())
rv_0.adapter = adapter_0
val RLM_1 = LinearLayoutManager(context)
rv_1.setHasFixedSize(false)
rv_1.isNestedScrollingEnabled = false
rv_1.layoutManager = RLM_1
adapter_1 = Adapter_1(itemTasks, requireActivity())
rv_1.adapter = adapter_1
Solution 1:
Check is: Sync scrolling of multiple RecyclerViews
Solution 2:
There is an other way to do so, the idea is to disable scroll for each recycler view and surround them with a scroll view.
This is how you can implement it:
binding.recyclerViewOne.setOnTouchListener((v, event) -> true);
binding.recyclerViewTwo.setOnTouchListener((v, event) -> true);
And for the layout:
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</LinearLayout>
</ScrollView>
I have a RecyclerView and inside I have a RecyclerView. and child RecyclerView is no scrolling I try put a child RecyclerView inside a scrollView NeastedCrollView but it doesn't work :
No I have this parent recyclerView:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/vehicle_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="#dimen/text_dp_20"
android:paddingTop="10dp"
android:scrollbars="none"
tools:ignore="MissingConstraints" />
and this is a child list :
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom|center_horizontal"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="100dp"
android:scrollbars="none" />
</androidx.core.widget.NestedScrollView>
First of all, don't use NestedScrollView, RecyclerView handles everything about scrolling!
use the below links guidelines for improving and optimizing your RecyclerViews, here some useful references:
https://www.geeksforgeeks.org/how-to-create-a-nested-recyclerview-in-android/
https://android.jlelse.eu/easily-adding-nested-recycler-view-in-android-a7e9f7f04047
follow one of them step by step!
you're first need in one recylerView in main activity
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
2-and in MainActivity.java :
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
ItemAdapter itemAdapter = new ItemAdapter(buildItemList());
rvItem.setAdapter(itemAdapter);
rvItem.setLayoutManager(layoutManager);
3- recyclerView in Layout Adapter:
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_sub_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
4- in Adapter :
LinearLayoutManager layoutManager = new LinearLayoutManager(
itemViewHolder.rvSubItem.getContext(),
LinearLayoutManager.VERTICAL,
false
);
layoutManager.setInitialPrefetchItemCount(item.getSubItemList().size());
// Create sub item view adapter
SubItemAdapter subItemAdapter = new SubItemAdapter(item.getSubItemList());
itemViewHolder.rvSubItem.setLayoutManager(layoutManager);
itemViewHolder.rvSubItem.setAdapter(subItemAdapter);
itemViewHolder.rvSubItem.setRecycledViewPool(viewPool);
the end.
you see full code in link
I want to use the horizontal scroll recycler.
For this, I use LinearLayoutManager (LinearLayoutManager.HORIZONTAL), this works, but different indentation is obtained for the items.
Screenshot - https://i.stack.imgur.com/Mqp8A.png
Layout for fragment:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/list_related"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingTop="20dp"
android:paddingBottom="20dp"
tools:listitem="#layout/item_2_lenta"/>
</LinearLayout>
Layout for item:
<com.google.android.material.card.MaterialCardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="4dp"
app:cardBackgroundColor="#ff0">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
UPD
Creating a layoutManager
vRelatedStub.setOnInflateListener((viewStub, sView) -> {
vLabelRelated = sView.findViewById(R.id.label_related);
vListRelated = sView.findViewById(R.id.list_related);
vLoaderRelated = createLoaderView(sView, mAdapterRelated);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(),
LinearLayoutManager.HORIZONTAL, false);
vListRelated.setLayoutManager(layoutManager);
vListRelated.setAdapter(mAdapterRelated);
});
vRelatedStub.inflate();
Found an error in my code.
The thing was that I asked margin to the first item in the bind holdar,
this remained from the vertical linear manager.
my friends
I am a new in android and I wanted to do an example (code) like the picture
GridView - Images,Text and ItemClick
Thanks to all
You are looking for RecyclerView. Create a layout and add recyclerView to it then create another layout file for the rows of the recyclerview as below:
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Create another layout row_layout.xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"/>
</RelativeLayout>
Then in your MainActivity, add following lines of code:
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager gridLayoutManager = new GridLayoutManager(this, 2, false);
recyclerView.setLayoutManager(gridLayoutManager);
After this, create an Adapter according to your specific need for the recyclerView which can be found with a quick google search. Just remmeber to infalte your row_layout in the adapter's onCreateViewHolder. After creating the adapter just set it to your recyclerView.
recyclerView.setAdapter(your_custom_adapter);
This is just a gist of what you ought to do. It'll get you started in the right direction. The rest is just a search away.
i have a recyclerview as grid view and its horizontal like this
recyclerView_Day = (RecyclerView) root.findViewById(R.id.day_recycler);
GridLayoutManager gridLayoutManager = new GridLayoutManager(context,3);
gridLayoutManager.setSpanCount(3);
RecyclerView.LayoutManager layoutManager = new
GridLayoutManager(context, 1,gridLayoutManager.HORIZONTAL, false);
recyclerView_Day.setLayoutManager(layoutManager);
recyclerView_Day.setAdapter(new DyaAdapter(context))
but its not working , so i need to set a 3 column in recyclerview !
Why don't you use a GridView instead of recyclerview, and set columnCount to 3 in gridview, this will easily do your work.
There are 2 ways in which you can achieve this with RecyclerView
1. Use GridLayoutManager
recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
adapter = new DyaAdapter(context);
recyclerView_Day.setAdapter(adapter);
Another way is to user LinearLayoutManager with orientation as Vertical.
Here you can set an item custom data and inflate a view with 3 same views.
Layout file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/v1"/>
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/v2"/>
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/v3"/>
</LinearLayout>