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

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ī)場休息室", R.mipmap.vip_blue));
? ? ? ? listDatas.add(new MenuBean("機(jī)艙服務(wù)", "機(jī)艙上網(wǎng) 游戲娛樂", R.mipmap.service_blue));
? ? ? ? listDatas.add(new MenuBean("更多", "更多信息", R.mipmap.more_blue));
? ? }
}其中GridLayoutManager用來設(shè)置顯示列數(shù),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) {
? ? ? ? // 列數(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());
? ? ? ? }
? ? }
}以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Windows實(shí)現(xiàn)Flutter環(huán)境搭建及配置這一篇就夠了
這篇文章主要介紹了Windows實(shí)現(xiàn)Flutter環(huán)境搭建及配置這一篇就夠了,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Windows下快速搭建安卓開發(fā)環(huán)境Android studio
這篇文章主要介紹了Windows下快速搭建安卓開發(fā)環(huán)境Android studio的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07
Android Compose Column列表不自動(dòng)刷新問題
這篇文章主要介紹了Android Compose Column列表數(shù)據(jù)更新列表不刷新的問題,總的來說這并不是一道難題,那為什么要拿出這道題介紹?拿出這道題真正想要傳達(dá)的是解題的思路,以及不斷優(yōu)化探尋最優(yōu)解的過程。希望通過這道題能給你帶來一種解題優(yōu)化的思路2023-01-01
利用Android中的TextView實(shí)現(xiàn)逐字顯示動(dòng)畫
在安卓程序啟動(dòng)的時(shí)候,想逐字顯示一段話,每個(gè)字都有一個(gè)從透明到不透明的漸變動(dòng)畫。那如何顯示這個(gè)效果,下面一起來看看。2016-08-08
Android開發(fā)listview選中高亮簡單實(shí)現(xiàn)代碼分享
這篇文章主要介紹了Android開發(fā)listview選中高亮簡單實(shí)現(xiàn)代碼分享,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
Android ListView的item中嵌套ScrollView的解決辦法
有時(shí)候,listview 的item要顯示的字段比較多,考慮到顯示問題,item外面不得不嵌套ScrollView來實(shí)現(xiàn),糾結(jié)怎么解決此問題呢?下面小編給大家分享下Android ListView的item中嵌套ScrollView的解決辦法,感興趣的朋友一起看看吧2016-10-10

