Android recyclerview multi layout Listview multi layout code implementation

Keywords: Android Java

Preface

Everyone will read the latest news on the browser every day. As an IT programmer, you will find that their layout and layout are different. Sometimes, there are 4 or 5 interfaces for the same style of items, and 1 appears below There are different items. Is the effect beautiful? This is called multi layout in Android. Let's implement IT briefly. Let's understand IT

Use

1.RecyclerView

The getItemViewType() method of Adapter is used to return different view ﹣ u types, and the algorithm of final remainder is used to get the items at the current position, and then the corresponding ViewHolder is set in onCreateViewHolder.

    final int VIEW_TYPE = 3;
    final int TYPE_1    = 0;
    final int TYPE_2    = 1;
    final int TYPE_3    = 2;
 /**
     * Get the View of each location
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        int p = position % VIEW_TYPE;
        if (p == 0) {
            return TYPE_1;
        } else if (p == 1) {
            return TYPE_2;
        } else {
            return TYPE_3;
        }

    }

   @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_1) {
            View view = View.inflate(mContext, R.layout.adapter, null);
            return new MyViewHolder(view);
        } else if (viewType == TYPE_2) {
            View view2 = View.inflate(mContext, R.layout.adapter2, null);

            return new MyViewHolder2(view2);
        } else {
            View view3 = View.inflate(mContext, R.layout.adapter3, null);
            return new MyViewHolder3(view3);
        }

    }

This method is to set data for each ViewHolder

@Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder holder1 = null;
        MyViewHolder2 holder2 = null;
        MyViewHolder3 holder3 = null;
        if (holder instanceof MyViewHolder) {
            holder1 = (MyViewHolder) holder;
        } else if (holder instanceof MyViewHolder2) {
            holder2 = (MyViewHolder2) holder;

        } else {
            holder3 = (MyViewHolder3) holder;

        }
    }

Full code:

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context       mContext;
    List<Integer> mList;
    final int VIEW_TYPE = 3;
    final int TYPE_1    = 0;
    final int TYPE_2    = 1;
    final int TYPE_3    = 2;


    public MyAdapter(Context context, List<Integer> list) {
        mContext = context;
        mList = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_1) {
            View view = View.inflate(mContext, R.layout.adapter, null);
            return new MyViewHolder(view);
        } else if (viewType == TYPE_2) {
            View view2 = View.inflate(mContext, R.layout.adapter2, null);

            return new MyViewHolder2(view2);
        } else {
            View view3 = View.inflate(mContext, R.layout.adapter3, null);
            return new MyViewHolder3(view3);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder holder1 = null;
        MyViewHolder2 holder2 = null;
        MyViewHolder3 holder3 = null;
        if (holder instanceof MyViewHolder) {
            holder1 = (MyViewHolder) holder;
        } else if (holder instanceof MyViewHolder2) {
            holder2 = (MyViewHolder2) holder;

        } else {
            holder3 = (MyViewHolder3) holder;

        }
    }


    /**
     * Get the View of each location
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        int p = position % VIEW_TYPE;
        if (p == 0) {
            return TYPE_1;
        } else if (p == 1) {
            return TYPE_2;
        } else {
            return TYPE_3;
        }

    }


    @Override
    public int getItemCount() {
        if (mList != null) {
            return mList.size();
        }
        return 0;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        public MyViewHolder(View itemView) {
            super(itemView);
        }
    }

    class MyViewHolder2 extends RecyclerView.ViewHolder {
        public MyViewHolder2(View itemView) {
            super(itemView);
        }
    }

    class MyViewHolder3 extends RecyclerView.ViewHolder {
        public MyViewHolder3(View itemView) {
            super(itemView);
        }
    }
}

2.ListView

ListView has more getViewTypeCount() layouts than RecyclerView

  //  There are several layouts
    @Override
    public int getViewTypeCount() {
        return VIEW_TYPE;
    }

Full code:

public class MyAdapter2 extends BaseAdapter {
    private List<Integer> list;
    private Context       ctx;
    final int VIEW_TYPE = 3;
    final int TYPE_1    = 0;
    final int TYPE_2    = 1;
    final int TYPE_3    = 2;

    public MyAdapter2(List<Integer> list, Context ctx) {
        super();
        this.list = list;
        this.ctx = ctx;
    }

    //  Get the type of View for the Item
    @Override
    public int getItemViewType(int position) {
        int i = position % VIEW_TYPE;
        if (i == 0) {
            return TYPE_1;
        } else if (i == 1) {
            return TYPE_2;
        } else {
            return TYPE_3;
        }
    }

    //  There are several layouts
    @Override
    public int getViewTypeCount() {
        return VIEW_TYPE;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder holder1 = null;
        MyViewHolder2 holder2 = null;
        MyViewHolder3 holder3 = null;
        int type = getItemViewType(position);
        if (convertView == null) {
            //Set layout and find ID
            switch (type) {
                case TYPE_1:
                    convertView = View.inflate(ctx, R.layout.adapter, null);
                    holder1 = new MyViewHolder(convertView);
                    convertView.setTag(holder1);
                    break;
                case TYPE_2:
                    convertView = View.inflate(ctx, R.layout.adapter2, null);
                    holder2 = new MyViewHolder2(convertView);
                    convertView.setTag(holder2);
                    break;
                case TYPE_3:
                    convertView = View.inflate(ctx, R.layout.adapter3, null);
                    holder3 = new MyViewHolder3(convertView);
                    convertView.setTag(holder3);
                    break;
            }
        } else {
            //Setup reuse
            switch (type) {
                case TYPE_1:
                    holder1 = (MyViewHolder) convertView.getTag();
                    break;

                case TYPE_2:
                    holder2 = (MyViewHolder2) convertView.getTag();
                    break;
                case TYPE_3:
                    holder3 = (MyViewHolder3) convertView.getTag();
                    break;
            }
        }
        //Setting data
        switch (type) {
            case TYPE_1:

                break;

            case TYPE_2:

                break;
            case TYPE_3:

                break;
        }
        return convertView;
    }

    private class MyViewHolder {
        public MyViewHolder(View convertView) {

        }
    }

    private class MyViewHolder2 {
        public MyViewHolder2(View convertView) {

        }
    }

    private class MyViewHolder3 {
        public MyViewHolder3(View convertView) {

        }
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
    RecyclerView mRecy;
    ListView     mLv;
    List<Integer> mList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initList();
        initView();

    }

    private void initList() {
        for (int i = 0; i < 100; i++) {
            mList.add(i);
        }
    }

    private void initView() {
        mRecy = findViewById(R.id.mRecy);
        mLv = findViewById(R.id.mLv);
        //        mRecy.setLayoutManager(new LinearLayoutManager(this));
        //        mRecy.setAdapter(new MyAdapter(this, mList));
        mLv.setAdapter(new MyAdapter2(mList,this));
    }
}

Design sketch:

Posted by DeX on Mon, 30 Mar 2020 11:49:03 -0700