Я использую RecyclerView и PagerSnapHelper, чтобы показать какой-то вопрос, на который пользователь должен ответить. После загрузки данных мне нужно позвонить TextView.setText()
, чтобы изменить заголовок вопроса. Представление элемента RecyclerView имеет свойство android:layout_height="wrap_content"
, но оно не автоматически изменяет высоту после setText()
.
, если текст заголовка должен отображаться в две строки (в этом нет ничего плохого, потому что моему тексту-заполнителю нужны две строки показать):
Выпуск , если текст заголовка должен отображаться в три строки:
Итак, мой вопрос заключается в том, как автоматически изменить высоту элемента после вызова TextView.setText()
в элементе RecyclerView.
item_evaluate_norm_view. xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@drawable/round_bg">
<ImageView
android:id="@+id/normTypeIV"
android:layout_width="7dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="@+id/normTypeTV"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/normTypeTV"
app:srcCompat="@drawable/bg_pure_color_gray" />
<TextView
android:id="@+id/normTypeTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:text="@string/text_common_norm"
android:textColor="@color/colorCommonText"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/line3"
app:layout_constraintStart_toEndOf="@+id/normTypeIV"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="@+id/normTypeTV"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/normTypeTV">
<TextView
android:id="@+id/totalItemCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="30"
android:textColor="@color/colorCommonText"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/curItemPos" />
<TextView
android:id="@+id/curItemPos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textColor="@color/colorBasicBlack"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<View
android:id="@+id/line3"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="8dp"
android:background="@color/colorFirstLvlBorder"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout13" />
<TextView
android:id="@+id/normNameTV"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:text="实验室安全责任人明确且与学院签订了安全责任书"
android:textSize="24sp"
android:textColor="@color/colorPrimaryText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/line3" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/normNameTV">
<RadioButton
android:id="@+id/itemEvaluateNormViewRBPassed"
style="@style/checkbox_style"
android:layout_width="match_parent"
android:layout_height="@dimen/btn_height"
android:text="@string/text_pass"
android:checked="false"
android:textSize="20sp" />
<RadioButton
android:id="@+id/itemEvaluateNormViewRBFailed"
style="@style/checkbox_style"
android:layout_width="match_parent"
android:layout_height="@dimen/btn_height"
android:layout_marginTop="10dp"
android:text="@string/text_failed"
android:checked="false"
android:textSize="20sp" />
</RadioGroup>
<TextView
android:id="@+id/textView28"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="20dp"
android:text="@string/text_text_describe"
android:textSize="@dimen/info_text_size"
android:textColor="@color/colorInfo"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/radioGroup" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout14"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="@drawable/bg_common_border"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView28">
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="top"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:background="@null"
android:singleLine="false"
android:hint="@string/text_text_describe_hint"
android:inputType="textMultiLine"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:scrollHorizontally="false"
android:textColorHint="@color/colorPlaceholderText"
android:textSize="@dimen/common_text_size"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:importantForAutofill="no" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="@+id/textView29"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="@string/text_prove_files"
android:textColor="@color/colorInfo"
android:textSize="@dimen/info_text_size"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout14" />
<com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton
android:id="@+id/lastItemBtn"
android:layout_width="0dp"
android:layout_height="@dimen/btn_height"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="@string/text_last_item"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/nextItemBtn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView29"
app:qmui_radius="@dimen/btn_radius" />
<com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton
android:id="@+id/nextItemBtn"
android:layout_width="0dp"
android:layout_height="@dimen/btn_height"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:layout_marginBottom="8dp"
android:text="@string/text_next_item"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/lastItemBtn"
app:layout_constraintTop_toBottomOf="@+id/textView29"
app:qmui_radius="@dimen/btn_radius" />
</androidx.constraintlayout.widget.ConstraintLayout>
frag_evaluate. xml
<androidx.core.widget.NestedScrollView 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="match_parent"
android:background="@color/qmui_config_color_background">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/evaluateNameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/roomNameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="@+id/evaluateNameTV"
app:layout_constraintTop_toBottomOf="@+id/evaluateNameTV" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/fragmentEvaluateNormRV"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/roomNameTV" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
EvaluateFragment. java
...
private void initRecyclerView() {
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(),
LinearLayoutManager.HORIZONTAL, false);
RecyclerView rv = mBinding.fragmentEvaluateNormRV;
rv.setLayoutManager(layoutManager);
EvaluateNormRVAdapter adapter = new EvaluateNormRVAdapter();
rv.setAdapter(adapter);
EvaluateNormRVAdapter.OnClickListener lastOrNextClick =
position -> layoutManager.smoothScrollToPosition(rv, new RecyclerView.State(), position);
adapter.setNextItemListener(lastOrNextClick);
adapter.setLastItemListener(lastOrNextClick);
PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();
pagerSnapHelper.attachToRecyclerView(rv);
}
...
EvaluateNormRVAdapter. java
public class EvaluateNormRVAdapter extends RecyclerView.Adapter<EvaluateNormRVAdapter.ViewHolder> {
private List<Norm> mItems;
public EvaluateNormRVAdapter() {
mItems = new ArrayList<>();
}
public void addItemBatch(int startPos, List<Norm> items) {
mItems.addAll(items);
notifyItemRangeInserted(startPos, items.size());
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int position) {
ItemEvaluateNormViewBinding binding = ItemEvaluateNormViewBinding.inflate(
LayoutInflater.from(parent.getContext()), parent, false);
ViewHolder holder = new ViewHolder(binding);
Context context = binding.getRoot().getContext();
binding.nextItemBtn.setOnClickListener(v -> {
if (nextItemListener != null && position < getItemCount() - 1) {
nextItemListener.onClick(position + 1);
}
});
binding.lastItemBtn.setOnClickListener(v -> {
if (lastItemListener != null && position > 0) {
lastItemListener.onClick(position - 1);
}
});
binding.curItemPos.setText(String.format(Locale.CHINA,
"%d", position + NumberUtils.INTEGER_ONE));
Norm norm = mItems.get(position);
Integer normType = norm.getNormType();
binding.normNameTV.setText(norm.getName());
if (Norm.NORM_TYPE_NORMAL.equals(normType)) {
binding.normTypeIV.setImageDrawable(context.getDrawable(R.drawable.bg_pure_color_gray));
binding.normTypeTV.setText(R.string.text_common_norm);
} else if (Norm.NORM_TYPE_KEY.equals(normType)) {
binding.normTypeIV.setImageDrawable(context.getDrawable(R.drawable.bg_pure_color_red));
binding.normTypeTV.setText(R.string.text_key_norm);
} else if (Norm.NORM_TYPE_ONCE.equals(normType)) {
binding.normTypeIV.setImageDrawable(context.getDrawable(R.drawable.bg_pure_color_green));
binding.normTypeTV.setText(R.string.text_once_norm);
}
binding.totalItemCount.setText(String.format(Locale.CHINA, "%d", getItemCount()));
return holder;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.setOptions(mItems.get(position), position);
}
public interface OnClickListener {
void onClick(int position);
}
private OnClickListener nextItemListener;
private OnClickListener lastItemListener;
public void setNextItemListener(OnClickListener listener) {
this.nextItemListener = listener;
}
public void setLastItemListener(OnClickListener listener) {
this.lastItemListener = listener;
}
@Override
public int getItemCount() {
return mItems.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
ItemEvaluateNormViewBinding mBinding;
ViewHolder(ItemEvaluateNormViewBinding binding) {
super(binding.getRoot());
mBinding = binding;
}
void setOptions(@NotNull Norm norm, int position) {
mBinding.nextItemBtn.setEnabled(norm.isEvaluated());
mBinding.radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
norm.setEvaluated(true);
norm.setCheckedId(checkedId);
mBinding.nextItemBtn.setEnabled(true);
});
}
}
}