简体   繁体   English

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

[英]Recycler view's item background doesn't update on click events?

I have audio player layout as an item in recycler view . 我在回收站视图中将音频播放器布局作为一项进行设置。 I want the play icon to change into pause icon on icon clicked . 我希望播放图标在单击的图标上变为暂停图标。 The log shows that icon has been set but the icon doesn't change in appearance in recycler view . 日志显示该图标已被设置,但在回收站视图中该图标的外观并未改变。

My code: 我的代码:

@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();
                }
            });

        }

    }

I actually have a linear layout for audio item. 我实际上有一个线性布局的音频项目。 I'm setting onClickListener() no an ImageButton to change it's background from play to pause. 我没有设置onClickListener()的ImageButton来将其背景从播放更改为暂停。

Extra information: 额外的信息:

I have observed that clicking the icon first time doesn't do anything . 我观察到,第一次单击该图标不会执行任何操作。

Clicking it second time changes it to pause icon but then it quickly turns back to play icon . 再次单击它会将其更改为暂停图标,但随后会迅速返回播放图标。

Clicking it third time changes it to pause icon . 第三次单击将其更改为暂停图标。

Any suggestion to get this working be great ! 任何使该工作正常的建议都很棒!

Do you execute the code you showed in Adapter? 您是否执行了在适配器中显示的代码? If so, why don't you call notifyItemChanged(position)? 如果是这样,为什么不调用notifyItemChanged(position)? And if not, why don't you create method in Adapter that update view with the parameter of position and call it from Activity or Fragment? 如果不是,为什么不在Adapter中创建使用position参数更新视图并从Activity或Fragment调用它的方法呢?

delete this line : 删除此行:

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

or save current background icon for recyclerview item 或保存当前的回收站背景图标

I'm always using notifyDataSetChanged() and never had any problem with that. 我一直在使用notifyDataSetChanged(),从来没有任何问题。 Can you just use that or are there certain reasons you doing it otherwise? 您可以只使用它,还是有某些其他原因?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM