如何在GridLayoutManager中设置项目行的高度

时间:2016-02-05 10:18:30

标签: android android-recyclerview gridlayoutmanager

Current View is

我的Recycler项目在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 哪一个有一排高度的回收者View? 并在其余空间添加填充? 我可以通过GridLayoutManager来做到这一点吗?

这是我的GridLayoutManager

        GridLayoutManager glm = new GridLayoutManager(getActivity(), 2);
        recyclerView.setLayoutManager(glm);

9 个答案:

答案 0 :(得分:54)

在适配器中为视图展开布局时,可以通过编程方式设置其高度。为了评估使用的正确高度,您可以依赖父ViewGroup(即RecyclerView本身)。这是一个样本:

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

希望这可以提供帮助。

答案 1 :(得分:10)

从支持库25.1.0开始,上面的答案已经开始工作了。我建议进行以下修改:

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

答案 2 :(得分:7)

您不需要设置项目的高度。这里的问题是图像试图填补所有空间。只需添加 android:adjustViewBounds="true"到您的ImageView,它不会添加空格

答案 3 :(得分:7)

v.getLayoutParams().width = parent.getMeasuredWidth() / 2;

v.getLayoutParams().height = parent.getMeasuredWidth() / 2;

答案 4 :(得分:3)

有时,在适配器中获取膨胀视图的大小返回0或负数。另一种方法是从适配器外部获取所需大小,操作它并将其设置为视图。在我的情况下,另一个问题是尺寸设置无效。所以我使用布局参数

设置大小

这里是在活动中设置我的适配器:

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

我将视图大小设置为:

@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;
    }

并且不要忘记在布局中为初始化设置布局高度

答案 5 :(得分:2)

在最近的API级别上,当前25是onCreateViewHolder中回收者的高度 总是空的。此片段用于在回收器视图的onMeasure之后设置高度 被调用并将正确的高度设置为膨胀的列表视图。

@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;
}

答案 6 :(得分:2)

这是我根据所需的实际宽高比调整循环视图元素高度的代码。

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.adapter_offers, parent, false);

    int width =  parent.getMeasuredWidth();
    float height = (float) width / Config.ASPECT_RATIO;//(Width/Height)
    RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) itemView.getLayoutParams();
    params.height = Math.round(height);
    itemView.setLayoutParams(params);
    return new MyViewHolder(itemView);
}

答案 7 :(得分:2)

kotlin版本

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val binding = ItemBinding.inflate(
        LayoutInflater.from(parent.context),
        parent,
        false
    )
    binding.root.post {
        binding.root.layoutParams.height = parent.width/3
        binding.root.requestLayout()
    }
    return ViewHolder(binding)
}

这里3是您的GridLayoutManager的跨度计数 。如果您不使用数据绑定,则可以用binding.root替换itemView

答案 8 :(得分:0)

我有一个类似的要求,它需要网格中每一行的动态高度,并且还能够从 RecyclerView 添加和删除项目。在 onCreateViewHolder 中设置高度布局参数的问题是这里很少有答案表明,当从 RecyclerView 添加/删除项目时,视图会被回收并且不会再次调用 onCreateViewHolder而是重新使用池中的视图(如果可用),并且布局管理器将执行一次传递以计算项目的高度/宽度,我们将丢失 onCreateViewHolder 中设置的原始预期高度/宽度.

下面的这种方法可能会对面临类似问题的人有所帮助。

这里的关键步骤是扩展 GridLayoutManager 并覆盖以下内容 -

  1. generateDefaultLayoutParams
  2. generateLayoutParams
  3. checkLayoutParams

布局管理器看起来像这样 -

SpanGridLayoutManager : GridLayoutManager {

constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) :
        super(context, attrs, defStyleAttr, defStyleRes)

constructor(context: Context, spanCount: Int) : super(context, spanCount)
constructor(context: Context, spanCount: Int, orientation: Int, reverseLayout: Boolean) :
        super(context, spanCount, orientation, reverseLayout)

override fun generateDefaultLayoutParams(): RecyclerView.LayoutParams {
    return spanLayoutSize(super.generateDefaultLayoutParams())
}

override fun generateLayoutParams(c: Context, attrs: AttributeSet): RecyclerView.LayoutParams {
    return spanLayoutSize(super.generateLayoutParams(c, attrs))
}

override fun generateLayoutParams(lp: ViewGroup.LayoutParams): RecyclerView.LayoutParams {
    return spanLayoutSize(super.generateLayoutParams(lp))
}

override fun checkLayoutParams(lp: RecyclerView.LayoutParams): Boolean {
    val layoutParams = generateDefaultLayoutParams()
    return super.checkLayoutParams(lp) &&
            layoutParams.width == lp.width &&
            layoutParams.height == lp.height
}

private fun spanLayoutSize(layoutParams: RecyclerView.LayoutParams): RecyclerView.LayoutParams {
    layoutParams.height = if (some_condition) x else y
    return layoutParams
}

此处的 xy 可以是您需要提供的高度(以像素为单位)。