这是我理想的目标:
我想用recyclerview以3行显示我的网格项.我在dp 120dpx120dp中也有固定的图像大小.但我希望我的网格项目填充所有垂直空间,以便在recyclerView和中没有空格.所以我对物品使用了这种布局:
我想用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
>
但是我看到的是这样的,图像不是裁剪中心!
但是通过将图像大小减小到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视图持有者:
@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空间和所有图像裁剪中心.