我可以只为一个使用不同设计的三种不同CardView使用一个RecyclerView吗?

时间:2016-02-08 10:36:38

标签: android android-recyclerview android-cardview

我是否只能创建一个ng-click="myClickedEvent($event)" 来处理具有不同设计的三组n0=0; w=0.5; N=64; Ncap=5*N; L=Ncap/N; n=(-Ncap/2:(Ncap/2)-1); Ws=N/Ncap; s=sinc(Ws.*(n+n0)); test_FIR=[0.5 0 0 0 0 1 0 0 0 0 0.5]; conv_out= (conv(test_FIR,s)/max(conv(s,test_FIR))); figure(2) plot(s,) hold on plot(conv_out,'k','LineWidth',2) 或者我是否需要为每个recycler_view.xml创建单独的cardview.xml我的recyclerView

例如,我对cardViews有不同的CardView

2 个答案:

答案 0 :(得分:4)

是的,您可以使用getItemViewType()

根据视图类型为项目

填充不同的布局
  

实施例

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    class ViewHolder0 extends RecyclerView.ViewHolder {
        ...
    }

    class ViewHolder2 extends RecyclerView.ViewHolder {
        ...
    }

    @Override
    public int getItemViewType(int position) {

        return viewType;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         switch (viewType) {
             case 0: return new ViewHolder0(...);
             case 2: return new ViewHolder2(...);
             ...
         }
    }
}

答案 1 :(得分:2)

是的,我们可以使用多个cardView。 供参考,请参阅以下链接 Sample Adapter

这里我显示的评论列表没有评论我显示没有评论可用。在这里,我为我的评论项目创建了一个ViewHolders,另一个没有评论可用项目。

代码:

package com.subbu.moviemasti.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.subbu.moviemasti.R;
import com.subbu.moviemasti.entities.Review;
import com.subbu.moviemasti.fragment.IReviewView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by subrahmanyam on 24-12-2015.
 */
public class ReviewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final List<Review> results;

    public ReviewAdapter(IReviewView reviewFragment, List<Review> results) {
        this.results = results;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        RecyclerView.ViewHolder viewHolder = null;
        switch (viewType) {
            case 0:
                view = View.inflate(parent.getContext(), R.layout.list_item_empty_view, null);
                viewHolder = new EmptyViewHolder(view);
                break;
            case 1:
                view = View.inflate(parent.getContext(), R.layout.review_list_item, null);
                viewHolder = new ViewHolder(view);
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int viewType = getItemViewType(position);
        switch (viewType) {
            case 0:
                EmptyViewHolder emptyViewHolder = (EmptyViewHolder) holder;
                emptyViewHolder.emptyView.setText(((EmptyViewHolder) holder).emptyView.getResources().getString(R.string.no_trailers));
                break;
            case 1:
                Review review = results.get(position);
                ViewHolder viewHolder = (ViewHolder) holder;
                viewHolder.userName.setText(review.getAuthor());
                viewHolder.review.setText(review.getContent());
                break;
        }
    }

    @Override
    public int getItemCount() {
        return results.size() > 0 ? results.size() : 1;
    }

    @Override
    public int getItemViewType(int position) {
        return results.size() == 0 ? 0 : 1;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.review_user)
        TextView userName;
        @Bind(R.id.review_review)
        TextView review;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}