美文网首页RecyclerView
Recyclerview基本使用<6>——添加标题行

Recyclerview基本使用<6>——添加标题行

作者: 天空在微笑 | 来源:发表于2017-07-10 10:46 被阅读121次

    添加标题行

    添加标题的gridlayoutmanager.png
     //RecyclerView设置布局管理manager,一行有三列
            final GridLayoutManager manager = new GridLayoutManager(context, 4);
            manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    //标题占一行,其他占三分之一
                    if (position % 5 == 0) {
                        return 4;
                    }
                    return 1;
                }
            });
            list_recycler.setLayoutManager(manager);
    
    
        private GridLayoutManager gridLayoutManager;
        private TestAdapter mAdapter;
        private List<String> mDatas;
        public void setImgRecyclerViewData() {
           mImgRecyclerView = (RecyclerView) findViewById(R.id.id_img_recyclerview);
           mDatas = new ArrayList<String>();
            for (int i = 1; i <20; i++)
            {
                mDatas.add(String.valueOf(i));
            }
            //设置布局管理器
            gridLayoutManager =new GridLayoutManager(this,4);
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    Log.v("TAG", "position:"+position);
                    Log.v("TAG", "position % 5:"+(position % 5));
                    if (position % 5 == 0) {
                        return 4;
                    }else{
                        return 1;
                    }
                }
            });
            mImgRecyclerView.setLayoutManager(gridLayoutManager);
            //设置Item增加、移除动画
            mImgRecyclerView.setItemAnimator(new DefaultItemAnimator());
            //设置adapter
            mImgRecyclerView.setAdapter(mAdapter = new TestAdapter());
        }
    
    
       class TestAdapter extends RecyclerView.Adapter<TestAdapter.MyViewHolder>
        {
    
            @Override
            public int getItemViewType(int position) {
                if (position % 5 == 0) {
                    return 0;
                }else{
                    return 1;
                }
            }
    
            @Override
            public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
            {
                MyViewHolder holder = null;
                if (viewType == 0) {
                    holder = new MyViewHolder(LayoutInflater.from(
                            DoctorInquiryNewActivity.this).inflate(R.layout.item_text, parent,
                            false));
    
                } else if (viewType == 1) {
                    holder = new MyViewHolder(LayoutInflater.from(
                            DoctorInquiryNewActivity.this).inflate(R.layout.item_img, parent,
                            false));
                }
                return holder;
            }
    
            @Override
            public void onBindViewHolder(MyViewHolder holder, int position)
            {
    //            holder.tv.setText(mDatas.get(position));
            }
    
            @Override
            public int getItemCount()
            {
                return mDatas.size();
            }
    
            class MyViewHolder extends RecyclerView.ViewHolder
            {
    
                //            TextView tv;
    //
                public MyViewHolder(View view)
                {
                    super(view);
    //                tv = (TextView) view.findViewById(R.id.id_num);
                }
            }
        }
    
      <android.support.v7.widget.RecyclerView
                    android:id="@+id/id_img_recyclerview"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingLeft="20dp"
                    android:paddingRight="15dp"
                    />
    
    
    item_text.xml
    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        >
        <TextView
        style="@style/item_text_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:gravity="left"
        android:text="(全图)"
        android:textColor="@color/gray2"
        android:textSize="15sp"
        android:visibility="visible" />
    </FrameLayout>
    
    item_img.xml
    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="91dp"
        >
        <ImageView
            android:id="@+id/id_iv"
            android:layout_width="73dp"
            android:layout_height="73dp"
            android:layout_gravity="left|center_vertical"
            android:gravity="center"
            android:src="@mipmap/default_square"
             />
        <TextView
            android:id="@+id/id_tv"
            android:layout_width="18dp"
            android:layout_height="18dp"
            android:textSize="13sp"
            android:layout_gravity="left|top"
            android:layout_marginLeft="64dp"
            android:gravity="center"
            android:background="@drawable/shape_bage_gray"
            android:textColor="@android:color/white"
            android:text="1" />
    </FrameLayout>
    
    

    相关文章

      网友评论

        本文标题:Recyclerview基本使用<6>——添加标题行

        本文链接:https://www.haomeiwen.com/subject/kesyhxtx.html