android – 如何创建一个包含内容和图像的固定大小图像的RecyclerView以裁剪为中心

这是我理想的目标:

enter image description here


我想用recyclerview以3行显示我的网格项.我在dp 120dpx120dp中也有固定的图像大小.但我希望我的网格项目填充所有垂直空间,以便在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

>

但是我看到的是这样的,图像不是裁剪中心!

enter image description here

但是通过将图像大小减小到100dpx100dp,我的RecyclerView不会填充项目:

enter image description here

我无法实现我想要的观点.

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

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视图持有者:

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

    }

解决方法

我通过set all width =“match_parent”更改Grid项目来解决这个问题:

<?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空间和所有图像裁剪中心.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...