Android?RecyclerView實(shí)現(xiàn)九宮格效果
RecyclerView更加優(yōu)化的復(fù)用機(jī)制和方便實(shí)現(xiàn)UI效果,幾乎替代Listview和GridView的使用。但是分割線的實(shí)現(xiàn),需要自己繼承ItemDecoration來(lái)繪制。
效果圖

item的布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:orientation="vertical" ? ? android:gravity="center" ? ? android:layout_width="match_parent" ? ? android:layout_height="wrap_content"> ? ? <LinearLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:orientation="horizontal" ? ? ? ? android:gravity="center_vertical" ? ? ? ? android:layout_marginTop="25dp" ? ? ? ? android:layout_marginLeft="15dp" ? ? ? ? android:layout_marginRight="15dp"> ? ? ? ? <TextView ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:text="餐飲" ? ? ? ? ? ? android:id="@+id/txt_title" ? ? ? ? ? ? android:textSize="16sp" ? ? ? ? ? ? android:textStyle="bold" ? ? ? ? ? ? android:textColor="#555555"/> ? ? ? ? <ImageView ? ? ? ? ? ? android:layout_width="36dp" ? ? ? ? ? ? android:layout_height="36dp" ? ? ? ? ? ? android:id="@+id/img_title" ? ? ? ? ? ? android:src="@mipmap/luggage_blue"/> ? ? </LinearLayout> ? ? <LinearLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="20dp" ? ? ? ? android:orientation="vertical" ? ? ? ? android:layout_marginBottom="25dp" ? ? ? ? android:layout_marginLeft="15dp" ? ? ? ? android:layout_marginRight="15dp"> ? ? ? ? <TextView ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? android:text="提供航空 餐飲美食" ? ? ? ? ? ? android:id="@+id/txt_info" ? ? ? ? ? ? android:textSize="14sp" ? ? ? ? ? ? android:textColor="#999999"/> ? ? </LinearLayout> </LinearLayout>
activity_main.xml布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:orientation="vertical" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:background="#f1f1f1" ? ? tools:context=".MainActivity"> ? ? <TextView ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="50dp" ? ? ? ? android:textColor="#ffffff" ? ? ? ? android:textSize="18sp" ? ? ? ? android:gravity="center" ? ? ? ? android:text="RecyclerView實(shí)現(xiàn)九宮格" ? ? ? ? android:background="#30B8E3"/> ? ? <ScrollView ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent"> ? ? ? ? <LinearLayout ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:orientation="vertical"> ? ? ? ? ? ? <LinearLayout ? ? ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? ? ? android:layout_height="50dp" ? ? ? ? ? ? ? ? android:gravity="center_vertical" ? ? ? ? ? ? ? ? android:layout_marginLeft="15dp" ? ? ? ? ? ? ? ? android:layout_marginRight="15dp" ? ? ? ? ? ? ? ? android:orientation="horizontal"> ? ? ? ? ? ? ? ? <ImageView ? ? ? ? ? ? ? ? ? ? android:layout_width="20dp" ? ? ? ? ? ? ? ? ? ? android:layout_height="20dp" ? ? ? ? ? ? ? ? ? ? android:src="@mipmap/air_gray" ? ? ? ? ? ? ? ? ? ? android:layout_marginRight="8dp"/> ? ? ? ? ? ? ? ? <TextView ? ? ? ? ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? ? ? android:text="航行助手" ? ? ? ? ? ? ? ? ? ? android:textStyle="bold" ? ? ? ? ? ? ? ? ? ? android:textSize="18sp"/> ? ? ? ? ? ? </LinearLayout> ? ? ? ? ? ? <android.support.v7.widget.RecyclerView ? ? ? ? ? ? ? ? android:id="@+id/main_recycleview" ? ? ? ? ? ? ? ? android:divider="#00000000" ? ? ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:background="@drawable/shape_bg" ? ? ? ? ? ? ? ? android:layout_marginLeft="15dp" ? ? ? ? ? ? ? ? android:layout_marginRight="15dp" ? ? ? ? ? ? ? ? android:layout_marginBottom="15dp"> ? ? ? ? ? ? </android.support.v7.widget.RecyclerView> ? ? ? ? </LinearLayout> ? ? </ScrollView> </LinearLayout>
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 List<MenuBean> listDatas = new ArrayList<MenuBean>();
? ? @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("行李", "提醒行李動(dòng)態(tài)", R.mipmap.luggage_blue));
? ? ? ? listDatas.add(new MenuBean("餐飲", "提供航空 餐飲美食", R.mipmap.food_blue));
? ? ? ? listDatas.add(new MenuBean("VIP休息", "機(jī)場(chǎng)休息室", R.mipmap.vip_blue));
? ? ? ? listDatas.add(new MenuBean("機(jī)艙服務(wù)", "機(jī)艙上網(wǎng) 游戲娛樂(lè)", R.mipmap.service_blue));
? ? ? ? listDatas.add(new MenuBean("更多", "更多信息", R.mipmap.more_blue));
? ? }
}其中GridLayoutManager用來(lái)設(shè)置顯示列數(shù),CommonDecoration用來(lái)繪制分隔線。
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) {
? ? ? ? // 列數(shù)
? ? ? ? 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 且縱向滾動(dòng)
? ? ? ? ? ? if (orientation == StaggeredGridLayoutManager.VERTICAL) {
? ? ? ? ? ? ? ? int last = childCount % spanCount;
? ? ? ? ? ? ? ? if (last == 0) {
? ? ? ? ? ? ? ? ? ? last = spanCount;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? childCount = childCount - last;
? ? ? ? ? ? ? ? // 如果是最后一行,則不需要繪制底部
? ? ? ? ? ? ? ? if (pos >= childCount) {
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? } else {// StaggeredGridLayoutManager 且橫向滾動(dòng)
? ? ? ? ? ? ? ? // 如果是最后一行,則不需要繪制底部
? ? ? ? ? ? ? ? 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());
? ? ? ? }
? ? }
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 利用三階貝塞爾曲線繪制運(yùn)動(dòng)軌跡的示例
本篇文章主要介紹了Android 利用三階貝塞爾曲線繪制運(yùn)動(dòng)軌跡的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
利用SurfaceView實(shí)現(xiàn)下雨與下雪動(dòng)畫(huà)效果詳解(Kotlin語(yǔ)法)
這篇文章主要給大家介紹了關(guān)于利用SurfaceView實(shí)現(xiàn)下雨與下雪動(dòng)畫(huà)效果的相關(guān)資料,需要一些基本的View知識(shí)和會(huì)一些基礎(chǔ)Kotlin語(yǔ)法,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09
Android編程自定義View時(shí)添加自己的監(jiān)聽(tīng)器示例
這篇文章主要介紹了Android編程自定義View時(shí)添加自己的監(jiān)聽(tīng)器,涉及Android自定義view中監(jiān)聽(tīng)器的添加、設(shè)置與使用相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
詳解Android沉浸式實(shí)現(xiàn)兼容解決辦法
本篇文章主要介紹了詳解Android沉浸式實(shí)現(xiàn)兼容解決辦法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
Android RecycleView和線型布局制作聊天布局
大家好,本篇文章主要講的是Android RecycleView和線型布局制作聊天布局,感興趣的同學(xué)趕緊來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
Android基于hover組件實(shí)現(xiàn)監(jiān)控鼠標(biāo)移動(dòng)事件的方法
這篇文章主要介紹了Android基于hover組件實(shí)現(xiàn)監(jiān)控鼠標(biāo)移動(dòng)事件的方法,結(jié)合實(shí)例形式分析了hover組件監(jiān)控鼠標(biāo)光標(biāo)在view上變化的操作技巧,需要的朋友可以參考下2017-02-02
Android系統(tǒng)進(jìn)程間通信(IPC)機(jī)制Binder中的Server啟動(dòng)過(guò)程源代碼分析
本文主要介紹Android IPC機(jī)制Binder中的Server啟動(dòng)過(guò)程源代碼,這里對(duì)Binder 中Server 啟動(dòng)過(guò)程中的源碼做了詳細(xì)的介紹,有研究Android源碼 Binder 通信的小伙伴可以參考下2016-08-08

