如何创建一个带有固定尺寸的RecyclerView,使图片自适应内容并居中裁剪?

4
这是我的目标: enter image description here 我想要使用RecyclerView将我的网格项显示在3行中。我也有一个固定的图像大小,为120dp x 120dp。但我希望我的网格项填满所有垂直空间,这样在RecyclerView中就没有空白空间了。因此,我使用了以下布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:gravity="center">

    <android.support.v7.widget.CardView
        android:id="@+id/view_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:orientation="vertical"
        app:elevation="10dp">

        <LinearLayout
            android:id="@+id/image_container"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/icon"
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:scaleType="centerCrop"/>

            <ir.per.ttf.PersianTextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:ellipsize="end"
                android:singleLine="true"
                android:textSize="16sp"/>

        </LinearLayout>

    </android.support.v7.widget.CardView>

</LinearLayout

>

但我看到的是这样的,图片没有居中裁剪!

输入图片说明

但当我将图像大小减小到100dpx100dp时,我的RecyclerView就无法填充项目:

输入图片说明

而我无法实现我想要的视图。

这是我使用适配器的方式:

 GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3,
                LinearLayoutManager.VERTICAL, false);
        gridLayoutManager.setReverseLayout(true);
        holder.horizonrtalItemRecyclerView.setLayoutManager(gridLayoutManager);
        RecyclerChambersAdapter recyclerAdapter = new RecyclerChambersAdapter(context, items.get(position),3); //pass context, Items and showing count
        holder.horizonrtalItemRecyclerView.setAdapter(recyclerAdapter);

这是onBind view holder:

 @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        RecyclerChambersItemViewHolder holder = (RecyclerChambersItemViewHolder) viewHolder;
        try {

            holder.setmyClass(myClasses.get(position));
            holder.setTitle(myClasses.get(position).getName());
            holder.setId(myClasses.get(position).getId());
            holder.setItemImageSrc(myClasses.get(position).getPhoto().getUrl().toString());

        } catch (Exception ex) {
            holder.setItemImageSrc("");
            ex.printStackTrace();
        }

    }

请发布您的onBindViewHolderadb shell dumpsys activity top的输出。 - pskink
@pskink 已更新 - Mahdi
dumpsys 输出和 RecyclerChambersItemViewHolder 代码。 - pskink
@pskink 我们如何开始聊天呢?这样我就可以把它们发送给你了。请帮忙检查和审查我的答案。 - Mahdi
1个回答

0

我通过将所有的Grid项设置为width="match_parent"来解决了这个问题:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:gravity="center">

    <android.support.v7.widget.CardView
        android:id="@+id/view_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:orientation="vertical"
        app:elevation="10dp">

        <LinearLayout
            android:id="@+id/image_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/icon"
                android:layout_width="match_parent"
                android:layout_height="120dp"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:transitionName="@string/transition_avatar"
                tools:ignore="ContentDescription"/>

            <ir.per.ttf.PersianTextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:ellipsize="end"
                android:singleLine="true"
                android:textSize="16sp"
                android:transitionName="@string/transition_title"
                app:customTypeface="@string/primary_font"/>

        </LinearLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

现在我实现了我的视图和项目填充了所有的RecyclerView空间,并且所有的图像都被裁剪到中心。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接