如何在回收站视图中动态更改项目视图?

时间:2017-02-04 19:49:26

标签: android android-recyclerview

我正在回收站视图中编写带有曲目列表的音乐播放器。我想更改点击项目中的图片视图。试图使用getItemViewType,但据我所知,它只设置一次视图类型。这是我的代码:

public class TrackListAdapter extends RecyclerView.Adapter<TrackListAdapter.RegularViewHolder> implements IntConstants{

private ArrayList<Track> trackList;
private Context context;
int selectedPos = -1;

public TrackListAdapter(ArrayList<Track> trackList, Context context) {
    this.trackList = trackList;
    this.context = context;
    EventBus.getDefault().register(this);
}

@Override
public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
    EventBus.getDefault().unregister(this);
    super.onDetachedFromRecyclerView(recyclerView);
}

@Override
public RegularViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    if (viewType == ITEM_TYPE_PLAYING){
        //here i am sending xml for clicked item
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.playing_track_item, parent, false);
        return new RegularViewHolder(itemView) ;
    }else {
        //here i'm sending xml for regular item
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.regular_track_item, parent, false);
        return new RegularViewHolder(itemView) ;
    }
}

@Override
public void onBindViewHolder(final RegularViewHolder holder, int position) {
    Track t = trackList.get(position);
    holder.textTrackTitle.setText(t.getTitle());
    holder.textTrackAlbum.setText(t.getAlbum());
    holder.textTrackArtist.setText(t.getArtist());
    holder.parent.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EventBus.getDefault().post(new MessageEvent(TRACK_CLICKED_ACTION_CODE, holder.getAdapterPosition()));
        }
    });
}

@Override
public int getItemCount() {
    return trackList.size();
}

class RegularViewHolder extends RecyclerView.ViewHolder{
    ImageView imageAlbumArt;
    TextView textTrackTitle, textTrackArtist, textTrackAlbum;
    RelativeLayout parent;

    private RegularViewHolder(View itemView){
        super(itemView);
        parent = (RelativeLayout)itemView.findViewById(R.id.item_parent);
        imageAlbumArt = (ImageView)itemView.findViewById(R.id.image_album_art);
        textTrackTitle = (TextView) itemView.findViewById(R.id.text_track_title);
        textTrackArtist = (TextView) itemView.findViewById(R.id.text_track_artist);
        textTrackAlbum = (TextView) itemView.findViewById(R.id.text_track_album);
    }}

@Override
public int getItemViewType(int position) {
    if (position == selectedPos){
        return ITEM_TYPE_PLAYING;
    }else {
        return ITEM_TYPE_REGULAR;
    }
}

@Subscribe
public void handleTrack(MessageEvent event){
    if (event.getCode() == TRACK_SELECTED_ACTION_CODE){
        selectedPos = event.getPosition();
    }
}

}

1 个答案:

答案 0 :(得分:1)

您必须更新适配器存储的选定位置,然后notifyItemChanged()两次,以更新旧项目(以前用于选择)和新项目(现在已选择)的位置。< / p>