回收者视图的项目背景不会在点击事件中更新吗?

时间:2019-08-09 11:09:24

标签: android android-studio android-recyclerview

我在回收站视图中将音频播放器布局作为一项进行设置。我希望播放图标在单击的图标上变为暂停图标。日志显示该图标已设置,但在回收站视图中该图标的外观没有改变。

我的代码:

@Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {


        customNoteView = notesList.get(i);
        notesText = viewHolder.notesWriteArea;
        notesImage = viewHolder.notesImage;
        notesAudio = viewHolder.notesAudio;
        notesAudioPlayButton = viewHolder.notesAudioPlayButton;
        notesAudioSeekBar = viewHolder.notesAudioSeekBar;
        notesAudioDeleteButton = viewHolder.notesAudioDeleteButton;



        if(customNoteView.getNoteText()!=null){
            editTextList.add(notesText);
            notesText.setVisibility(View.VISIBLE);
            notesImage.setVisibility(View.GONE);
            notesAudio.setVisibility(View.GONE);
            notesText.requestFocus();
            focussedEditText = notesText;
            notesText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    if(b){
                        focussedEditText = ((CustomEditText)view);
                        //Toast.makeText(context, ((CustomEditText) view).getText().toString(), Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
        else if(customNoteView.getImageUri()!=0){
            notesImage.setVisibility(View.VISIBLE);
            notesText.setVisibility(View.GONE);
            notesAudio.setVisibility(View.GONE);
            Glide.with(notesImage.getContext())
                    .load(notesImage.getContext().getDrawable(customNoteView.getImageUri()))
                    .into(notesImage);

            notesImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, FullScreen.class);
                    ActivityOptionsCompat options =  ActivityOptionsCompat.makeSceneTransitionAnimation((Activity) context,
                            notesImage,
                            ViewCompat.getTransitionName(notesImage)
                            );

                    context.startActivity(intent, options.toBundle());

                }
            });
        }
        else if(customNoteView.getAudioUri()!=null){
            notesAudio.setVisibility(View.VISIBLE);
            notesImage.setVisibility(View.GONE);
            notesText.setVisibility(View.GONE);
            notesAudioPlayButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    notesAudioPlayButton.setBackground(context.getDrawable(R.drawable.ic_pause));
                    notesAudio.setBackground(context.getDrawable(R.drawable.gray_rectangular_background));
                    Adapter.this.notifyItemChanged(viewHolder.getAdapterPosition());

                    Toast.makeText(context, ""+viewHolder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
                }
            });

        }

    }

我实际上是音频项目的线性布局。我将onClickListener()设置为没有ImageButton来将其背景从播放更改为暂停。

其他信息:

我观察到第一次单击图标没有任何作用。

第二次单击它会将其更改为暂停图标,但随后会迅速转回播放图标。

第三次单击将其更改为暂停图标。

任何使该工作正常的建议都很棒!

3 个答案:

答案 0 :(得分:0)

您执行在适配器中显示的代码吗?如果是这样,为什么不致电notifyItemChanged(position)? 如果不是,为什么不在Adapter中创建使用position参数更新视图并从Activity或Fragment中调用它的方法呢?

答案 1 :(得分:0)

删除此行:

Adapter.this.notifyItemChanged(viewHolder.getAdapterPosition());

或保存当前的回收站视图项目背景图标

答案 2 :(得分:0)

我一直在使用notifyDataSetChanged(),从没有任何问题。您可以只使用它,还是有某些其他原因?

相关问题