android - Recycleview 自定义布局约束正在改变每个膨胀的视图导致问题
问题描述
我有一个 recyclerview 夸大从 json 获取的数据。
我面临的问题是设计问题 - 有时文本正确对齐并显示图像,有时文本太长,导致我认为无法显示图像。
这是我的 XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.support.constraint.ConstraintLayout
android:layout_width="395dp"
android:layout_height="170dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:background="#335634"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/heroImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:src="@mipmap/ic_launcher"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/heroTitle"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:autoSizeMaxTextSize="25dp"
android:autoSizeMinTextSize="15dp"
android:text="Hero Title"
android:textSize="25dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/heroDescription"
android:layout_width="200dp"
android:breakStrategy="simple"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:autoSizeMaxTextSize="25dp"
android:autoSizeMinTextSize="15dp"
android:text="TextView"
android:textAlignment="center"
android:textSize="18dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/heroImage"
app:layout_constraintTop_toBottomOf="@+id/heroTitle" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
这是我的适配器:
public class HeroesAdapter extends RecyclerView.Adapter<HeroesAdapter.HeroesViewHolder> {
private List<Hero> heroList;
public HeroesAdapter(List<Hero> heroList) {
this.heroList = heroList;
}
@NonNull
@Override
public HeroesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View rowView = LayoutInflater.from(parent.getContext()).inflate(R.layout.hero_cell, parent, false);
return new HeroesViewHolder(rowView);
}
@Override
public void onBindViewHolder(@NonNull HeroesViewHolder holder, int position) {
Hero currentHero = heroList.get(position);
String str = String.join(",", currentHero.abilities);
holder.heroTitle.setText(currentHero.title);
holder.heroAbilties.setText(str);
Picasso.get().load(currentHero.image).resize(500,500).into(holder.heroesImage);
}
@Override
public int getItemCount() {
return heroList.size();
}
public static class HeroesViewHolder extends RecyclerView.ViewHolder {
ImageView heroesImage;
TextView heroTitle;
TextView heroAbilties;
public HeroesViewHolder(@NonNull View itemView) {
super(itemView);
heroesImage = itemView.findViewById(R.id.heroImage);
heroTitle = itemView.findViewById(R.id.heroTitle);
heroAbilties = itemView.findViewById(R.id.heroDescription);
}
}
}
我面临的问题是:
使每一行都有一个干净的设计,并且对所有人都一样。
有时显示图像,有时不显示。
底部视图是“一半在屏幕内”和一半在屏幕外。
解决方案
通过使用 LinearLayout,你可以设计你的 hero_cell 来给你想要的输出。就像我现在做的那样。
<?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:orientation="horizontal"
android:padding="10dp"
android:background="#335634"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/heroImage"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:src="@drawable/logo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:background="#335634"
android:layout_marginLeft="20dp"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/heroTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoSizeMaxTextSize="25dp"
android:autoSizeMinTextSize="15dp"
android:text="Hero Title"
android:textSize="25dp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/heroDescription"
android:layout_width="match_parent"
android:breakStrategy="simple"
android:layout_height="wrap_content"
android:autoSizeMaxTextSize="25dp"
android:autoSizeMinTextSize="15dp"
android:text="TextView"
android:textSize="18dp" />
</LinearLayout>
推荐阅读
- c++ - 在不使用额外变量的情况下将不同的值输入到集合中(C++)
- python - Auto height paper Reportlab Python(关于打印购物收据的色带纸案例研究)
- c# - 在 ASP.NET Core 中仅使用输入按钮将参数传递给 Action 的发布表单
- arduino - Arduino IDE 1.8.10 串口监视器没有速度设置
- c++ - 如何在 C++ 上添加 SOCP 约束而不发生内存泄漏?
- ruby - Zed A.Shaw ex48
- python - 脚本化的 ssh 命令偶尔会无限期挂起,服务器处于活动状态并接受连接
- flutter - 在 Flutter 中使用 ListView 小部件时出现布局错误
- python - 如何使用 mechanize (python) 在 global_form ListControl 元素中进行选择?
- python - 如何从两个数据框返回更大的数据框