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