Android开发经验谈Android开发Android技术知识

RecyclerView 分组实现(一)

2017-09-11  本文已影响1012人  tuionf

分组

分组效果

image

思路

  1. 数据部分可以通过内容或者标志位来区分是不是第一个,例子中通过标志位区分
  2. Adapter中通过getItemViewType方法来获取标志位,并返回int值用于区分
  3. onCreateViewHolder中通过区分 viewType 来加载不同的布局

实现

Activity中

  1. initData() 初始化数据,给标志位来区分数据
  2. 调用SectionItemDecoration中定义的接口实现数据传递
package com.example.tuionf.recyclerviewlearn;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class SectionActivity extends AppCompatActivity {

    private RecyclerView sectionRv;
    private List<SectionBean> mList = new ArrayList<>();

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

        initData();

        sectionRv = (RecyclerView) findViewById(R.id.section_rv);

        sectionRv.setLayoutManager(new LinearLayoutManager(this));
        SectionAdapter sectionAdapter = new SectionAdapter(this,mList);
        sectionRv.addItemDecoration(new SectionItemDecoration(this, new SectionItemDecoration.DecorationCallback() {
            @Override
            public boolean isGroupFirst(int position) {
                return mList.get(position).isFirst();
            }

            @Override
            public String getGroupFirstLine(int position) {
                return mList.get(position).getTag();
            }
        }));
        sectionRv.setAdapter(sectionAdapter);


    }

    private void initData() {
        for (int i = 0; i < 10; i++) {
            SectionBean sectionBean = new SectionBean();
            sectionBean.setText("--" + i);
            sectionBean.setTag("山西省");
            if (i == 0){
                sectionBean.setFirst(true);
            }else {
                sectionBean.setFirst(false);
            }
            mList.add(sectionBean);
        }

        for (int i = 0; i < 20; i++) {
            SectionBean sectionBean = new SectionBean();
            sectionBean.setText("--" + i);
            sectionBean.setTag("辽宁省");
            if (i == 0){
                sectionBean.setFirst(true);
            }else {
                sectionBean.setFirst(false);
            }
            mList.add(sectionBean);
        }

        for (int i = 0; i < 25; i++) {
            SectionBean sectionBean = new SectionBean();
            sectionBean.setText("--" + i);
            sectionBean.setTag("河南省");
            if (i == 0){
                sectionBean.setFirst(true);
            }else {
                sectionBean.setFirst(false);
            }
            mList.add(sectionBean);
        }

        for (int i = 0; i < 29; i++) {
            SectionBean sectionBean = new SectionBean();
            sectionBean.setText("--" + i);
            sectionBean.setTag("广东省");
            if (i == 0){
                sectionBean.setFirst(true);
            }else {
                sectionBean.setFirst(false);
            }
            mList.add(sectionBean);
        }

        for (int i = 0; i < 20; i++) {
            SectionBean sectionBean = new SectionBean();
            sectionBean.setText("--" + i);
            sectionBean.setTag("北京市");
            if (i == 0){
                sectionBean.setFirst(true);
            }else {
                sectionBean.setFirst(false);
            }
            mList.add(sectionBean);
        }

        for (int i = 0; i < 20; i++) {
            SectionBean sectionBean = new SectionBean();
            sectionBean.setText("--" + i);
            sectionBean.setTag("天津市");
            if (i == 0){
                sectionBean.setFirst(true);
            }else {
                sectionBean.setFirst(false);
            }
            mList.add(sectionBean);
        }

        for (int i = 0; i < 20; i++) {
            SectionBean sectionBean = new SectionBean();
            sectionBean.setText("--" + i);
            sectionBean.setTag("重庆市");
            if (i == 0){
                sectionBean.setFirst(true);
            }else {
                sectionBean.setFirst(false);
            }
            mList.add(sectionBean);
        }

        for (int i = 0; i < 19; i++) {
            SectionBean sectionBean = new SectionBean();
            sectionBean.setText("--" + i);
            sectionBean.setTag("山东省");
            if (i == 0){
                sectionBean.setFirst(true);
            }else {
                sectionBean.setFirst(false);
            }
            mList.add(sectionBean);
        }
    }


}

SectionAdapter.java中主要是 getItemViewType() 和 onCreateViewHolder()这两个方法

package com.example.tuionf.recyclerviewlearn;

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

import java.util.List;

/**
 * Created by tuion on 2017/9/7.
 */

public class SectionAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List<SectionBean> mList;

    public SectionAdapter(Context mContext, List<SectionBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @Override
    public int getItemViewType(int position) {
        int viewType;
        if (mList.get(position).isFirst()){
            viewType = 0; //这个是第一条
        }else {
            viewType = 1;
        }
        return viewType;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        MyViewHolder myViewHolder = null;
        switch(viewType){
            case 0:
                myViewHolder = new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item1,null));
            break;
            case 1:
                myViewHolder = new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item,null));
            break;
        }

        return  myViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        myViewHolder.textView.setText(mList.get(position).getText());
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder{

        TextView textView;
        public MyViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.tv);
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读