0

我有一個錄音機應用程序,它顯示RecyclerView中的錄音列表。每個項目都有一個默認的佈局,看起來像這樣(1): enter image description hereRecyclerView行的交換佈局onClick

我要的是,當用戶點擊播放按鈕,換行佈局球員佈局,看起來如下所示(2 ):

enter image description here

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

對不起,我的代碼的混亂,我一直在試圖從其他職位幾種解決方案。

回答

0

添加一個成員,保存您點擊的行的位置(玩)。然後你可以檢查getItemViewType()中的成員並決定應該加載哪個佈局。

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

欲瞭解更多信息檢查:How to create RecyclerView with multiple view type?

+0

我用mCurrentType,設置它喜歡: mCurrentType =位置; 「play」按鈕監聽器中的 。像魅力一樣工作,謝謝! – dragonkhanrider