1

I have a voice recorder app which shows the list of recordings in a RecyclerView. Each item has a default layout which looks like this (1): enter image description here

What I want is, when the user clicks on Play button, to swap the row layout to a player layout, to look as shown here (2):

enter image description here

I have been playing around with getItemViewType, but I have only managed to swap all of the rows, which is wrong as I only want to swap the current recording being played.

As for now, this is what I have:

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

Sorry for the mess of code, I have been trying several solutions from other posts.

Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58

1 Answers1

0

Add a member which saves the position of the row you clicked (play). Then you can check the member in getItemViewType() and decide which layout should be loaded.

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

For more info check this: How to create RecyclerView with multiple view type?

Community
  • 1
  • 1
beeb
  • 1,615
  • 1
  • 12
  • 24
  • I have used mCurrentType, setting it like: mCurrentType = position; in the "play" button listener. Works like a charm, thank you! – dragonkhanrider Apr 11 '17 at 16:19