My Recycler Item which inflate in onCreateViewHolder
<?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="match_parent" android:gravity="center" android:orientation="vertical" android:padding="16dp"> <ImageView android:id="@+id/gridListImageView" android:layout_width="96dp" android:layout_height="96dp" android:src="@drawable/a" /> <TextView android:id="@+id/gridListView_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout>
I want to display something like this Which has one row of half the height of recycler View? And add padding to the rest of the space? Can i do this by GridLayoutManager?
And this is my GridLayoutManager
GridLayoutManager glm = new GridLayoutManager(getActivity(), 2); recyclerView.setLayoutManager(glm);
solution: In the item layout xml, use RelativeLayout as root only and ensure the layout whose child view be dynamically added is surrounded with RelativeLayout too.
RecyclerView is the ViewGroup that contains the views corresponding to your data. It's a view itself, so you add RecyclerView into your layout the way you would add any other UI element. Each individual element in the list is defined by a view holder object.
The RecyclerView is a ViewGroup that renders any adapter-based view in a similar way. It is supposed to be the successor of ListView and GridView. One of the reasons is that RecyclerView has a more extensible framework, especially since it provides the ability to implement both horizontal and vertical layouts.
A RecyclerView is an advanced version of ListView with improved performance. When you have a long list of items to show you can use RecyclerView. It has the ability to reuse its views. In RecyclerView when the View goes out of the screen or not visible to the user it won't destroy it, it will reuse these views.
When inflating layout for your views in adapter, you can set their height programmatically. In order to evaluate proper height to use you can rely on parent ViewGroup (that is the RecyclerView itself). Here it is a sample:
@Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = mLayoutInflater.inflate(R.layout.view_item, parent, false); // work here if you need to control height of your items // keep in mind that parent is RecyclerView in this case int height = parent.getMeasuredHeight() / 4; itemView.setMinimumHeight(height); return new ItemViewHolder(itemView); }
Hope this could help.
As of support library 25.1.0, ABOVE ANSWER DOESN'T WORK. I suggest below modifications:
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false); GridLayoutManager.LayoutParams lp = (GridLayoutManager.LayoutParams) v.getLayoutParams(); lp.height = parent.getMeasuredHeight() / 4; v.setLayoutParams(lp); return new ViewHolder(v); }
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