在recyclerview中有多个视图

时间:2016-12-19 10:35:35

标签: android android-recyclerview

我读过How to create RecyclerView with multiple view type?但不了解一些事情。

首先,我的目标是:

  1. 我不明白 import * as FriendCard from module("./FriendCardJs"); (在我的情况下有效):

    getItemViewType

    因为,我想

  2. 设置Item2 仅在点击后不在开头。这导致我理解的问题,如何实现getItemViewType。任何伪代码/原型都非常受欢迎。

  3. 代码: [第一种方法 - 将每隔一行设置为详细布局,并尝试在默认情况下隐藏它。稍后更改onClick方法的可见性..]

    public int getItemViewType(int position) {
      // Just as an example, return 0 or 2 depending on position
      // Note that unlike in ListView adapters, types don't have to be contiguous
      return position % 2 * 2;
    }
    

    [2方法 - 尝试添加ITEM2 onClick]

    public class DifferentRowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    
    private List<Model> items;
    FragmentActivity c;
    
    
    public DifferentRowAdapter(List<Model> items, FragmentActivity c) {
        this.items = items;
        this.c = c;
    }
    
    
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
    
        switch (viewType) {
            case MEAL_TYPE:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rec_item, parent, false);
                return new ViewHolder1(view);
            case DETAIL_TYPE:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.detail_test, parent, false);
                return new ViewHolder2(view);
        }
        return null;
    }
    
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    
        final Model object = items.get(position);
        final int currentPosition = position;
    
    
        if (object != null) {
            switch (object.getmType()) {
                case MEAL_TYPE:
                    ((ViewHolder1) holder).mNameTextView.setText(object.getName());
                    ((ViewHolder1) holder).img.setImageResource(R.mipmap.ic_launcher);
                    break;
                case DETAIL_TYPE:
                    ((ViewHolder2) holder).detailsText.setText(object.getDescription());
                    break;
            }
        }
    
    }
    
    @Override
    public int getItemCount() {
        if (items == null)
            return 0;
        return items.size();
    }
    
    
    
    @Override
    public int getItemViewType(int position) {
           // I need ITEM2 to be at third/sixth/nineth ect. position
           if(position % 3 == 0){
                return DETAIL_TYPE;
           }
           else{
               return MEAL_TYPE;
           }
    
    }
    
    public class ViewHolder1 extends RecyclerView.ViewHolder {
    
        TextView mNameTextView;
        ImageView img;
        mealsItemClickListener icl;
    
        public ViewHolder1(View itemView) {
            super(itemView);
    
            mNameTextView = (TextView) itemView.findViewById(R.id.name);
            img = (ImageView) itemView.findViewById(R.id.category_image);
        }
    
        public TextView getmNameTextView() {
            return mNameTextView;
        }
        public void setmNameTextView(TextView mNameTextView) {
            this.mNameTextView = mNameTextView;
        }
        public ImageView getImg() {
            return img;
        }
        public void setImg(ImageView img) {
            this.img = img;
        }
    }
    
    
    public class ViewHolder2 extends RecyclerView.ViewHolder {
    
        TextView detailsText;
        mealsItemClickListener icl;
    
        public ViewHolder2(View v) {
            super(v);
            detailsText = (TextView) itemView.findViewById(R.id.desc);
        }
    
        public TextView getDetailsText() {
            return detailsText;
        }
        public void setDetailsText(TextView detailsText) {
            this.detailsText = detailsText;
        }
    }
    
    
    }
    

    TL; DR我的情况:

    Approach1 :给我

    public class DifferentRowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    
    private List<Model> items;
    FragmentActivity c;
    
    
    public DifferentRowAdapter(List<Model> items, FragmentActivity c) {
        this.items = items;
        this.c = c;
    }
    
    
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
    
        switch (viewType) {
            case MEAL_TYPE:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rec_item, parent, false);
                return new ViewHolder1(view);
            case DETAIL_TYPE:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.detail_test, parent, false);
                return new ViewHolder2(view);
        }
        return null;
    }
    
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    
        final Model object = items.get(position);
        final int currentPosition = position;
    
    
        if (object != null) {
            switch (object.getmType()) {
                case MEAL_TYPE:
                    ((ViewHolder1) holder).mNameTextView.setText(object.getName());
                    ((ViewHolder1) holder).img.setImageResource(R.mipmap.ic_launcher);
                    break;
                case DETAIL_TYPE:
                    ((ViewHolder2) holder).detailsText.setText(object.getDescription());
                    break;
            }
    
            //THIS METHOD WORKS FINE POSITIONING WISE, BUT IT DUPLICATES CLICKED ITEM INSTEAD OF PUTTING ITEM2
            ((ViewHolder1) holder).img.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
    
                Toast.makeText(c, "Position: " + currentPosition, Toast.LENGTH_SHORT).show();
                    addItem(currentPosition, object);
            }
        });
        }
    
    }
    
    @Override
    public int getItemCount() {
        if (items == null)
            return 0;
        return items.size();
    }
    
        @Override
        public int getItemViewType(int position) {
            if (items != null) {
               Model object = items.get(position);
                if (object != null) {
                    return object.getmType();
              }
            }
            return 0;
        }
    
        // Check if clicked item is left/right  
        private boolean isLeft(final int position){
        if (position % 2 == 0){
            return true; // Returns 1 if it's left columns item
        }
        else return false;
        }
    
        // Add item at wanted position. If left pos+2, if right+1.
        private void addItem(final int position, Model object){
        if(isLeft(position)){
            items.add(position+2, object);
            notifyItemInserted(position+2);
        }
        else{
            items.add(position+1, object);
            notifyItemInserted(position+1);
        }
    
    }
    
    
    public class ViewHolder1 extends RecyclerView.ViewHolder {
    
        TextView mNameTextView;
        ImageView img;
        mealsItemClickListener icl;
    
        public ViewHolder1(View itemView) {
            super(itemView);
    
            mNameTextView = (TextView) itemView.findViewById(R.id.name);
            img = (ImageView) itemView.findViewById(R.id.category_image);
        }
    
        public TextView getmNameTextView() {
            return mNameTextView;
        }
        public void setmNameTextView(TextView mNameTextView) {
            this.mNameTextView = mNameTextView;
        }
        public ImageView getImg() {
            return img;
        }
        public void setImg(ImageView img) {
            this.img = img;
        }
    }
    
    
    public class ViewHolder2 extends RecyclerView.ViewHolder {
    
        TextView detailsText;
        mealsItemClickListener icl;
    
        public ViewHolder2(View v) {
            super(v);
            detailsText = (TextView) itemView.findViewById(R.id.desc);
        }
    
        public TextView getDetailsText() {
            return detailsText;
        }
        public void setDetailsText(TextView detailsText) {
            this.detailsText = detailsText;
        }
    }
    
    
    }
    

    Approach2 :在特定位置添加项目工作正常,但它添加了点击项目,而不是item2。我想更多地关注方法2,但如果有人可以解释第一个 - 我也可以。我不确定它为什么不起作用,因为它应该是 - 或者我是否想念smth?

    PROBABLY APPROACH1解决方案:

    在第一种方法中, 如果位置值相同,则getItemViewType(position)返回的值与items.get(position).getmType()返回的值不同。

    我必须在getItemViewType和onBindViewHolder方法中使类型的值相同;

    怎么做?

0 个答案:

没有答案