I'm using RecyclerView and I'm trying to make the width item of RecyclerView match_parent since I'm using
LinearLayoutManager.HORIZONTAL
<android.support.v7.widget.RecyclerView
android:id="@+id/listOffersHits"
android:layout_width="match_parent"
android:layout_height="match_parent" />
custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_margin="2dp"
card_view:cardCornerRadius="2dp">
<ImageView
android:id="@+id/offerImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/item_selector"
android:scaleType="fitXY"
android:src="@drawable/offer"
/>
</android.support.v7.widget.CardView>
LayoutManager
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL, false);
listOffersHits.setLayoutManager(linearLayoutManager);
ViewHolder
View view= LayoutInflater.from(parent.getContext())
.inflate(R.layout.custom_layout, parent, false);
ViewOffersHolder viewHolder=new ViewOffersHolder(view);
return viewHolder;
A RecyclerView. ViewHolder class which caches views associated with the default Preference layouts. A ViewHolder describes an item view and metadata about its place within the RecyclerView.
How is this implemented? Typically in a simple RecyclerView, we load elements to the adapter from a Data Structure. In order to show the loading icon view at the bottom of the RecyclerView, we need to first add a NULL element to the end of the Data Structure.
getItemCount() : RecyclerView calls this method to get the size of the data set. For example, in an address book app, this might be the total number of addresses. RecyclerView uses this to determine when there are no more items that can be displayed.
Where do you add the android:onClick attribute to make items in a RecyclerView respond to clicks? In the layout file that displays the RecyclerView, add it to the element. Add it to the layout file for an item in the row.
Calling to GridLayoutManager instead of LinearLayoutManager and put one column is most simple
From your xml:
<androidx.recyclerview.widget.RecyclerView
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="1"
...
Or from Kotlin:
recyclerView.layoutManager = GridLayoutManager(this,1)
Or from Java:
recyclerView.setLayoutManager(new GridLayoutManager (this, 1);
I fixed the problem:
1- Get the screen size (width).
2- make the ViewHolder width same as screen size.
Below my code if any one need it.
WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
int width = windowManager.getDefaultDisplay().getWidth();
int height = windowManager.getDefaultDisplay().getHeight();
view.setLayoutParams(new RecyclerView.LayoutParams(width, RecyclerView.LayoutParams.MATCH_PARENT));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With