我的recyclerview中的getItemViewType

时间:2016-02-11 11:41:33

标签: android android-recyclerview

我的适配器出现了这个问题,无法猜出解决方案。它是关于我的recyclerview中的多视图(完全是2个):   - 头   - 列表

一切都适用于第一行不会出现的列表 这是我的适配器代码:

 public class CardAdapterPorfileview extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private ImageLoader imageLoader;
    private Context mcontext;
    public static final int Head = 1;
    public static final int List = 0;
    List<ProfileFeed> ProfileFeeds;
    @Override
    public int getItemViewType(int position) {

        return (position == 0? Head : List);
    }

    public CardAdapterPorfileview(List<ProfileFeed> dataSet, Context context) {
        this.ProfileFeeds = dataSet;
        this.mcontext = context;
    }

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

            case Head:
                return new ViewHolderHead(LayoutInflater.from(parent.getContext()).inflate(R.layout.profilestar_head, parent, false));

            case List:
                return new ViewHolderlist(LayoutInflater.from(parent.getContext()).inflate(R.layout.profilestar_feed, parent, false));
        }
        throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
    }
    //imgLoader1.displayImage(imageprofile, holder1.imageviewProf);
    //imgLoader1.displayImage(imagecover, holder1.imageviewCover);
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ProfileFeed feed = ProfileFeeds.get(position);
        imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
        switch (getItemViewType(position)) {
            case Head:
                ViewHolderHead holder1 = (ViewHolderHead) holder;
                holder1.biographie.setText(linebio);
                imageLoader.get(lineimageprofile, ImageLoader.getImageListener(holder1.imageviewProf, R.mipmap.blank, android.R.drawable.ic_dialog_alert));
                holder1.imageviewProf.setImageUrl(lineimageprofile, imageLoader);
                imageLoader.get(lineimagecover, ImageLoader.getImageListener(holder1.imageviewCover, R.mipmap.blank, android.R.drawable.ic_dialog_alert));
                holder1.imageviewCover.setImageUrl(lineimagecover, imageLoader);
                registerForContextMenu(holder1.imageviewProf);
                holder1.imageviewProf.setFocusable(true);
                holder1.imageviewProf.setClickable(true);
                registerForContextMenu(holder1.imageviewCover);
                holder1.imageviewCover.setFocusable(true);
                holder1.imageviewCover.setClickable(true);
                break;

            case List:
                ViewHolderlist holder2 = (ViewHolderlist) holder;
                imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
                imageLoader.get(feed.getImageUrl(), ImageLoader.getImageListener(holder2.imageView, R.mipmap.blank, android.R.drawable.ic_dialog_alert));
                holder2.textViewuserName.setText(feed.getuserName());
                holder2.textViewName.setText(feed.getCaption());
                holder2.imageView.setImageUrl(feed.getImageUrl(), imageLoader);
                holder2.textViewid.setText(feed.getpostID());
                holder2.textTime.setText(feed.getTime());
                break;
        }
    }



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

    class ViewHolderHead extends RecyclerView.ViewHolder {

        public NetworkImageView imageviewProf;
        public NetworkImageView imageviewCover;
        public TextView biographie;

        public ViewHolderHead(View itemView) {
            super(itemView);
            imageviewProf = (NetworkImageView) itemView.findViewById(R.id.imageViewProfilePicture1);
            imageviewCover = (NetworkImageView) itemView.findViewById(R.id.imageViewProfileCover);
            biographie = (TextView) itemView.findViewById(R.id.textbio);
        }
    }

    class ViewHolderlist extends RecyclerView.ViewHolder {
        public NetworkImageView imageView;
        public TextView textViewName;
        public TextView textViewuserName;
        public TextView textViewid;
        public TextView textTime;

        public ViewHolderlist(View itemView) {
            super(itemView);
            imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewnewsfeedprofile);
            textViewName = (TextView) itemView.findViewById(R.id.textViewNameprofile);
            textViewuserName = (TextView) itemView.findViewById(R.id.textusernamenewsfeedprofile);
            textViewid = (TextView) itemView.findViewById(R.id.textPostIDprofile);
            textTime = (TextView) itemView.findViewById(R.id.textTimeprofile);

        }
    }
}

非常感谢

1 个答案:

答案 0 :(得分:2)

由于您需要RecyclerView中的附加项,因此您需要修改count和所需的dataset项:

@Override
public int getItemCount() {
    return ProfileFeeds.size() + 1;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if(position != 0){
        ProfileFeed feed = ProfileFeeds.get(position - 1); //or move this to case List :)
    }
    imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
    switch (getItemViewType(position)) {
        case Head:
            //your stuff as it is
            break;

        case List:
            //your stuff
            break;
    }
}