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

0aydgbwb  于 2023-06-04  发布在  Android
关注(0)|答案(9)|浏览(239)

在onCreateViewHolder中膨胀的My Recycler Item

<?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查看?并添加填充到其余的空间?我可以使用GridLayoutManager吗?
这是我的GridLayoutManager

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

jljoyd4f1#

当在adapter中扩展视图的布局时,您可以通过编程方式设置它们的高度。为了评估要使用的适当高度,您可以依赖父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);        
}

希望这能帮上忙。

k2fxgqgv

k2fxgqgv2#

自支持库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);
    }
ve7v8dk2

ve7v8dk23#

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

afdcj2ne

afdcj2ne4#

onCreateViewHolder内部,只需

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

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

vxbzzdmp5#

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

h43kikqp

h43kikqp6#

有时,在适配器中获取充气视图大小返回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;
    }

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

5tmbdcev

5tmbdcev7#

在最近的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;
}
hrirmatl

hrirmatl8#

这是我的代码,用于根据实际的纵横比调整回收视图元素的高度。

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

ne5o7dgx9#

我有一个类似的要求,需要一个动态的高度为每一行在网格中的能力,以添加和删除项目的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可以是您需要提供的像素高度。

相关问题