gpt4 book ai didi

java - 安卓。 java 。回收适配器

转载 作者:太空狗 更新时间:2023-10-29 13:14:53 24 4
gpt4 key购买 nike

我在一个布局中有两个不同的卡片 View 。我需要将最新消息添加到一个卡片 View 中,其余的则添加到另一个卡片 View 中。例如,在图像上

enter image description here

MyRecyclerAdapter代码

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.CustomViewHolder> {
private List<FeedItem> feedItemList;
private Context mContext;

public MyRecyclerAdapter(Context context, List<FeedItem> feedItemList) {
this.feedItemList = feedItemList;
this.mContext = context;
}

@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, null);
CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;
}

@Override
public void onBindViewHolder(CustomViewHolder customViewHolder, int i) {
FeedItem feedItem = feedItemList.get(i);

//Download image using picasso library
Picasso.with(mContext).load(feedItem.getThumbnail())
.error(R.drawable.placeholder)
.placeholder(R.drawable.placeholder)
.into(customViewHolder.imageView);

//Setting text view title
customViewHolder.textView.setText(Html.fromHtml(feedItem.getTitle()));

View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
CustomViewHolder holder = (CustomViewHolder) view.getTag();
int position = holder.getPosition();

FeedItem feedItem = feedItemList.get(position);
Toast.makeText(mContext, feedItem.getTitle(), Toast.LENGTH_SHORT).show();
}
};

//Handle click event on both title and image click
customViewHolder.textView.setOnClickListener(clickListener);
customViewHolder.imageView.setOnClickListener(clickListener);

customViewHolder.textView.setTag(customViewHolder);
customViewHolder.imageView.setTag(customViewHolder);
}

@Override
public int getItemCount() {
return (null != feedItemList ? feedItemList.size() : 0);
}

public class CustomViewHolder extends RecyclerView.ViewHolder {
protected ImageView imageView;
protected TextView textView;

public CustomViewHolder(View view) {
super(view);
this.imageView = (ImageView) view.findViewById(R.id.thumbnail);
this.textView = (TextView) view.findViewById(R.id.title);
}
}
}

最佳答案

如果我在你身边,我会使用的方法来完成你在图片中发布的内容是覆盖 getItemViewType并且有两种不同类型的 ViewHolder,一种用于 header,一种用于大图像,另一种用于小行。 onBindViewHolder 获取类型作为参数。在你的例子中是 int i

关于java - 安卓。 java 。回收适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36055842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com