问题描述
我在 RecyclerView 中的 CardView 中的 ImageView 有问题。
我使用 Glide 加载我的图像,但是,第一次打开应用程序时,CardView 中的 ImageView 位于 CardView 中>Recyclerview 显示错误的宽度和高度,但是,如果我去其他活动并再次回到 recyclerView,我会看到正确的图像尺寸。
Good size after come from other activty
如您所见,不应看到绿色。
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardViewvegetables"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="@dimen/corner_radius_cardview"
card_view:cardElevation="@dimen/elevation_cardview"
card_view:cardMaxElevation="@dimen/elevation_cardview"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/md_green_900">
<RelativeLayout
android:id="@+id/relativeLayoutvegetable1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/md_black_1000">
<ImageView
android:id="@+id/imagevegetable1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
/>
<me.grantland.widget.AutofitTextView
android:id="@+id/namevegetable1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="@color/whiteGrayvegetable"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textStyle="bold"
android:maxLines="2"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/relativeLayoutvegetable2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imagevegetable2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"/>
<me.grantland.widget.AutofitTextView
android:id="@+id/namevegetable2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="@color/whiteGrayvegetable"
android:textStyle="bold"
android:maxLines="2"/>
</RelativeLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
这里是来自 recyclerview 的代码:
public class vegetableRVA extends RecyclerView.Adapter<vegetableRVA.vegetableViewHolder>{
private Context context;
private List<vegetable> vegetables;
class vegetableViewHolder extends RecyclerView.ViewHolder {
private AutofitTextView vegetableName1;
private AutofitTextView vegetableName2;
private ImageView vegetablePhoto1;
private ImageView vegetablePhoto2;
private RelativeLayout relativeLayoutvegetable1;
private RelativeLayout relativeLayoutvegetable2;
vegetableViewHolder(@NonNull View itemView) {
super(itemView);
vegetableName1 = itemView.findViewById(R.id.namevegetable1);
vegetableName2 = itemView.findViewById(R.id.namevegetable2);
vegetableName1.setTextSize((float)context.getResources()
.getdisplayMetrics().widthPixels/25);
vegetableName1.setGravity(Gravity.CENTER);
vegetableName2.setTextSize((float)context.getResources()
.getdisplayMetrics().widthPixels/25);
vegetableName2.setGravity(Gravity.CENTER);
vegetablePhoto1 = itemView.findViewById(R.id.imagevegetable1);
vegetablePhoto2 = itemView.findViewById(R.id.imagevegetable2);
@Override
public void onBindViewHolder(@NonNull vegetableViewHolder vegetableViewHolder,int position) {
int widthDP = (int) context.getResources().getdisplayMetrics().widthPixels / 2;
vegetableViewHolder.vegetableName1.setMaxWidth(widthDP);
vegetableViewHolder.vegetableName1.setSizetoFit();
vegetableViewHolder.vegetableName1.setMaxTextSize((int) widthDP/12);
vegetableViewHolder.vegetableName2.setMaxWidth(widthDP);
vegetableViewHolder.vegetableName2.setSizetoFit();
vegetableViewHolder.vegetableName2.setMaxTextSize((int) widthDP/12);
vegetableViewHolder.vegetablePhoto1.setimageDrawable(null);
vegetableViewHolder.vegetablePhoto2.setimageDrawable(null);
if (context != null) {
Glide.with(context)
.asBitmap()
.load(vegetables.get(position).getimagevegetabledisyuntive().get(0))
.dontAnimate()
.centerCrop()
.override(widthDP+50,heightDP+50 )
.into(vegetableViewHolder.vegetablePhoto1);
Glide.with(context)
.asBitmap()
.load(vegetables.get(position).getimagevegetabledisyuntive().get(1))
.dontAnimate()
.centerCrop()
.override(widthDP+50,heightDP+50)
.into(vegetableViewHolder.vegetablePhoto2);
}
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(widthDP,heightDP);
vegetableViewHolder.vegetablePhoto1.setLayoutParams(parms);
vegetableViewHolder.vegetablePhoto2.setLayoutParams(parms);
vegetableViewHolder.vegetablePhoto1.setBackgroundColor(Color.RED);
}
我不知道为什么我第一次打开de app时CardView中Image的大小有问题,但是打开其他活动后,大小就可以了。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)