Android recyclerview 项目在不应该的时候更改其布局

问题描述 投票:0回答:0

我希望我的 recyclerview 项目只有在我单击其

ivEditTitle
视图时才更改其布局,并在我再次单击
ivEditTitle
后恢复旧布局。当我来回滚动时,其他项目应该具有完整的布局。

现在是这样的。我有一个带有两个视图持有者的回收视图,现在当我单击

ivEditTitle
时,特定项目会正确更改其布局,但是当我滚动并到达此视图持有者的另一个项目时,它也会像前面提到的项目一样更改布局,尽管它不应该正如我之前所说。

这是我认为自定义 recyclerview 适配器的一部分的关键代码:

   public class ViewHolderMultipleElement extends RecyclerView.ViewHolder {
    private static final int MODE_EDIT = 1;
    private static final int MODE_SAVE = 2;

    final RelativeLayout relativeLayout;
    final LinearLayout linearLayout;
    final TextView tvNotesGroupNr;
    final TextView tvNotesTitle;
    final TextView tvNotesCount;
    final ImageView ivEditTitle;

    private EditText etTitle;

    public int externalNoteOrder;
    public int mode = MODE_EDIT;
    private boolean isOnMove;

    ViewHolderMultipleElement(View itemView) {
        super(itemView);
        relativeLayout = itemView.findViewById(R.id.rl_main);
        linearLayout = itemView.findViewById(R.id.ll);
        tvNotesGroupNr = itemView.findViewById(R.id.notes_group_nr);
        tvNotesTitle = itemView.findViewById(R.id.tv_notes_title);
        tvNotesCount = itemView.findViewById(R.id.tv_notes_count);
        ivEditTitle = itemView.findViewById(R.id.iv_edit_title);

        ivEditTitle.setOnClickListener(view -> {
            if (mode == MODE_EDIT) {
                ivEditTitle.setImageResource(android.R.drawable.ic_menu_save);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
                layoutParams.weight = 4;
                layoutParams.gravity = Gravity.START;

                etTitle = new EditText(fragment.requireContext());
                etTitle.setText(tvNotesTitle.getText().toString());
                etTitle.setHint(R.string.group_title);
                etTitle.setLayoutParams(layoutParams);
                linearLayout.removeView(tvNotesTitle);
                linearLayout.removeView(tvNotesCount);
                linearLayout.addView(etTitle);
                linearLayout.addView(tvNotesCount);
                mode = MODE_SAVE;
            } else if (mode == MODE_SAVE) {
                db.updateGroupTitle(externalNoteOrder, etTitle.getText().toString());
                ivEditTitle.setImageResource(android.R.drawable.ic_menu_edit);
                tvNotesTitle.setText(etTitle.getText().toString());
                linearLayout.removeView(etTitle);
                linearLayout.removeView(tvNotesCount);
                linearLayout.addView(tvNotesTitle);
                linearLayout.addView(tvNotesCount);
                mode = MODE_EDIT;
            }
        });
    }
}

任何帮助将不胜感激。

java android android-recyclerview
© www.soinside.com 2019 - 2024. All rights reserved.