交换 RecyclerView 行的布局 onClick

Swap layout for a RecyclerView row onClick

我有一个录音机应用程序,它在 RecyclerView 中显示录音列表。每个项目都有一个默认布局,如下所示 (1):

我想要的是,当用户点击播放按钮时,将行布局切换为播放器布局,如下所示 (2):

我一直在玩 getItemViewType,但我只设法交换了所有行,这是错误的,因为我只想交换当前正在播放的录音。

至于现在,这是我拥有的:

public class ListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private ArrayList<String> files;
    private boolean is_recording;

    private static final int PLAYING_LAYOUT = 1;
    private static final int STILL_LAYOUT = 0;
    private int mCurrentType = 0;

    private MediaPlayer multimedia_reproduccion;

    //STILL LAYOUT VH
    public class ViewHolderStill extends RecyclerView.ViewHolder {
        public TextView txt_file;
        public CardView cardView;
        public ImageButton btn_play;
        public ImageButton btn_delete;
        public TextView txt_duration;

        public ViewHolderStill(View v) {
            super(v);
            cardView = (CardView) v.findViewById(R.id.card);
            txt_file = (TextView) v.findViewById(R.id.text_item);
            btn_play = (ImageButton) v.findViewById(R.id.play_item);
            btn_delete = (ImageButton) v.findViewById(R.id.btn_delete);
            txt_duration = (TextView) v.findViewById(R.id.duration);
        }
    }

    //PLAYING LAYOUT VH
    public class ViewHolderPlaying extends RecyclerView.ViewHolder {
        public ViewHolderPlaying(View v) {
            //dummy just for testing
            super(v);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == STILL_LAYOUT) {
            View v_still = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_file, parent, false);
            return new ViewHolderStill(v_still);
        } else {
            View v_play = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_file_play, parent, false);
            return new ViewHolderPlaying(v_play);
        }

    }

    @Override
    public int getItemViewType (int position) {
        return mCurrentType;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (holder.getItemViewType() == STILL_LAYOUT) {
          final ViewHolderStill viewHolder = (ViewHolderStill)holder;
          (...)
          viewHolder.btn_play.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(final View view) {
                        (...)
                            if (!multimedia_reproduccion.isPlaying() && viewHolder.btn_play.getTag().toString().equals("play")) {

                                    //Trying to change the layout type, but I know this should not be a global variable so I need suggestions here
                                    mCurrentType = 1;
                                    getItemViewType(position);
                                    notifyItemChanged(position);
                            }
          (...)
          }

抱歉代码乱七八糟,我一直在尝试其他帖子中的几种解决方案。

添加一个保存您点击(播放)行位置的成员。然后你可以检查getItemViewType()中的成员并决定应该加载哪个布局。

@Override
public int getItemViewType(int position) {
  return position == playposition ? PLAYING_LAYOUT : STILL_LAYOUT;
}

有关更多信息,请查看:How to create RecyclerView with multiple view type?