Я пытаюсь установить высоту ViewHolder в onBindViewHolder после изменения его содержимого, но он работает только в начале. Когда он перерабатывается, высоты неверны. Я думаю, что он сохраняет высоту предыдущего контента. Почему это не регулирует это снова? Что я делаю не так?
Мой код (упрощенно):
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mMyView.setText(holder.mItem.getValue());
holder.mView.measure(
View.MeasureSpec.makeMeasureSpec(Resources.getSystem().getDisplayMetrics().widthPixels, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
);
int layoutHeight = holder.mView.getMeasuredHeight();
holder.mMyView.getLayoutParams().height = layoutHeight;
//holder.mView.requestLayout();
}
public class ViewHolder extends RecyclerView.ViewHolder {
final View mView;
final TextView mMyView;
ViewHolder(View view) {
super(view);
mView = view;
mMyView= view.findViewById(R.id.myView);
}
}
Редактировать: Мой оригинальный код немного сложнее, чем этот. Мне нужно настроить размер вида, чтобы он соответствовал размеру другого вида. Приведенный выше код упрощен, чтобы его было легче читать.
Редактировать 2: В соответствии с запросом, здесь приведена упрощенная версия моего XML, чтобы понять, почему я хочу отрегулировать высоту. Моя цель - настроить высоту ImageView так, чтобы она соответствовала высоте TextView.
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/imageView"
app:layout_constraintStart_toStartOf="parent"
/>
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Редактировать 3: Я просто установил высоту на фиксированное значение. В любом случае, расчет высоты оказал большое влияние на производительность. Смешно, как трудно сделать макеты отзывчивыми в Android.
Заранее спасибо.