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

时间:2016-12-22 17:23:59

标签: android android-recyclerview recycler-adapter

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

我有 news_layout_one news_layout_two ,但我不知道使用两种布局请在此代码中帮助我...谢谢

公共类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);

    }
}

}

2 个答案:

答案 0 :(得分:1)

您需要覆盖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()中执行相同的逻辑

答案 1 :(得分:1)

您必须为不同的布局创建不同的viewHolder类,并在onCreateVIewHolder中附加不同的视图。这是一个例子:

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;
    RecyclerView.ViewHolder viewHolder;

    switch (viewType) {
        case TYPE_ONE: {
            view = getLayoutView(parent, R.layout.list_one);
            viewHolder = new OneViewHolders(view, currActivity);
            break;
        }
        case TYPE_TWO: {
            view = getLayoutView(parent, R.layout.list_two);
            viewHolder = new TwoViewHolders(view, currActivity, getItem(viewType));
            break;
        }
        case TYPE_THREE: {
            view = getLayoutView(parent, R.layout.list_three);
            viewHolder = new ThreeViewHolders(view, currActivity, getItem(viewType));
            break;
        }
        default: {
            viewHolder = null;
        }

    }
    return viewHolder;
}

然后创建三个不同的viewHolder类(在本例中为OneViewHolders,TwoViewHolders,ThreeViewHolders)并在那里定义您的视图。