android gridview布局添加多个title

在项目中要求实现在gridview的布局效果上添加title,并且是多个在不同地方的title,每个title占据一整行,下面分享一下如何实现

先上效果图:
android gridview布局添加多个title_第1张图片

1.首先添加title到list集合中:


 List ls;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView rl = (RecyclerView) findViewById(R.id.myrv);
        final GridLayoutManager manager = new GridLayoutManager(this,2);
        rl.setLayoutManager(manager);

        ls = new ArrayList<>();
        for(int i = 0 ; i< 100 ; i ++){
            ls.add(i + "");
        }
        MyAdapter myAdapter = new MyAdapter();
        myAdapter.addTitle(7,"呵呵哒");
        myAdapter.addTitle(11,"萌萌哒");
        myAdapter.addTitle(20,"饿饿哒");
        rl.setAdapter(myAdapter);


    }

2.在recyclerview复写的oncreateviewholder判断类型,是否是title,是的话就创建title的自定义view

        public SparseArray titles = new SparseArray<>();
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if(isTitle(viewType)){
                View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_title,parent,false);
                TitleViewHolder titleViewHolder = new TitleViewHolder(v);
                return titleViewHolder;

            }else{
                View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_test,parent,false);
                MyViewHolder myViewHolder = new MyViewHolder(v);
                return myViewHolder;
            }


        }

3.复写adapter中的getviewtype方法

        @Override
        public int getItemViewType(int position) {
            if(isTitle(position)){
                return position;
            }
            return super.getItemViewType(position);
        }

4.复写onbindviewholder,对不同类型的view进行绑定数据操作

@Override

        public void onBindViewHolder(RecyclerView.ViewHolder holder,int position) {
            if(isTitle(position)){
                TitleViewHolder titleViewHolder = (TitleViewHolder) holder;
                titleViewHolder.tv.setText(titles.get(position));
                return;
            }
            //获取空过去的item(关键)
            for(int i = 0; i < titles.size(); i++){
                int key = titles.keyAt(i);
                if(position > titles.keyAt(titles.size() - 1)){
                    position -= titles.size();
                    break;
                }else if(position > key && position < titles.keyAt(i + 1)){
                    position -= (i+1);
                    break;
                }

            }
            final int s = position;
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            myViewHolder.tv.setText(ls.get(s));

            myViewHolder.tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(MainActivity.this,"您点击了" + s,Toast.LENGTH_SHORT).show();
                }
            });
        }

5.复写getitemcount方法重新负值item数量

        public int getItemCount() {
            return (ls.size() + titles.size());
        }

6.添加两个方法用于判断是否是title,和添加title方法

public boolean isTitle(int position){
           return titles.get(position) == null ? false:true;
        }
        public void addTitle(int position,String title){
            titles.put(position,title);
        }

7.重点,对title类型进行分配两个单元格处理

 @Override
        public void onAttachedToRecyclerView(RecyclerView recyclerView) {
            //如果是title就占据2个单元格(重点)
            GridLayoutManager manager = (GridLayoutManager) recyclerView.getLayoutManager();
            manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    if(isTitle(position)){
                        return 2;
                    }
                    return 1;
                }
            });
        }

到此为止就完成了一个可以任意添加title的recyclerview

代码已经上传到github点击查看,如果觉得不错start一下下,有问题可以直接联系我邮箱地址:[email protected],欢迎交流

你可能感兴趣的:(android)