<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
RecyclerView更加優化的複用機制和方便實現UI效果,幾乎替代Listview和GridView的使用。但是分割線的實現,需要自己繼承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實現九宮格" 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("行李", "提醒行李動態", 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()); } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45