我有一個回收器視圖,應顯示如下數據(如果位置是偶數,則爲1項,如果位置奇數則爲2項),並誠實地嘗試並嘗試但我沒有這樣做。RecyclerView佈局管理器操縱
下面是它應該是什麼樣子
這裏是如何它現在(忽略的寬度和高度,看到3如何將視頻充氣兩次)
我m使用LinearLayoutManager,這裏是我的適配器:
public class MediaItemsAdapter extends RecyclerView.Adapter<MediaItemsAdapter.RecyclerViewHolder> {
private List<MediaItem> allMediaItems;
private Activity context;
private RecyclerViewOnItemClickListener itemClickListener;
public MediaItemsAdapter(Activity context, List<MediaItem> allMediaItems, RecyclerViewOnItemClickListener itemClickListener) {
this.allMediaItems = allMediaItems;
this.context = context;
this.itemClickListener = itemClickListener;
}
@Override
public void onBindViewHolder(final RecyclerViewHolder holder, final int position) {
MediaItem mediaItem = allMediaItems.get(position);
if(position %2 == 0){ // even
holder.tvTitle.setText(mediaItem.getTitle());
holder.tvSummary.setText(mediaItem.getDate());
try {
Picasso.with(context)
.load(mediaItem.getImageUrl())
.into(holder.ivImage, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
holder.ivImage.setAlpha((float) 1);
}
@Override
public void onError() {
Log.wtf("error", "in loading");
}
});
} catch (Exception e) {
e.printStackTrace();
}
holder.ivPlay.setVisibility(mediaItem.isVideo() ? View.VISIBLE : View.GONE);
}else{ //odd
holder.tvTitle.setText(mediaItem.getTitle());
holder.tvSummary.setText(mediaItem.getDate());
try {
Picasso.with(context)
.load(mediaItem.getImageUrl())
.into(holder.ivImage, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
holder.ivImage.setAlpha((float) 1);
}
@Override
public void onError() {
Log.wtf("error", "in loading");
}
});
} catch (Exception e) {
e.printStackTrace();
}
holder.ivPlay.setVisibility(mediaItem.isVideo() ? View.VISIBLE : View.GONE);
try{
MediaItem secondMediaItem = allMediaItems.get(position + 1);
holder.tvSecondTitle.setText(secondMediaItem.getTitle());
holder.tvSecondSummary.setText(secondMediaItem.getDate());
try {
Picasso.with(context)
.load(secondMediaItem.getImageUrl())
.into(holder.ivSecondImage, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
holder.ivSecondImage.setAlpha((float) 1);
}
@Override
public void onError() {
Log.wtf("error", "in loading");
}
});
} catch (Exception e) {
e.printStackTrace();
}
holder.ivSecondPlay.setVisibility(secondMediaItem.isVideo() ? View.VISIBLE : View.GONE);
}catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public int getItemViewType(int position) {
return position %2 == 0 ? 0 : 1;
}
@Override
public int getItemCount() {
return this.allMediaItems.size();
}
public interface RecyclerViewOnItemClickListener {
void onItemClicked(View v, int position);
}
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView;
if (viewType == 0){
layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.even_media_layout, parent, false);
RecyclerViewHolder rcv = new RecyclerViewHolder(layoutView);
return (rcv);
}else{
layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.odd_media_layout, parent, false);
RecyclerViewHolder rcv = new RecyclerViewHolder(layoutView);
return (rcv);
}
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView tvTitle, tvSecondTitle, tvSummary, tvSecondSummary;
private ImageView ivImage, ivSecondImage;
private ImageButton ivPlay, ivSecondPlay;
protected View itemView;
public RecyclerViewHolder(View itemView) {
super(itemView);
this.itemView = itemView;
this.itemView.setOnClickListener(this);
tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
tvSecondTitle = (TextView) itemView.findViewById(R.id.tvSecondTitle);
tvSummary = (TextView) itemView.findViewById(R.id.tvSummary);
tvSecondSummary = (TextView) itemView.findViewById(R.id.tvSecondSummary);
ivImage = (ImageView) itemView.findViewById(R.id.ivImage);
ivSecondImage = (ImageView) itemView.findViewById(R.id.ivSecondImage);
ivPlay = (ImageButton) itemView.findViewById(R.id.ivPlay);
ivSecondPlay = (ImageButton) itemView.findViewById(R.id.ivSecondPlay);
}
@Override
public void onClick(View view) {
itemClickListener.onItemClicked(view, getLayoutPosition());
}
}
}
如何防止數據複製,如何判斷數組列表中的對象3是否已經膨脹?有沒有比LinearLayoutManager更好的方法
問候。
嗨,歡迎來到Stack Overflow。如果你提供[mcve],你的問題可能會吸引更多的興趣。就目前來看,這個問題太複雜,而且有太多不相關的部分。 –