gpt4 book ai didi

android - 如何在 recyclerview 中使用不同的布局?

转载 作者:行者123 更新时间:2023-11-29 17:08:27 25 4
gpt4 key购买 nike

如何在recyclerview中使用两种不同的布局

我有 news_layout_one , news_layout_two 但我不知道使用两个布局请帮我写这段代码...谢谢

公共(public)类 CardAdapter 扩展 RecyclerView.Adapter {

//Imageloader to load image
private ImageLoader imageLoader;
private Context context;
private Activity activity;
private LayoutInflater inflater;

private static String Title="title";
private static String Genre="genre";
private static String Rating="rating";



//List to store all superheroes
List<SuperHero> superHeroes;

//Constructor of this class
public CardAdapter(List<SuperHero> superHeroes, Context context){
super();
//Getting all superheroes
this.superHeroes = superHeroes;
this.context = context;
}




@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_layout_one, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}




@Override
public void onBindViewHolder(final ViewHolder holder, int position) {

//Getting the particular item from the list
final SuperHero superHero = superHeroes.get(position);
//Loading image from url
imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
imageLoader.get(superHero.getThumbnailUrl(), ImageLoader.getImageListener(holder.imageView, R.drawable.au_small_logo, android.R.drawable.ic_dialog_alert));

//Showing data on the views
holder.imageView.setImageUrl(superHero.getThumbnailUrl(), imageLoader);
holder.textViewName.setText(superHero.getTitle());
holder.textViewPublisher.setText(superHero.getRating());
holder.secid.setText(superHero.getSec_id());
holder.category.setText(superHero.getCategory());



holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), News_Detail.class);

intent.putExtra("images", superHero.getThumbnailUrl());
intent.putExtra(Title, superHero.getTitle());
intent.putExtra(Genre, superHero.getGenre());
intent.putExtra(Rating, superHero.getRating());
v.getContext().startActivity(intent);
}
});



holder.share.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick( View view)
{
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "\n" + "\n" + "http://abc.news/News/"+holder.secid.getText().toString()+"/"+holder.category.getText().toString());
view.getContext().startActivity(intent);
}

});
}

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

class ViewHolder extends RecyclerView.ViewHolder{
//Views
public NetworkImageView imageView;
public TextView textViewName;
public TextView textViewPublisher;
public TextView secid;
public TextView category;
public RelativeLayout relativeLayout;
public ImageView share;

//Initializing Views
public ViewHolder(View itemView) {
super(itemView);
imageView = (NetworkImageView) itemView.findViewById(R.id.thumbnail);
textViewName = (TextView) itemView.findViewById(R.id.title);
textViewPublisher = (TextView) itemView.findViewById(R.id.rating);
secid = (TextView) itemView.findViewById(R.id.sec_id);
category = (TextView) itemView.findViewById(R.id.category);
relativeLayout = (RelativeLayout) itemView.findViewById(R.id.relat);
share = (ImageView) itemView.findViewById(R.id.share);

}
}

最佳答案

您需要重写 getItemViewType(int position) 如下:

class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
private static final int VIEW_TYPE_ONE = 1;
private static final int VIEW_TYPE_TWO = 2;

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_ONE) {
// inflate first view type
} else {
// inflate second view type
}
}

@Override
public int getItemViewType(int position) {
// return your ViewType depending on position here
}
}

然后你必须在 onBindViewHolder 中执行与在 onCreateViewHolder 中相同的逻辑,通过 holder.getItemViewType()

关于android - 如何在 recyclerview 中使用不同的布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41288676/

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