有时候需要对一些数据进行分类显示,还要显示他们的分类标题,普通的GridView是挺难实现的,因为GridView不支持不同行自定义列数,例如我想标题占满一行,其他子项每两个占一行。GridView也可以实现,需要对GridView的方法做大量地重写override,重绘item等等,对一些像我这种菜鸟级别的人来说,门槛太高。后来发现可以使用RecyclerView实现这种功能,RecyclerView支持自定义不同列数。接下来介绍一下如何使用。
解释都写在代码里,直接看代码吧:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="标题"/>
LinearLayout>
recyclerview_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/image"
android:scaleType="fitXY"
android:alpha="0.7"
android:src="@drawable/ic_launcher_background"/>
<TextView
android:id="@+id/item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="40dp"
android:gravity="center"
android:text="这是一个item"
android:textColor="#ffffff"
android:textSize="20sp" />
RelativeLayout>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mrc.csdndemo.RecyclerViewCategory.RecyclerViewCategoryStyle">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:gravity="center"
android:horizontalSpacing="8dp"
android:verticalSpacing="8dp"/>/>
LinearLayout>
ScrollView>
package com.example.mrc.csdndemo.RecyclerViewCategory;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.mrc.csdndemo.R;
import java.util.List;
import java.util.Map;
/**
* Created by Mr.C on 2018/3/17.
*/
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
private Context mContext;
private LayoutInflater mInflater;
private static final int VIEW_TYPE_TITLE= 0;
private static final int VIEW_TYPE_ITEM = 1;
int IS_TITLE_OR_NOT =1;
int MESSAGE = 2;
int ColumnNum;
List
package com.example.mrc.csdndemo.RecyclerViewCategory;
import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import com.example.mrc.csdndemo.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class RecyclerViewCategoryStyle extends AppCompatActivity {
RecyclerView mRecyclerView ;
LinearLayoutManager mLayoutManager;
RecyclerViewAdapter mRecyclerViewAdapter;
int IS_TITLE_OR_NOT =1;
int MESSAGE = 2;
List> mData =new ArrayList<>();
Map map = new HashMap();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view_category_style);
findView();
init();
mRecyclerViewAdapter = new RecyclerViewAdapter(this, mData, 2);
mRecyclerView.setAdapter(mRecyclerViewAdapter);
}
void findView(){
mRecyclerView =(RecyclerView)findViewById(R.id.recyclerView);
//2表示列数为2,LinearLayoutManager.VERTICAL表示竖直布局
mLayoutManager = new GridLayoutManager(this, 2, LinearLayoutManager.VERTICAL, false);
mRecyclerView.addItemDecoration(new SpaceItemDecoration(12));//item之间的间距
mRecyclerView.setLayoutManager(mLayoutManager);
}
void init(){
//对item的数据进行初始化
for(int i=0;i<15 ;i++){
map = new HashMap();
map.put(IS_TITLE_OR_NOT , "false");
map.put(MESSAGE , "item "+(i+1));
mData.add(map);
}
//对分类标题进行初始化
map = new HashMap();
map.put(IS_TITLE_OR_NOT , "true");
map.put(MESSAGE , "1-3的分类标题");
mData.add(0,map);
map = new HashMap();
map.put(IS_TITLE_OR_NOT , "true");
map.put(MESSAGE , "4-7的分类标题");
mData.add( 4,map);
map = new HashMap();
map.put(IS_TITLE_OR_NOT , "true");
map.put(MESSAGE , "8-15的分类标题");
mData.add(9,map);
for (int i=0;i"Title",mData.get(i).get(IS_TITLE_OR_NOT));
Log.d("Title_message",mData.get(i).get(MESSAGE));
}
}
//设置recyclerView中item的上下左右间距
public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
private int space;
public SpaceItemDecoration(int space) {
this.space = space;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
//分别设置item的间距
if (parent.getChildViewHolder(view).getItemViewType() == 0) {
outRect.bottom = 0;
outRect.top = space / 2;
} else {
outRect.bottom = space;
outRect.top = space;
}
outRect.right = space;
outRect.left = space;
}
}
}
代码已经打包放在里面,看不懂的伙伴可以去下载下来研究研究:
RecyclerView有标题栏的分类列表 自定义列数(不同行设置不同个子项item)