У меня есть 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>
Согласно вашим ожиданиям, вы должны использовать карточный вид, и я обновил макет. пожалуйста, проверьте
<?xml version = "1.0" encoding = "utf-8"?>
<android.support.v7.widget.CardView
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_margin = "10dp"
app:cardCornerRadius = "10dp"
android:elevation = "10dp"
android:layout_height = "wrap_content">
<android.support.constraint.ConstraintLayout
android:layout_width = "395dp"
android:layout_height = "170dp"
android:background = "#fff"
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_marginStart = "16dp"
android:layout_marginTop = "20dp"
android:layout_marginEnd = "16dp"
android:autoSizeMaxTextSize = "25dp"
android:autoSizeMinTextSize = "15dp"
android:text = "Hero Title"
android:textColor = "#000"
android:textSize = "25dp"
android:textStyle = "bold"
app:layout_constraintBottom_toTopOf = "@+id/heroDescription"
app:layout_constraintEnd_toEndOf = "parent"
app:layout_constraintStart_toEndOf = "@+id/heroImage"
app:layout_constraintTop_toTopOf = "parent" />
<TextView
android:id = "@+id/heroDescription"
android:layout_width = "200dp"
android:layout_height = "wrap_content"
android:layout_marginStart = "16dp"
android:layout_marginTop = "16dp"
android:layout_marginEnd = "16dp"
android:autoSizeMaxTextSize = "25dp"
android:autoSizeMinTextSize = "15dp"
android:breakStrategy = "simple"
android:text = "TextView"
android:textColor = "#444"
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.v7.widget.CardView>
теперь мне осталось исправить только одну проблему - некоторые изображения появляются, а другие нет, но другие, которые не появляются, оставляют пустое место, что заставляет меня подозревать, что это связано с макетом, а не с кодом Связанный. я прав?
Затем вы можете добавить изображение по умолчанию, чтобы пользователь мог понять, что для этого элемента нет изображения.
добавьте проверку в вашем адаптере, если есть изображение или нет. if (currentHero.image!=null && currentHero.image! = "") Picasso.get().load(currentHero.image).resize(500,500).into(holder.heroesImage);
Вы также можете установить ширину и высоту для просмотра изображения
Я нашел кое-что об окончании URL. похоже, что piccaso не может обрабатывать окончания .png и не показывает их - вы знаете что-нибудь об этом?
Давайте продолжить обсуждение в чате.
Я не сталкивался с какими-либо проблемами с Picasso с URL-адресом изображения png. Например, см. это. Picasso.get() .load("https://futurestud.io/images/books/picasso.png") .placeholder(R.drawable.placeholder) .error(R.drawable.error) .into(binding.picassoImage );
Пожалуйста, используйте последнюю версию этой библиотеки. см. этот github.com/square/пикассо
Почему ConstraintLayout внутри другого ConstraintLayout? ConstraintLayout должен быть 375dp или должен рассчитывать ширину родителя?