How to set the height of an item row in GridLayout

2019-03-11 05:51发布

Current View is

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);

7条回答
Explosion°爆炸
2楼-- · 2019-03-11 06:20

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.

查看更多
别忘想泡老子
3楼-- · 2019-03-11 06:21

You don't need to set the height of an item. The problem here is that image tries to fill all the space. Just add android:adjustViewBounds="true" to your ImageView and it will not add blank spaces

查看更多
Explosion°爆炸
4楼-- · 2019-03-11 06:30

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);
    }
查看更多
神经病院院长
5楼-- · 2019-03-11 06:30

sometime, getting size of inflate view in adapter return 0 or negative. another approach is get required size from out side the adapter, manipulate it and set it into view. in my case, another problem was size set effectless. so i set the size using layout parameter

here is setting my adapter in activity:

Display display = MainActivity.this.getWindowManager().getDefaultDisplay();
                    Point size = new Point();
                    display.getSize(size);
                    int  y = size.y;
                    y=(int)y/2;
 GridLayoutManager linearLayoutManager = new GridLayoutManager(MainActivity.this,2);
                    recyclerView.setLayoutManager(linearLayoutManager);
                    NewOrderAdapter newOrderAdapter=new NewOrderAdapter(MainActivity.this,arrayListname,arrayListimage,y);
                    recyclerView.setAdapter(newOrderAdapter);

and i set view size like this:

@Override
    public myviewholder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = inflator.inflate(R.layout.new_order_row, viewGroup, false);
        GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) view.getLayoutParams();
        params.height = ysize;
        view.setLayoutParams(params);
        myviewholder holder = new myviewholder(view);
        return holder;
    }

and dont forget to set a height to layout in your layout for initializatiin

查看更多
▲ chillily
6楼-- · 2019-03-11 06:31
v.getLayoutParams().width = parent.getMeasuredWidth() / 2;

v.getLayoutParams().height = parent.getMeasuredWidth() / 2;
查看更多
老娘就宠你
7楼-- · 2019-03-11 06:36

On a recent API Level currently 25 the height from the recycler in onCreateViewHolder is always empty. This snippet is to set the hight after the recycler view's onMeasure is invoked and set the correct height to the inflated list view.

@Override
public DataBindingViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
    // You may inflate your view here.
    parent.post(new Runnable() {

        @Override
        public void run() {
            int height = parent.getMeasuredHeight() / rows;
            View view = holder.getBinding().getRoot();
            view.getLayoutParams().height = height;
        }
    });
    return holder;
}
查看更多
登录 后发表回答