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

Kun*_*nal 25 android gridlayoutmanager android-recyclerview

目前的观点是

我的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>
Run Code Online (Sandbox Code Playgroud)

我想显示这样的东西, 哪一行有一半高度的回收者View?并在其余空间添加填充?我可以通过GridLayoutManager来做到这一点吗?

这是我的GridLayoutManager

        GridLayoutManager glm = new GridLayoutManager(getActivity(), 2);
        recyclerView.setLayoutManager(glm);
Run Code Online (Sandbox Code Playgroud)

the*_*les 62

在适配器中为视图填充布局时,可以以编程方式设置其高度.为了评估使用的正确高度,您可以依赖父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);        
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.

  • 似乎从版本recyclelerview-v7:25.1.1现在不支持或行为不端.通过调用```parent.getMeasuredHeight()```返回值始终为零. (3认同)

abe*_*far 15

从支持库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);
    }
Run Code Online (Sandbox Code Playgroud)


Val*_*ova 9

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


Pra*_*ami 8

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

v.getLayoutParams().height = parent.getMeasuredWidth() / 2;
Run Code Online (Sandbox Code Playgroud)

  • 以上都不对我有用,但是这个对我有用。告诉您您还需要什么其他信息?您要我在此处发布全部功能吗? (2认同)

a2e*_*2en 6

科特林版本

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)
}
Run Code Online (Sandbox Code Playgroud)

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