RecyclerView更加优化的复用机制和方便实现UI效果,几乎替代Listview和GridView的使用。但是分割线的实现,需要自己继承ItemDecoration来绘制。
效果图
item的布局文件
activity_main.xml布局文件
MainActivity.java代码
package com.davis.recyclerviewdemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import com.davis.recyclerviewdemo.adapter.CommonDecoration; import com.davis.recyclerviewdemo.adapter.RecyclerViewAdapter; import com.davis.recyclerviewdemo.bean.MenuBean; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private RecyclerViewAdapter adapter; private ListlistDatas = new ArrayList (); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init(){ recyclerView = (RecyclerView)findViewById(R.id.main_recycleview); loadMenuData(); recyclerView.setLayoutManager(new GridLayoutManager(this, 2)); recyclerView.addItemDecoration(new CommonDecoration(this)); adapter = new RecyclerViewAdapter(this, listDatas); recyclerView.setAdapter(adapter); } private void loadMenuData(){ listDatas.add(new MenuBean("安检", "快速安检", R.mipmap.check_blue)); listDatas.add(new MenuBean("行李", "提醒行李动态", R.mipmap.luggage_blue)); listDatas.add(new MenuBean("餐饮", "提供航空 餐饮美食", R.mipmap.food_blue)); listDatas.add(new MenuBean("VIP休息", "机场休息室", R.mipmap.vip_blue)); listDatas.add(new MenuBean("机舱服务", "机舱上网 游戏娱乐", R.mipmap.service_blue)); listDatas.add(new MenuBean("更多", "更多信息", R.mipmap.more_blue)); } }
其中GridLayoutManager用来设置显示列数,CommonDecoration用来绘制分隔线。
CommonDecoration.java代码
package com.davis.recyclerviewdemo.adapter; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.view.View; /** * Created by Administrator on 2019/4/14. */ public class CommonDecoration extends RecyclerView.ItemDecoration { private static final int[] ATTRS = new int[]{android.R.attr.listDivider}; private Drawable mDivider; public CommonDecoration(Context context) { final TypedArray a = context.obtainStyledAttributes(ATTRS); mDivider = a.getDrawable(0); a.recycle(); } @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { drawHorizontal(c, parent); drawVertical(c, parent); } private int getSpanCount(RecyclerView parent) { // 列数 int spanCount = -1; RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); if (layoutManager instanceof GridLayoutManager) { spanCount = ((GridLayoutManager) layoutManager).getSpanCount(); } else if (layoutManager instanceof StaggeredGridLayoutManager) { spanCount = ((StaggeredGridLayoutManager) layoutManager) .getSpanCount(); } return spanCount; } public void drawHorizontal(Canvas c, RecyclerView parent) { int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child .getLayoutParams(); final int left = child.getLeft() - params.leftMargin; final int right = child.getRight() + params.rightMargin + mDivider.getIntrinsicWidth(); final int top = child.getBottom() + params.bottomMargin; final int bottom = top + mDivider.getIntrinsicHeight(); mDivider.setBounds(left, top, right, bottom); mDivider.draw(c); } } public void drawVertical(Canvas c, RecyclerView parent) { final int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child .getLayoutParams(); final int top = child.getTop() - params.topMargin; final int bottom = child.getBottom() + params.bottomMargin; final int left = child.getRight() + params.rightMargin; final int right = left + mDivider.getIntrinsicWidth(); mDivider.setBounds(left, top, right, bottom); mDivider.draw(c); } } private boolean isLastColum(RecyclerView parent, int pos, int spanCount, int childCount) { RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); if (layoutManager instanceof GridLayoutManager) { // 如果是最后一列,则不需要绘制右边 if ((pos + 1) % spanCount == 0) { return true; } } else if (layoutManager instanceof StaggeredGridLayoutManager) { int orientation = ((StaggeredGridLayoutManager) layoutManager) .getOrientation(); if (orientation == StaggeredGridLayoutManager.VERTICAL) { // 如果是最后一列,则不需要绘制右边 if ((pos + 1) % spanCount == 0) { return true; } } else { childCount = childCount - childCount % spanCount; if (pos >= childCount) {// 如果是最后一列,则不需要绘制右边 return true; } } } return false; } private boolean isLastRaw(RecyclerView parent, int pos, int spanCount, int childCount) { RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); if (layoutManager instanceof GridLayoutManager) { int last = childCount % spanCount; if (last == 0) { last = spanCount; } childCount = childCount - last; if (pos >= childCount) {// 如果是最后一行,则不需要绘制底部 return true; } } else if (layoutManager instanceof StaggeredGridLayoutManager) { int orientation = ((StaggeredGridLayoutManager) layoutManager) .getOrientation(); // StaggeredGridLayoutManager 且纵向滚动 if (orientation == StaggeredGridLayoutManager.VERTICAL) { int last = childCount % spanCount; if (last == 0) { last = spanCount; } childCount = childCount - last; // 如果是最后一行,则不需要绘制底部 if (pos >= childCount) { return true; } } else {// StaggeredGridLayoutManager 且横向滚动 // 如果是最后一行,则不需要绘制底部 if ((pos + 1) % spanCount == 0) { return true; } } } return false; } @Override public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) { int spanCount = getSpanCount(parent); int childCount = parent.getAdapter().getItemCount(); if (isLastColum(parent, itemPosition, spanCount, childCount)) {// 如果是最后一列,则不需要绘制右边 if (itemPosition == (childCount - 1)) { outRect.set(0, 0, 0, 0); } else { outRect.set(0, 0, 0, mDivider.getIntrinsicHeight()); } } else if (isLastRaw(parent, itemPosition, spanCount, childCount)) {// 如果是最后一行,则不需要绘制底部 outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0); } else { outRect.set(0, 0, mDivider.getIntrinsicWidth(), mDivider.getIntrinsicHeight()); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。