-
Recyclerview 認(rèn)識(shí)
- Recyclerview 庫(kù)
- Recyclerview 指南
- RecyclerView 可以顯示大型數(shù)據(jù)集,通過(guò)回收有限數(shù)量的視圖可以有效地滾動(dòng)這些數(shù)據(jù)集座哩,同時(shí)最大限度減少內(nèi)存用量投放。 在實(shí)例化 ViewHolder 視圖時(shí)可以定義單擊偵聽(tīng)器皮仁。
- RecyclerView 庫(kù)會(huì)根據(jù)需要?jiǎng)討B(tài)創(chuàng)建元素。顧名思義,RecyclerView 會(huì)回收這些單個(gè)的元素打毛。當(dāng)列表項(xiàng)滾動(dòng)出屏幕時(shí),RecyclerView 不會(huì)銷(xiāo)毀其視圖俩功。相反幻枉,RecyclerView 會(huì)對(duì)屏幕上滾動(dòng)的新列表項(xiàng)重用該視圖。這種重用可以顯著提高性能诡蜓,改善應(yīng)用響應(yīng)能力并降低功耗熬甫。* 使用:在應(yīng)用或模塊的 build.gradle 文件中添加所需工件的依賴(lài)項(xiàng):
dependencies { implementation "androidx.recyclerview:recyclerview:1.2.1" }
- 第二步:定義頁(yè)面布局 (activity_out_in.xml)
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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=".ui.warehouse.OutInActivity"> <!-- 省略其他布局內(nèi)容 ,保留 RecyclerView --> <!-- 標(biāo)題 --> <!-- 條件查詢部分 --> <!-- 內(nèi)容 --> <androidx.recyclerview.widget.RecyclerView android:id="@+id/OutIn_page_recyclerview" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/card_OutIn_page_query" /> </androidx.constraintlayout.widget.ConstraintLayout> 復(fù)制代碼
- 第三步:定義RecyclerView每一項(xiàng)(Item)頁(yè)面(recyclerview_ware_item.xml)
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content"> <androidx.cardview.widget.CardView android:id="@+id/ware_card" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/f_mp_10_lr" android:layout_marginTop="5dp" android:layout_marginRight="@dimen/f_mp_10_lr" android:layout_marginBottom="5dp" app:cardCornerRadius="5dp" app:cardElevation="5dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="@dimen/f_mp_20_lr"> <!-- 通過(guò)預(yù)設(shè)尺寸蔓罚,指定文字大小椿肩。注意: layout_height瞻颂、layout_width這兩個(gè)屬性,不同的用法有意想不到的驚喜 --> <!-- app:autoSizeText 屬性設(shè)置為 none 或 uniform郑象。none 是默認(rèn)值贡这,而 uniform 可讓 TextView 在水平和垂直軸上均勻縮放。 --> <!-- app:autoSizePresetSizes="@array/auto_size_text_sizes" 屬性設(shè)置為預(yù)設(shè)尺寸數(shù)組厂榛。如要將該數(shù)組作為資源來(lái)訪問(wèn)盖矫,請(qǐng)?jiān)?res/values/arrays.xml 文件中定義該數(shù)組。 --> <TextView android:id="@+id/tvIt_material_name" android:layout_width="wrap_content" android:layout_height="25dp" android:gravity="center|left" android:maxWidth="220dp" android:minWidth="160dp" android:text="物品測(cè)試的測(cè)試測(cè)試" app:autoSizePresetSizes="@array/auto_size_text_sizes" app:autoSizeTextType="uniform" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/tvIt_number_name" android:layout_width="60dp" android:layout_height="25dp" android:gravity="center|left" android:text="出庫(kù)數(shù)量:" app:autoSizePresetSizes="@array/auto_size_text_sizes" app:autoSizeTextType="uniform" app:layout_constraintEnd_toStartOf="@+id/tvIT_unit_num" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/tvIT_unit_num" android:layout_width="55dp" android:layout_height="25dp" android:gravity="center|right" android:text="1008/只" app:autoSizePresetSizes="@array/auto_size_text_sizes" app:autoSizeTextType="uniform" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/tvIT_date_name" android:layout_width="60dp" android:layout_height="25dp" android:layout_marginTop="5dp" android:gravity="center|left" android:text="出庫(kù)日期:" app:autoSizePresetSizes="@array/auto_size_text_sizes" app:autoSizeTextType="uniform" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tvIt_material_name" /> <TextView android:id="@+id/tvIT_date" android:layout_width="wrap_content" android:layout_height="25dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:gravity="center" android:text="2022-10-11" app:layout_constraintStart_toEndOf="@+id/tvIT_date_name" app:layout_constraintTop_toBottomOf="@+id/tvIt_material_name" /> <TextView android:id="@+id/tvIT_ware_status_name" android:layout_width="60dp" android:layout_height="25dp" android:layout_marginTop="5dp" android:gravity="center|left" android:text="出庫(kù)狀態(tài):" app:autoSizePresetSizes="@array/auto_size_text_sizes" app:autoSizeTextType="uniform" app:layout_constraintEnd_toStartOf="@+id/tvIT_ware_status" app:layout_constraintTop_toBottomOf="@+id/tvIt_number_name" /> <TextView android:id="@+id/tvIT_ware_status" android:layout_width="55dp" android:layout_height="25dp" android:layout_marginTop="5dp" android:gravity="center" android:text="銷(xiāo)售" app:autoSizePresetSizes="@array/auto_Item_size" app:autoSizeTextType="uniform" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/tvIT_unit_num" /> </androidx.constraintlayout.widget.ConstraintLayout> <TextView android:id="@+id/tvIT_ware_out_in" android:layout_width="50dp" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_marginTop="6dp" android:background="@color/blue" android:gravity="center" android:paddingLeft="5dp" android:paddingRight="5dp" android:rotation="45" android:text="出庫(kù)" android:textColor="@color/white" android:textSize="10sp" android:translationX="12dp" android:translationZ="@dimen/f_mp_10_lr" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.cardview.widget.CardView> </androidx.constraintlayout.widget.ConstraintLayout>
第四步 :創(chuàng)建RecyclerView適配器 噪沙,實(shí)現(xiàn) Adapter 和 ViewHolder
ViewHolder 是包含列表中各列表項(xiàng)的布局的 View 的封裝容器炼彪。Adapter 會(huì)根據(jù)需要?jiǎng)?chuàng)建 ViewHolder 對(duì)象,還會(huì)為這些視圖設(shè)置數(shù)據(jù)正歼。將視圖與其數(shù)據(jù)相關(guān)聯(lián)的過(guò)程稱(chēng)為“綁定”辐马。
-
定義 Adapter,需要重寫(xiě)以下三個(gè)方法
-
onCreateViewHolder()
:每當(dāng) RecyclerView 需要?jiǎng)?chuàng)建新的 ViewHolder 時(shí),它都會(huì)調(diào)用此方法局义。此方法會(huì)創(chuàng)建并初始化 ViewHolder 及其關(guān)聯(lián)的 View喜爷,但不會(huì)填充視圖的內(nèi)容,因?yàn)?ViewHolder 此時(shí)尚未綁定到具體數(shù)據(jù)萄唇。 -
onBindViewHolder()
:RecyclerView 調(diào)用此方法將 ViewHolder 與數(shù)據(jù)相關(guān)聯(lián)檩帐。此方法會(huì)提取適當(dāng)?shù)臄?shù)據(jù),并使用該數(shù)據(jù)填充 ViewHolder 的布局另萤。 -
getItemCount()
:RecyclerView 調(diào)用此方法來(lái)獲取數(shù)據(jù)集的大小湃密。
//數(shù)據(jù)源 實(shí)體類(lèi)對(duì)象 public class TestData { private int testId; private String testName; private String testNum; private String testDate; private String testStatus; private String testOutIn; //構(gòu)造方法 。getter 和 setter 方法省略不寫(xiě)(不展示出來(lái)) }
import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.List; //部分涉及 項(xiàng)目包名 導(dǎo)入(沒(méi)寫(xiě)出來(lái)) //創(chuàng)建 RecyclerView 適配器 四敞。繼承于RecyclerView.Adapter<VH>泛源,其中VH是我們創(chuàng)建的一個(gè)繼承于RecyclerView.ViewHolder的靜態(tài)內(nèi)部類(lèi) public class WareRecyclerViewAdapter extends RecyclerView.Adapter<WareRecyclerViewAdapter.myView> { private List<TestData> dataList; // 數(shù)據(jù)源 private Context context;//上下文 private myItemOnClick myItemOnClick ; //第一步:創(chuàng)建 myItemOnClick 接口 public interface myItemOnClick { void onItemClick(int position); } //聲明給外界方法 public void setItemOnClick(myItemOnClick myItemOnClick) { this.myItemOnClick = myItemOnClick; } public WareRecyclerViewAdapter(Context context, List<TestData> dataList) { this.dataList = dataList; this.context = context; } //初始化item布局 @NonNull @Override public myView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { // View view = View.inflate(context, R.layout.recyclerview_ware_item, null); //寫(xiě)法一 //寫(xiě)法二 解決item寬度不能鋪滿 View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_ware_item, parent, false); return new myView(view); } //綁定元素的數(shù)據(jù) @Override public void onBindViewHolder(@NonNull myView holder, int position) { holder.itMaterial.setText(dataList.get(position).getTestName()); holder.itUnitNum.setText(dataList.get(position).getTestNum()); holder.itDate.setText(dataList.get(position).getTestDate()); holder.itStatus.setText(dataList.get(position).getTestStatus()); holder.itWareOutIn.setText(dataList.get(position).getTestOutIn()); String OutIn=dataList.get(position).getTestOutIn(); if (OutIn.equals("出庫(kù)")) { holder.itNumber.setText(context.getString(R.string.OutNumber) + ":"); holder.itDateName.setText(context.getString(R.string.OutDate) + ":"); holder.itStatusName.setText(context.getString(R.string.OutStatus) + ":"); holder.itWareOutIn.setBackgroundColor(context.getResources().getColor(R.color.aurantius));//右上角標(biāo)識(shí) holder.itDate.setTextColor(context.getResources().getColor(R.color.aurantius));//出庫(kù)日期 holder.itStatus.setTextColor(context.getResources().getColor(R.color.aurantius));//出庫(kù)用途 } if (OutIn.equals("入庫(kù)")) { holder.itNumber.setText(context.getString(R.string.InNumber) + ":"); holder.itDateName.setText(context.getString(R.string.InDate) + ":"); holder.itStatusName.setText(context.getString(R.string.InStatus) + ":"); holder.itWareOutIn.setBackgroundColor(context.getResources().getColor(R.color.green));//右上角標(biāo)識(shí) holder.itDate.setTextColor(context.getResources().getColor(R.color.green));//入庫(kù)日期 holder.itStatus.setTextColor(context.getResources().getColor(R.color.green));//入庫(kù)狀態(tài) } //給每一項(xiàng)item 添加點(diǎn)擊事件 //實(shí)際上的觸發(fā)的點(diǎn)擊事件 if (myItemOnClick!=null) { holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { myItemOnClick.onItemClick(holder.getLayoutPosition()); } }); } } /*獲取數(shù)據(jù)的總數(shù)據(jù)量*/ @Override public int getItemCount() { return dataList == null ? 0 : dataList.size(); } //獲取元素 public class myView extends RecyclerView.ViewHolder { private TextView itMaterial, itNumber, itUnitNum, itDateName, itDate, itStatusName, itStatus, itWareOutIn, itRepair1,itRepair2,itRepair3,itRepair4,itRepair5,itRepair6,itRepair7,itRepair8; private ConstraintLayout repair_constraint; public myView(@NonNull View itemView) { super(itemView); itMaterial = itemView.findViewById(R.id.tvIt_material_name);//物品名稱(chēng) itNumber = itemView.findViewById(R.id.tvIt_number_name); //出入庫(kù)數(shù)量 itUnitNum = itemView.findViewById(R.id.tvIT_unit_num);//具體數(shù)據(jù)+單位 itDateName = itemView.findViewById(R.id.tvIT_date_name);//出入庫(kù)日期 itDate = itemView.findViewById(R.id.tvIT_date);//具體日期 itStatusName = itemView.findViewById(R.id.tvIT_ware_status_name);//出入庫(kù)狀態(tài) itStatus = itemView.findViewById(R.id.tvIT_ware_status);//出入庫(kù)狀態(tài)值 itWareOutIn = itemView.findViewById(R.id.tvIT_ware_out_in);//出入庫(kù)標(biāo)識(shí) } } }
- 第五步:使用RecyclerView的適配器:
recycleView.setAdapter(adapter)
-
-
在 OutInActivity 中 使用 RecyclerView的適配器,添加數(shù)據(jù)生成列表
import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import java.util.ArrayList; import java.util.List; // 部分涉及 項(xiàng)目包名 導(dǎo)入(沒(méi)寫(xiě)出來(lái)) /** * @esc: 出入庫(kù)明細(xì)頁(yè)面 * @Author: xiaozhao * @CreateDate: 2022-10-23 11:18 */ public class OutInActivity extends AppCompatActivity { private Activity myActivity; private RecyclerView setOutInPageR; private WareRecyclerViewAdapter myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { myActivity = OutInActivity.this; super.onCreate(savedInstanceState); setContentView(R.layout.activity_out_in); initView(); } //初始頁(yè)面 控件 private void initView() { setOutInPageR = findViewById(R.id.OutIn_page_recyclerview); setOutInAdapter(); } // 創(chuàng)建 RecyclerView 適配器 private void setOutInAdapter() { //setOutInR.setLayoutManager(new LinearLayoutManager(myActivity)); //設(shè)置線性布局忿危,//網(wǎng)格布局GridLayoutManager //瀑布流網(wǎng)格布局StaggeredGridLayoutManager LinearLayoutManager layoutManager = new LinearLayoutManager(myActivity, RecyclerView.VERTICAL, false); setOutInPageR.setLayoutManager(layoutManager); // 設(shè)置每一項(xiàng) item 的分割線 // setOutInPageR.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL)); myAdapter = new WareRecyclerViewAdapter(myActivity, getArrayList(),1); setOutInPageR.setAdapter(myAdapter); //給每一項(xiàng)添加點(diǎn)擊事件 myAdapter.setItemOnClick(new WareRecyclerViewAdapter.myItemOnClick() { @Override public void onItemClick(int position) { Toast.makeText(myActivity, "你單擊了:"+position, Toast.LENGTH_SHORT).show(); } }); } //模擬數(shù)據(jù) private List<TestData> getArrayList() { List<TestData> dataList = new ArrayList<>(); dataList.add(new TestData(1, "測(cè)試內(nèi)容物料名稱(chēng)1111", "1/只", "2022-10-11", "銷(xiāo)售", "出庫(kù)")); dataList.add(new TestData(2, "測(cè)試內(nèi)容物料名稱(chēng)2222", "1/只", "2022-9-30", "客戶維修", "出庫(kù)")); dataList.add(new TestData(3, "測(cè)試內(nèi)容物料名稱(chēng)33", "10/套", "2022-10-12", "加工組件", "入庫(kù)")); dataList.add(new TestData(4, "測(cè)試內(nèi)容物料名稱(chēng)4", "1008/張", "2022-10-11", "采購(gòu)", "入庫(kù)")); dataList.add(new TestData(5, "測(cè)試內(nèi)容物料名稱(chēng)", "5/只", "2022-10-11", "損耗", "出庫(kù)")); dataList.add(new TestData(6, "66768測(cè)試內(nèi)容物料名稱(chēng)1111", "5/只", "2022-10-05", "損耗", "出庫(kù)")); dataList.add(new TestData(7, "測(cè)試內(nèi)容物料名稱(chēng)1111343", "5/只", "2022-10-01", "加工組件", "出庫(kù)")); dataList.add(new TestData(8, "測(cè)試內(nèi)容物料名稱(chēng)3431)", "5/只", "2022-10-10", "加工組件", "出庫(kù)")); dataList.add(new TestData(9, "測(cè)試內(nèi)容物料名稱(chēng)1111e", "5/只", "2022-10-12", "加工組件", "出庫(kù)")); dataList.add(new TestData(10, "測(cè)試內(nèi)容物料名稱(chēng)454", "30/臺(tái)", "2022-10-15", "采購(gòu)", "入庫(kù)")); dataList.add(new TestData(11, "測(cè)試內(nèi)容物料名稱(chēng)", "5/臺(tái)", "2022-10-11", "補(bǔ)齊", "入庫(kù)")); dataList.add(new TestData(12, "對(duì)對(duì)對(duì)", "5/臺(tái)", "2022-08-11", "配件維修", "入庫(kù)")); dataList.add(new TestData(13, "橙色粉色測(cè)試", "5/只", "2022-09-16", "生產(chǎn)", "出庫(kù)")); dataList.add(new TestData(14, "測(cè)試內(nèi)容二111", "5/只", "2022-09-11", "銷(xiāo)售", "出庫(kù)")); return dataList; } }
關(guān)于 RecyclerView 中的列表布局問(wèn)題
RecyclerView 中的列表項(xiàng)由 LayoutManager 類(lèi)負(fù)責(zé)排列达箍。RecyclerView 庫(kù)提供了三種布局管理器,用于處理最常見(jiàn)的布局情況:
LinearLayoutManager 線性布局:將各個(gè)項(xiàng)排列在一維列表中铺厨。
/**
* LinearLayoutManager線性布局的構(gòu)造函數(shù)原型
* @param context 當(dāng)前的上下文對(duì)象, 用于獲取資源.
* @param orientation 布局方向. 設(shè)置成 RecyclerView.VERTICAL 或 RecyclerView.HORIZONTAL.
* @param reverseLayout 當(dāng)設(shè)置成 true 時(shí), 布局會(huì)翻轉(zhuǎn), 從尾部開(kāi)始頭部結(jié)束.
*/
public LinearLayoutManager(Context context, @RecyclerView.Orientation int orientation,boolean reverseLayout) {
setOrientation(orientation);
setReverseLayout(reverseLayout);
}
// LinearLayoutManager 線性布局的簡(jiǎn)單使用
private LinearLayoutManager getLinearLayoutManager() {
//設(shè)置線性布局
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false) {
//禁止豎向滑動(dòng) RecyclerView 為垂直狀態(tài)(VERTICAL)
@Override
public boolean canScrollVertically() {
return false;
}
//禁止橫向滑動(dòng) RecyclerView 為水平狀態(tài)(HORIZONTAL) canScrollHorizontally
};
return layoutManager;
}
GridLayoutManager 網(wǎng)格布局:將所有項(xiàng)排列在二維網(wǎng)格中:
如果網(wǎng)格垂直排列缎玫,GridLayoutManager 會(huì)盡量使每行中所有元素的寬度和高度相同,但不同的行可以有不同的高度解滓。
如果網(wǎng)格水平排列赃磨,GridLayoutManager 會(huì)盡量使每列中所有元素的寬度和高度相同,但不同的列可以有不同的寬度洼裤。
/**
* GridLayoutManager 網(wǎng)格布局的構(gòu)造函數(shù)原型 (其中一個(gè))
* @param context 當(dāng)前的上下文對(duì)象, 用于獲取資源.
* @param spanCount 網(wǎng)格中的列數(shù)
*/
public GridLayoutManager(Context context, int spanCount) {
super(context);
setSpanCount(spanCount);
}
/**
* GridLayoutManager 網(wǎng)格布局的構(gòu)造函數(shù)原型 (其中一個(gè))
* @param context 當(dāng)前的上下文對(duì)象, 用于獲取資源.
* @param spanCount 網(wǎng)格中的列或行數(shù)
* @param orientation 布局方向. 設(shè)置成 RecyclerView.VERTICAL 或 RecyclerView.HORIZONTAL.
* @param reverseLayout 當(dāng)設(shè)置成 true 時(shí), 布局會(huì)翻轉(zhuǎn), 從尾部開(kāi)始頭部結(jié)束.
*/
public GridLayoutManager(Context context, int spanCount, @RecyclerView.Orientation int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
setSpanCount(spanCount);
}
//簡(jiǎn)單用法
private GridLayoutManager getGridLayoutManager() {
//設(shè)置線性布局//網(wǎng)格布局GridLayoutManager //瀑布流網(wǎng)格布局 StaggeredGridLayoutManager
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2,RecyclerView.VERTICAL,false) {
//禁止豎向滑動(dòng) RecyclerView 為垂直狀態(tài)(VERTICAL)
@Override
public boolean canScrollVertically() {
return false;
}
//禁止橫向滑動(dòng) RecyclerView 為水平狀態(tài)(HORIZONTAL) canScrollHorizontally
};
return gridLayoutManager;
}
StaggeredGridLayoutManager 瀑布流網(wǎng)格布局
與 GridLayoutManager 類(lèi)似邻辉,但不要求同一行中的列表項(xiàng)具有相同的高度(垂直網(wǎng)格有此要求)或同一列中的列表項(xiàng)具有相同的寬度(水平網(wǎng)格有此要求)。其結(jié)果是,同一行或同一列中的列表項(xiàng)可能會(huì)錯(cuò)落不齊恩沛。
/**
* StaggeredGridLayoutManager 瀑布流網(wǎng)格布局的構(gòu)造函數(shù)原型 (其中一個(gè))
* @param spanCount 如果方向是垂直的,則spanCount是列數(shù)缕减。 如果方向是水平的雷客,則spanCount是行數(shù)。
* @param orientation 布局方向. 設(shè)置成 StaggeredGridLayoutManager.VERTICAL 或 StaggeredGridLayoutManager.HORIZONTAL.
*/
public StaggeredGridLayoutManager(int spanCount, int orientation) {
mOrientation = orientation;
setSpanCount(spanCount);
mLayoutState = new LayoutState();
createOrientationHelpers();
}
// 瀑布流網(wǎng)格布局 布局簡(jiǎn)單用法
private StaggeredGridLayoutManager getStaggeredGridLayoutManager() {
StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL) {
@Override
public boolean canScrollVertically() { //禁止垂直滾動(dòng)
return false;
}
//@Override
//public boolean canScrollHorizontally() {return false;}//禁止水平滾動(dòng)
};
return gridLayoutManager;
}