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
Related
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.
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.
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>
I need both horizontal scrolling and vertical scrolling. how can be it possible using recycler views, or should i use 2 way views? Any one help me please.
How will set the adapter for the same?
you can add recycler view to your layout file
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/recycler_view"
xmlns:android="http://schemas.android.com/apk/res/android" />
In java file set its orientation to horizontal or vertical
LinearLayoutManager LayoutManager = new LinearLayoutManager(this);
LayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);// or LinearLayoutManager.VERTICAL
RecyclerView.setLayoutManager(LayoutManager);
You have to use two times same adapter one for HORIZONTAL and second for VERTICAL.
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/recycler_view"
xmlns:android="http://schemas.android.com/apk/res/android" />
Use above in XML file two times one for HORIZONTAL and one for VERTICAL
This for HORIZONTAL
LinearLayoutManager LayoutManager = new LinearLayoutManager(this);
LayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
RecyclerView.setLayoutManager(LayoutManager);
OrderedDetailsAdapter orderedDetailsAdapter;
orderedDetailsAdapter = new OrderedDetailsAdapter(OrderDetailsActivity.this, orderDetailsPojo.getOrderItemsList());mOrderDetailsRecyclerView.setAdapter(orderedDetailsAdapter);
This for VERTICAL
LinearLayoutManager LayoutManager = new LinearLayoutManager(this);
LayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
RecyclerView.setLayoutManager(LayoutManager);
OrderedDetailsAdapter orderedDetailsAdapter;
orderedDetailsAdapter = new OrderedDetailsAdapter(OrderDetailsActivity.this, orderDetailsPojo.getOrderItemsList());mOrderDetailsRecyclerView.setAdapter(orderedDetailsAdapter);
Just add your Vertical-LinearLayout recyclerView inside HorizontalScrollView :
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/nested"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</HorizontalScrollView>
For vertical recyclerview add these lines:
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);