柚子快報激活碼778899分享:
柚子快報激活碼778899分享:
… // 其余寫法與復(fù)寫系統(tǒng)自帶的Adapter相同 }
方式2:繼承 自 VirtualLayoutAdapter
定義:當(dāng)需要實(shí)現(xiàn)復(fù)雜需求時, 可以通過繼承VirtualLayoutAdapter從而實(shí)現(xiàn)自定義Adapter 具體使用
public class MyAdapter extends VirtualLayoutAdapter { …// 自定義Adapter邏輯 }
步驟4:根據(jù)數(shù)據(jù)列表,創(chuàng)建對應(yīng)的LayoutHelper
系統(tǒng)以封裝好以下布局類型(對應(yīng)同名的LayoutHelper)
具體使用如下:
1. 線性布局(LinearLayoutHelper)
布局說明:布局子元素(Item)以線性排布的布局
具體使用
/** 設(shè)置線性布局 */ LinearLayoutHelper linearLayoutHelper = new LinearLayoutHelper(); // 創(chuàng)建對應(yīng)的LayoutHelper對象
// 所有布局的公共屬性(屬性會在下面詳細(xì)說明) linearLayoutHelper.setItemCount(4);// 設(shè)置布局里Item個數(shù) linearLayoutHelper.setPadding(10,10,10,10);// 設(shè)置LayoutHelper的子元素相對LayoutHelper邊緣的距離 linearLayoutHelper.setMargin(10,10,10,10);// 設(shè)置LayoutHelper邊緣相對父控件(即RecyclerView)的距離 linearLayoutHelper.setBgColor(Color.GRAY);// 設(shè)置背景顏色 linearLayoutHelper.setAspectRatio(6);// 設(shè)置設(shè)置布局內(nèi)每行布局的寬與高的比
// linearLayoutHelper特有屬性 linearLayoutHelper.setDividerHeight(1); // 設(shè)置每行Item的距離
1. 所有布局的共有屬性說明:
a. setItemCount屬性
作用:設(shè)置整個布局里的Item數(shù)量
如設(shè)置的Item總數(shù)如與Adapter的getItemCount()方法返回的數(shù)量不同,會取決于后者
具體使用
// 接口示意 public void setItemCount(int Count)
// 具體使用 linearLayoutHelper.setItemCount(4);
b. Adding & Margin屬性
定義:都是邊距的含義,但二者邊距的定義不同:
Padding:是 LayoutHelper 的子元素相對 LayoutHelper 邊緣的距離;Margin:是 LayoutHelper 邊緣相對父控件(即RecyclerView)的距離。具體如下圖:
具體使用
// 接口示意 public void setPadding(int leftPadding, int topPadding, int rightPadding, int bottomPadding) public void setMargin(int leftMargin, int topMargin, int rightMargin, int bottomMargin)
// 具體使用 linearLayoutHelper.setPadding(10,10,10,10); // 設(shè)置LayoutHelper的子元素相對LayoutHelper邊緣的距離
linearLayoutHelper.setMargin(10,10,10,10); // 設(shè)置LayoutHelper邊緣相對父控件(即RecyclerView)的距離
c. bgColor屬性
作用:設(shè)置布局背景顏色具體使用:
// 接口示意 public void setBgColor(int bgColor)
// 具體使用 linearLayoutHelper.setBgColor(Color.YELLOW);
d. aspectRatio屬性
作用:設(shè)置布局內(nèi)每行布局的寬與高的比。如下圖
具體使用
// 接口示意 public void setAspectRatio(float aspectRatio); // LayoutHelper定義的aspectRatio
((VirutalLayoutManager.LayoutParams) layoutParams).mAspectRatio // 視圖的LayoutParams定義的aspectRatio // 在LayoutHelper計(jì)算出視圖寬度之后,用來確定視圖高度時使用的,它會覆蓋通過LayoutHelper的aspectRatio計(jì)算出來的視圖高度,因此具備更高優(yōu)先級。
// 具體使用 linearLayoutHelper.setAspectRatio(6);
2. LinearLayoutHelper布局的特有屬性說明
a. dividerHeight屬性
作用:設(shè)置每行Item之間的距離
設(shè)置的間隔會與RecyclerView的addItemDecoration()添加的間隔疊加
具體使用
// 接口示意 public void setDividerHeight(int dividerHeight)
// 具體使用 linearLayoutHelper.setDividerHeight(1);
2. 網(wǎng)格布局(GridLayout)
布局說明:布局里的Item以網(wǎng)格的形式進(jìn)行排列
具體使用
/** 設(shè)置Grid布局 */ GridLayoutHelper gridLayoutHelper = new GridLayoutHelper(3); // 在構(gòu)造函數(shù)設(shè)置每行的網(wǎng)格個數(shù)
// 公共屬性 gridLayoutHelper.setItemCount(6);// 設(shè)置布局里Item個數(shù) gridLayoutHelper.setPadding(20, 20, 20, 20);// 設(shè)置LayoutHelper的子元素相對LayoutHelper邊緣的距離 gridLayoutHelper.setMargin(20, 20, 20, 20);// 設(shè)置LayoutHelper邊緣相對父控件(即RecyclerView)的距離 gridLayoutHelper.setBgColor(Color.GRAY);// 設(shè)置背景顏色 gridLayoutHelper.setAspectRatio(6);// 設(shè)置設(shè)置布局內(nèi)每行布局的寬與高的比
// gridLayoutHelper特有屬性(下面會詳細(xì)說明) gridLayoutHelper.setWeights(new float[]{40, 30, 30});//設(shè)置每行中 每個網(wǎng)格寬度 占 每行總寬度 的比例 gridLayoutHelper.setVGap(20);// 控制子元素之間的垂直間距 gridLayoutHelper.setHGap(20);// 控制子元素之間的水平間距 gridLayoutHelper.setAutoExpand(false);//是否自動填充空白區(qū)域 gridLayoutHelper.setSpanCount(3);// 設(shè)置每行多少個網(wǎng)格 // 通過自定義SpanSizeLookup來控制某個Item的占網(wǎng)格個數(shù) gridLayoutHelper.setSpanSizeLookup(new GridLayoutHelper.SpanSizeLookup() { @Override public int getSpanSize(int position) { if (position > 7 ) { return 3; // 第7個位置后,每個Item占3個網(wǎng)格 }else { return 2; // 第7個位置前,每個Item占2個網(wǎng)格 } } });
GridLayoutHelper布局的特有屬性說明
a. weights屬性
作用:設(shè)置每行中每個網(wǎng)格寬度占每行總寬度的比例
默認(rèn)情況下,每行中每個網(wǎng)格中的寬度相等weights屬性是一個float數(shù)組,每一項(xiàng)代表當(dāng)個網(wǎng)格占每行總寬度的百分比;總和是100,否則布局會超出容器寬度;如果布局中有4列,那么weights的長度也應(yīng)該是4;長度大于4,多出的部分不參與寬度計(jì)算;如果小于4,不足的部分默認(rèn)平分剩余的空間。
具體使用
// 接口示意 public void setWeights(float[] weights)
// 具體使用 gridLayoutHelper.setWeights(new float[]{40, 30, 30});
b. vGap、hGap屬性
作用:分別控制子元素之間的垂直間距 和 水平間距。
具體使用
// 接口示意 public void setHGap(int hGap) public void setVGap(int vGap)
// 具體使用 gridLayoutHelper.setVGap(20);// 控制子元素之間的垂直間距 gridLayoutHelper.setHGap(20);// 控制子元素之間的水平間距
c. spanCount、spanSizeLookup屬性
作用:
spanCount:設(shè)置每行多少個網(wǎng)格spanSizeLookup:設(shè)置每個 Item占用多少個網(wǎng)格(默認(rèn)= 1)
具體使用
// 接口示意 public void setSpanCount(int spanCount) public void setSpanSizeLookup(SpanSizeLookup spanSizeLookup)
// 具體使用 gridLayoutHelper.setSpanCount(5);// 設(shè)置每行多少個網(wǎng)格
// 通過自定義SpanSizeLookup來控制某個Item的占網(wǎng)格個數(shù) gridLayoutHelper.setSpanSizeLookup(new GridLayoutHelper.SpanSizeLookup() { @Override public int getSpanSize(int position) { if (position > 7 ) { return 3; // 第7個位置后,每個Item占3個網(wǎng)格 }else { return 2; // 第7個位置前,每個Item占2個網(wǎng)格 } } });
d. autoExpand屬性
作用:當(dāng)一行里item的個數(shù) < (每行網(wǎng)格列數(shù) - spanCount值/ 每個Item占有2個網(wǎng)格-setSpanSizeLookup )時,是否自動填滿空白區(qū)域
若autoExpand=true,那么視圖的總寬度會填滿可用區(qū)域;否則會在屏幕上留空白區(qū)域。
具體使用
// 接口示意 public void setAutoExpand(boolean isAutoExpand)
// 具體使用 gridLayoutHelper.setAutoExpand(false);
3. 固定布局(FixLayoutHelper)
布局說明:布局里的Item 固定位置
固定在屏幕某個位置,且不可拖拽 & 不隨頁面滾動而滾動。如下圖:(左上角)
具體使用
/** 設(shè)置固定布局 */
FixLayoutHelper fixLayoutHelper = new FixLayoutHelper(FixLayoutHelper.TOP_LEFT,40,100); // 參數(shù)說明: // 參數(shù)1:設(shè)置吸邊時的基準(zhǔn)位置(alignType) - 有四個取值:TOP_LEFT(默認(rèn)), TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT // 參數(shù)2:基準(zhǔn)位置的偏移量x // 參數(shù)3:基準(zhǔn)位置的偏移量y
// 公共屬性 fixLayoutHelper.setItemCount(1);// 設(shè)置布局里Item個數(shù) // 從設(shè)置Item數(shù)目的源碼可以看出,一個FixLayoutHelper只能設(shè)置一個 // @Override // public void setItemCount(int itemCount) { // if (itemCount > 0) { // super.setItemCount(1); // } else { // super.setItemCount(0); // } // } fixLayoutHelper.setPadding(20, 20, 20, 20);// 設(shè)置LayoutHelper的子元素相對LayoutHelper邊緣的距離 fixLayoutHelper.setMargin(20, 20, 20, 20);// 設(shè)置LayoutHelper邊緣相對父控件(即RecyclerView)的距離 fixLayoutHelper.setBgColor(Color.GRAY);// 設(shè)置背景顏色 fixLayoutHelper.setAspectRatio(6);// 設(shè)置設(shè)置布局內(nèi)每行布局的寬與高的比
// fixLayoutHelper特有屬性 fixLayoutHelper.setAlignType(FixLayoutHelper.TOP_LEFT);// 設(shè)置吸邊時的基準(zhǔn)位置(alignType) fixLayoutHelper.setX(30);// 設(shè)置基準(zhǔn)位置的橫向偏移量X fixLayoutHelper.setY(50);// 設(shè)置基準(zhǔn)位置的縱向偏移量Y
FixLayoutHelper特有屬性說明
a. AlignType、x、y屬性
作用:
alignType:吸邊基準(zhǔn)類型
共有4個取值:TOP_LEFT(默認(rèn)), TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT,具體請看下面示意圖
x:基準(zhǔn)位置的橫向偏移量y:基準(zhǔn)位置的縱向偏移量
作用對象:FixLayoutHelper, ScrollFixLayoutHelper, FloatLayoutHelper的屬性
// 接口示意 public void setAlignType(int alignType) public void setX(int x) public void setY(int y)
// 具體使用 fixLayoutHelper.setAlignType(FixLayoutHelper.TOP_LEFT); fixLayoutHelper.setX(30); fixLayoutHelper.setY(50);
4. 可選顯示的固定布局(ScrollFixLayoutHelper)
布局說明:布局里的Item 固定位置
固定在屏幕某個位置,且不可拖拽 & 不隨頁面滾動而滾動(繼承自固定布局(FixLayoutHelper))唯一不同的是,可以自由設(shè)置該Item什么時候顯示(到頂部顯示 / 到底部顯示),可如下圖:(左上角)需求場景:到頁面底部顯示”一鍵到頂部“的按鈕功能
以下示意圖為:滑動到底部,布局才在左上角顯示
具體使用
/** 設(shè)置可選固定布局 */
ScrollFixLayoutHelper scrollFixLayoutHelper = new ScrollFixLayoutHelper(ScrollFixLayoutHelper.TOP_RIGHT,0,0); // 參數(shù)說明: // 參數(shù)1:設(shè)置吸邊時的基準(zhǔn)位置(alignType) - 有四個取值:TOP_LEFT(默認(rèn)), TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT // 參數(shù)2:基準(zhǔn)位置的偏移量x // 參數(shù)3:基準(zhǔn)位置的偏移量y
// 公共屬性 scrollFixLayoutHelper.setItemCount(1);// 設(shè)置布局里Item個數(shù) // 從設(shè)置Item數(shù)目的源碼可以看出,一個FixLayoutHelper只能設(shè)置一個 // @Override // public void setItemCount(int itemCount) { // if (itemCount > 0) { // super.setItemCount(1); // } else { // super.setItemCount(0); // } // } scrollFixLayoutHelper.setPadding(20, 20, 20, 20);// 設(shè)置LayoutHelper的子元素相對LayoutHelper邊緣的距離 scrollFixLayoutHelper.setMargin(20, 20, 20, 20);// 設(shè)置LayoutHelper邊緣相對父控件(即RecyclerView)的距離 scrollFixLayoutHelper.setBgColor(Color.GRAY);// 設(shè)置背景顏色 scrollFixLayoutHelper.setAspectRatio(6);// 設(shè)置設(shè)置布局內(nèi)每行布局的寬與高的比
// fixLayoutHelper特有屬性 scrollFixLayoutHelper.setAlignType(FixLayoutHelper.TOP_LEFT);// 設(shè)置吸邊時的基準(zhǔn)位置(alignType) scrollFixLayoutHelper.setX(30);// 設(shè)置基準(zhǔn)位置的橫向偏移量X scrollFixLayoutHelper.setY(50);// 設(shè)置基準(zhǔn)位置的縱向偏移量Y scrollFixLayoutHelper.setShowType(ScrollFixLayoutHelper.SHOW_ON_ENTER);// 設(shè)置Item的顯示模式
ScrollFixLayoutHelper特有屬性說明
a. AlignType、x、y屬性
作用:
alignType:吸邊基準(zhǔn)類型
共有4個取值:TOP_LEFT(默認(rèn)), TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT,具體請看下面示意圖
x:基準(zhǔn)位置的橫向偏移量y:基準(zhǔn)位置的縱向偏移量
具體使用
// 接口示意 public void setAlignType(int alignType) public void setX(int x) public void setY(int y)
// 具體使用 ScrollFixLayoutHelper.setAlignType(FixLayoutHelper.TOP_LEFT); ScrollFixLayoutHelper.setX(30); ScrollFixLayoutHelper.setY(50);
b. ShowType屬性
作用:設(shè)置Item的顯示模式
共有三種顯示模式
SHOW_ALWAYS:永遠(yuǎn)顯示(即效果同固定布局)SHOW_ON_ENTER:默認(rèn)不顯示視圖,當(dāng)頁面滾動到該視圖位置時才顯示;SHOW_ON_LEAVE:默認(rèn)不顯示視圖,當(dāng)頁面滾出該視圖位置時才顯示
具體使用
// 接口示意 public void setShowType(int showType)
// 具體使用 scrollFixLayoutHelper.setShowType(ScrollFixLayoutHelper.SHOW_ON_ENTER);
5. 浮動布局(FloatLayoutHelper)
布局說明:布局里的Item只有一個
可隨意拖動,但最終會被吸邊到兩側(cè)不隨頁面滾動而移動
具體使用
/** 設(shè)置浮動布局 */ FloatLayoutHelper floatLayoutHelper = new FloatLayoutHelper(); // 創(chuàng)建FloatLayoutHelper對象
// 公共屬性 floatLayoutHelper.setItemCount(1);// 設(shè)置布局里Item個數(shù) // 從設(shè)置Item數(shù)目的源碼可以看出,一個FixLayoutHelper只能設(shè)置一個 // @Override // public void setItemCount(int itemCount) { // if (itemCount > 0) { // super.setItemCount(1); // } else { // super.setItemCount(0); // } // } floatLayoutHelper.setPadding(20, 20, 20, 20);// 設(shè)置LayoutHelper的子元素相對LayoutHelper邊緣的距離 floatLayoutHelper.setMargin(20, 20, 20, 20);// 設(shè)置LayoutHelper邊緣相對父控件(即RecyclerView)的距離 floatLayoutHelper.setBgColor(Color.GRAY);// 設(shè)置背景顏色 floatLayoutHelper.setAspectRatio(6);// 設(shè)置設(shè)置布局內(nèi)每行布局的寬與高的比
// floatLayoutHelper特有屬性 floatLayoutHelper.setDefaultLocation(300,300);// 設(shè)置布局里Item的初始位置
6. 欄格布局(ColumnLayoutHelper)
布局說明:該布局只設(shè)有一欄(該欄設(shè)置多個Item)
可理解為只有一行的線性布局
/** 設(shè)置欄格布局 */ ColumnLayoutHelper columnLayoutHelper = new ColumnLayoutHelper(); // 創(chuàng)建對象
// 公共屬性 columnLayoutHelper.setItemCount(3);// 設(shè)置布局里Item個數(shù) columnLayoutHelper.setPadding(20, 20, 20, 20);// 設(shè)置LayoutHelper的子元素相對LayoutHelper邊緣的距離 columnLayoutHelper.setMargin(20, 20, 20, 20);// 設(shè)置LayoutHelper邊緣相對父控件(即RecyclerView)的距離 columnLayoutHelper.setBgColor(Color.GRAY);// 設(shè)置背景顏色 columnLayoutHelper.setAspectRatio(6);// 設(shè)置設(shè)置布局內(nèi)每行布局的寬與高的比
// columnLayoutHelper特有屬性 columnLayoutHelper.setWeights(new float[]{30, 40, 30});// 設(shè)置該行每個Item占該行總寬度的比例 // 同上面Weigths屬性講解
7. 通欄布局(SingleLayoutHelper)
布局說明:布局只有一欄,該欄只有一個Item
具體使用
/** 設(shè)置通欄布局 */
SingleLayoutHelper singleLayoutHelper = new SingleLayoutHelper();
// 公共屬性 singleLayoutHelper.setItemCount(3);// 設(shè)置布局里Item個數(shù) singleLayoutHelper.setPadding(20, 20, 20, 20);// 設(shè)置LayoutHelper的子元素相對LayoutHelper邊緣的距離 singleLayoutHelper.setMargin(20, 20, 20, 20);// 設(shè)置LayoutHelper邊緣相對父控件(即RecyclerView)的距離 singleLayoutHelper.setBgColor(Color.GRAY);// 設(shè)置背景顏色 singleLayoutHelper.setAspectRatio(6);// 設(shè)置設(shè)置布局內(nèi)每行布局的寬與高的比
8. 一拖N布局 (OnePlusNLayoutHelper)
布局說明:將布局分為不同比例,最多是1拖4。具體如下圖
具體使用
/** 設(shè)置1拖N布局 */ OnePlusNLayoutHelper onePlusNLayoutHelper = new OnePlusNLayoutHelper(5); // 在構(gòu)造函數(shù)里傳入顯示的Item數(shù) // 最多是1拖4,即5個
// 公共屬性 onePlusNLayoutHelper.setItemCount(3);// 設(shè)置布局里Item個數(shù) onePlusNLayoutHelper.setPadding(20, 20, 20, 20);// 設(shè)置LayoutHelper的子元素相對LayoutHelper邊緣的距離 onePlusNLayoutHelper.setMargin(20, 20, 20, 20);// 設(shè)置LayoutHelper邊緣相對父控件(即RecyclerView)的距離 onePlusNLayoutHelper.setBgColor(Color.GRAY);// 設(shè)置背景顏色 onePlusNLayoutHelper.setAspectRatio(3);// 設(shè)置設(shè)置布局內(nèi)每行布局的寬與高的比
9. 吸邊布局(StickyLayoutHelper)
布局說明:布局只有一個Item,顯示邏輯如下:
當(dāng)它包含的組件處于屏幕可見范圍內(nèi)時,像正常的組件一樣隨頁面滾動而滾動當(dāng)組件將要被滑出屏幕返回的時候,可以吸到屏幕的頂部或者底部,實(shí)現(xiàn)一種吸住的效果
示意圖(吸在頂部)
具體使用
/** 設(shè)置吸邊布局 */ StickyLayoutHelper stickyLayoutHelper = new StickyLayoutHelper();
// 公共屬性 stickyLayoutHelper.setItemCount(3);// 設(shè)置布局里Item個數(shù) stickyLayoutHelper.setPadding(20, 20, 20, 20);// 設(shè)置LayoutHelper的子元素相對LayoutHelper邊緣的距離 stickyLayoutHelper.setMargin(20, 20, 20, 20);// 設(shè)置LayoutHelper邊緣相對父控件(即RecyclerView)的距離 stickyLayoutHelper.setBgColor(Color.GRAY);// 設(shè)置背景顏色 stickyLayoutHelper.setAspectRatio(3);// 設(shè)置設(shè)置布局內(nèi)每行布局的寬與高的比
// 特有屬性 stickyLayoutHelper.setStickyStart(true); // true = 組件吸在頂部 // false = 組件吸在底部
stickyLayoutHelper.setOffset(100);// 設(shè)置吸邊位置的偏移量
Adapter_StickyLayout = new MyAdapter(this, stickyLayoutHelper,1, listItem) { // 設(shè)置需要展示的數(shù)據(jù)總數(shù),此處設(shè)置是1 // 為了展示效果,通過重寫onBindViewHolder()將布局的第一個數(shù)據(jù)設(shè)置為Stick @Override public void onBindViewHolder(MainViewHolder holder, int position) { super.onBindViewHolder(holder, position); if (position == 0) { holder.Text.setText(“Stick”); } } };
adapters.add(Adapter_StickyLayout) ; // 將當(dāng)前的Adapter加入到Adapter列表里
stickyStart、 offset屬性說明
作用:
stickyStart:設(shè)置吸邊位置
當(dāng)視圖的位置在屏幕范圍內(nèi)時,視圖會隨頁面滾動而滾動;當(dāng)視圖的位置滑出屏幕時,StickyLayoutHelper會將視圖固定在頂部(stickyStart = true)或 底部(stickyStart = false)
offset:設(shè)置吸邊的偏移量
具體使用
// 接口示意 public void setStickyStart(boolean stickyStart) public void setOffset(int offset)
// 具體使用 stickyLayoutHelper.setStickyStart(true); // true = 組件吸在頂部 // false = 組件吸在底部 stickyLayoutHelper.setOffset(100);// 設(shè)置吸邊位置的偏移量
10. 瀑布流布局(StaggeredGridLayoutHelper)
布局說明:以網(wǎng)格的形式進(jìn)行布局。與網(wǎng)格布局類似,區(qū)別在于:網(wǎng)格布局每欄的Item高度是相等的;瀑布流布局每欄的Item高度是可以不相等的。
具體使用
/** 設(shè)置瀑布流布局 */
StaggeredGridLayoutHelper staggeredGridLayoutHelper = new StaggeredGridLayoutHelper(); // 創(chuàng)建對象
// 公有屬性 staggeredGridLayoutHelper.setItemCount(20);// 設(shè)置布局里Item個數(shù) staggeredGridLayoutHelper.setPadding(20, 20, 20, 20);// 設(shè)置LayoutHelper的子元素相對LayoutHelper邊緣的距離 staggeredGridLayoutHelper.setMargin(20, 20, 20, 20);// 設(shè)置LayoutHelper邊緣相對父控件(即RecyclerView)的距離 staggeredGridLayoutHelper.setBgColor(Color.GRAY);// 設(shè)置背景顏色 staggeredGridLayoutHelper.setAspectRatio(3);// 設(shè)置設(shè)置布局內(nèi)每行布局的寬與高的比
// 特有屬性 staggeredGridLayoutHelper.setLane(3);// 設(shè)置控制瀑布流每行的Item數(shù) staggeredGridLayoutHelper.setHGap(20);// 設(shè)置子元素之間的水平間距 staggeredGridLayoutHelper.setVGap(15);// 設(shè)置子元素之間的垂直間距
自定義布局(即自定義LayoutHelper)
除了使用系統(tǒng)提供的默認(rèn)布局 LayoutHelper,開發(fā)者還可以通過自定義LayoutHelper從而實(shí)現(xiàn)自定義布局樣式。有三種方式:
繼承BaseLayoutHelper:從上而下排列的順序 & 內(nèi)部 View可以按行回收的布局;主要實(shí)現(xiàn)layoutViews()、computeAlignOffset()等方法
LinearLayoutHelper、GridLayoutHelper都是采用該方法實(shí)現(xiàn)
繼承AbstractFullFillLayoutHelper:有些布局內(nèi)部的 View 并不是從上至下排列的順序(即 Adatper 里的數(shù)據(jù)順序和物理視圖順序不一致,那么可能就不能按數(shù)據(jù)順序布局和回收),需要一次性布局 & 回收。主要實(shí)現(xiàn)layoutViews()等方法
OnePlusNLayoutHelper采用該方法實(shí)現(xiàn)
繼承FixAreaLayoutHelper:fix 類型布局,子節(jié)點(diǎn)不隨頁面滾動而滾動。主要實(shí)現(xiàn)layoutViews()、beforeLayout()、afterLayout()等方法
FixLayoutHelper采用該方法實(shí)現(xiàn)
步驟5:將生成的LayoutHelper 交給Adapter,并綁定到RecyclerView 對象
此處的做法會因步驟3中Adapter的設(shè)置而有所不同
<-- Adapter繼承 自 DelegateAdapter -->
// 步驟1:設(shè)置Adapter列表(同時也是設(shè)置LayoutHelper列表) List
// 步驟2:創(chuàng)建自定義的Adapter對象 & 綁定數(shù)據(jù) & 綁定上述對應(yīng)的LayoutHelper // 綁定你需要展示的布局LayoutHelper即可,此處僅展示兩個。 MyAdapter Adapter_linearLayout = new MyAdapter(this, linearLayoutHelper,ListItem); // ListItem是需要綁定的數(shù)據(jù)(其實(shí)取決于你的Adapter如何定義)
MyAdapter Adapter_gridLayoutHelper = new MyAdapter(this, gridLayoutHelper,ListItem);
// 步驟3:將創(chuàng)建的Adapter對象放入到DelegateAdapter.Adapter列表里 adapters.add(Adapter_linearLayout ) ; adapters.add(Adapter_gridLayoutHelper ) ;
// 步驟4:創(chuàng)建DelegateAdapter對象 & 將layoutManager綁定到DelegateAdapter DelegateAdapter delegateAdapter = new DelegateAdapter(layoutManager);
// 步驟5:將DelegateAdapter.Adapter列表綁定到DelegateAdapter delegateAdapter.setAdapters(adapters);
// 步驟6:將delegateAdapter綁定到recyclerView recyclerView.setAdapter(delegateAdapter);
<-- Adapter繼承 自 VirtualLayoutAdapter -->
// 步驟1:設(shè)置LayoutHelper列表 List helpers = new LinkedList<>();
// 步驟2:綁定上述對應(yīng)的LayoutHelper helpers.add(Adapter_linearLayout ); helpers.add(Adapter_gridLayoutHelper ) ;
// 步驟3:創(chuàng)建自定義的MyAdapter對象 & 綁定layoutManager MyAdapter myAdapter = new MyAdapter(layoutManager);
// 步驟4:將 layoutHelper 列表傳遞給 adapter myAdapter.setLayoutHelpers(helpers);
// 步驟5:將adapter綁定到recyclerView recycler.setAdapter(myAdapter);
至此,使用過程講解完畢。
6. 實(shí)例說明
V-Layout的優(yōu)點(diǎn)在于快速的組合不同布局下面,我將根據(jù)上面的步驟說明,用一個實(shí)例來使用 V - Layout快速組合布局
步驟1:在Android - Gradle加入依賴
compile (‘com.alibaba.android:vlayout:1.0.3@aar’) { transitive = true }
步驟2:定義主 xml布局
activity_main.xml
步驟3:定義 RecyclerView每個子元素( Item )的xml布局
item.xml
此處定義的 Item 布局是常用的 上字下圖
步驟4:設(shè)置Adapter
設(shè)置 V - Layout的Adapter有兩種方式:
繼承 自 DelegateAdapter
此處主要以該方式進(jìn)行演示
繼承 自 VirtualLayoutAdapter
具體使用
MyAdapter.java
public class MyAdapter extends DelegateAdapter.Adapter
private ArrayList
private Context context; private LayoutHelper layoutHelper; private RecyclerView.LayoutParams layoutParams; private int count = 0;
private MyItemClickListener myItemClickListener; // 用于設(shè)置Item點(diǎn)擊事件
//構(gòu)造函數(shù)(傳入每個的數(shù)據(jù)列表 & 展示的Item數(shù)量) public MyAdapter(Context context, LayoutHelper layoutHelper, int count, ArrayList
public MyAdapter(Context context, LayoutHelper layoutHelper, int count, @NonNull RecyclerView.LayoutParams layoutParams, ArrayList
// 把ViewHolder綁定Item的布局 @Override public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new MainViewHolder(LayoutInflater.from(context).inflate(R.layout.item, parent, false)); }
// 此處的Adapter和普通RecyclerView定義的Adapter只相差了一個onCreateLayoutHelper()方法 @Override public LayoutHelper onCreateLayoutHelper() { return layoutHelper; }
// 綁定Item的數(shù)據(jù) @Override public void onBindViewHolder(MainViewHolder holder, int position) { holder.Text.setText((String) listItem.get(position).get(“ItemTitle”)); holder.image.setImageResource((Integer) listItem.get(position).get(“ItemImage”));
}
// 返回Item數(shù)目 @Override public int getItemCount() { return count; }
// 設(shè)置Item的點(diǎn)擊事件 // 綁定MainActivity傳進(jìn)來的點(diǎn)擊監(jiān)聽器 public void setOnItemClickListener(MyItemClickListener listener) { myItemClickListener = listener; }
//定義Viewholder class MainViewHolder extends RecyclerView.ViewHolder { public TextView Text; public ImageView image;
public MainViewHolder(View root) { super(root);
// 綁定視圖 Text = (TextView) root.findViewById(R.id.Item); image = (ImageView) root.findViewById(R.id.Image);
root.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (myItemClickListener != null) myItemClickListener.onItemClick(v, getPosition()); }
} //監(jiān)聽到點(diǎn)擊就回調(diào)MainActivity的onItemClick函數(shù) );
}
public TextView getText() { return Text; } } }
以下步驟都將寫在同一個.Java文件里
步驟5:創(chuàng)建RecyclerView & VirtualLayoutManager 對象并進(jìn)行綁定 步驟6:設(shè)置回收復(fù)用池大小 步驟7:設(shè)置需要存放的數(shù)據(jù) 步驟8:根據(jù)數(shù)據(jù)列表,創(chuàng)建對應(yīng)的LayoutHelper 步驟9:將生成的LayoutHelper 交給Adapter,并綁定到RecyclerView 對象
詳細(xì)請看注釋
MainActivity.java
public class MainActivity extends AppCompatActivity implements MyItemClickListener { RecyclerView recyclerView; MyAdapter Adapter_linearLayout,Adapter_GridLayout,Adapter_FixLayout,Adapter_ScrollFixLayout ,Adapter_FloatLayout,Adapter_ColumnLayout,Adapter_SingleLayout,Adapter_onePlusNLayout, Adapter_StickyLayout,Adapter_StaggeredGridLayout; private ArrayList
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
/**
步驟1:創(chuàng)建RecyclerView & VirtualLayoutManager 對象并進(jìn)行綁定*/ recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); // 創(chuàng)建RecyclerView對象
VirtualLayoutManager layoutManager = new VirtualLayoutManager(this); // 創(chuàng)建VirtualLayoutManager對象 // 同時內(nèi)部會創(chuàng)建一個LayoutHelperFinder對象,用來后續(xù)的LayoutHelper查找
recyclerView.setLayoutManager(layoutManager); // 將VirtualLayoutManager綁定到recyclerView
最后
自我介紹一下,小編13年上海交大畢業(yè),曾經(jīng)在小公司待過,也去過華為、OPPO等大廠,18年進(jìn)入阿里一直到現(xiàn)在。
深知大多數(shù)初中級Android工程師,想要提升技能,往往是自己摸索成長,自己不成體系的自學(xué)效果低效漫長且無助。
因此我收集整理了一份《2024年Android移動開發(fā)全套學(xué)習(xí)資料》,初衷也很簡單,就是希望能夠幫助到想自學(xué)提升又不知道該從何學(xué)起的朋友,同時減輕大家的負(fù)擔(dān)。
既有適合小白學(xué)習(xí)的零基礎(chǔ)資料,也有適合3年以上經(jīng)驗(yàn)的小伙伴深入學(xué)習(xí)提升的進(jìn)階課程,基本涵蓋了95%以上Android開發(fā)知識點(diǎn)!不論你是剛?cè)腴TAndroid開發(fā)的新手,還是希望在技術(shù)上不斷提升的資深開發(fā)者,這些資料都將為你打開新的學(xué)習(xí)之門
如果你覺得這些內(nèi)容對你有幫助,需要這份全套學(xué)習(xí)資料的朋友可以戳我獲?。?!
由于文件比較大,這里只是將部分目錄截圖出來,每個節(jié)點(diǎn)里面都包含大廠面經(jīng)、學(xué)習(xí)筆記、源碼講義、實(shí)戰(zhàn)項(xiàng)目、講解視頻,并且會持續(xù)更新! w = (RecyclerView) findViewById(R.id.my_recycler_view); // 創(chuàng)建RecyclerView對象
VirtualLayoutManager layoutManager = new VirtualLayoutManager(this); // 創(chuàng)建VirtualLayoutManager對象 // 同時內(nèi)部會創(chuàng)建一個LayoutHelperFinder對象,用來后續(xù)的LayoutHelper查找
recyclerView.setLayoutManager(layoutManager); // 將VirtualLayoutManager綁定到recyclerView
最后
自我介紹一下,小編13年上海交大畢業(yè),曾經(jīng)在小公司待過,也去過華為、OPPO等大廠,18年進(jìn)入阿里一直到現(xiàn)在。
深知大多數(shù)初中級Android工程師,想要提升技能,往往是自己摸索成長,自己不成體系的自學(xué)效果低效漫長且無助。
因此我收集整理了一份《2024年Android移動開發(fā)全套學(xué)習(xí)資料》,初衷也很簡單,就是希望能夠幫助到想自學(xué)提升又不知道該從何學(xué)起的朋友,同時減輕大家的負(fù)擔(dān)。
[外鏈圖片轉(zhuǎn)存中…(img-lZl6am8e-1715792183850)]
[外鏈圖片轉(zhuǎn)存中…(img-LoGRKSC9-1715792183851)]
[外鏈圖片轉(zhuǎn)存中…(img-Oyq85aBp-1715792183851)]
[外鏈圖片轉(zhuǎn)存中…(img-g13FgU5X-1715792183852)]
既有適合小白學(xué)習(xí)的零基礎(chǔ)資料,也有適合3年以上經(jīng)驗(yàn)的小伙伴深入學(xué)習(xí)提升的進(jìn)階課程,基本涵蓋了95%以上Android開發(fā)知識點(diǎn)!不論你是剛?cè)腴TAndroid開發(fā)的新手,還是希望在技術(shù)上不斷提升的資深開發(fā)者,這些資料都將為你打開新的學(xué)習(xí)之門
如果你覺得這些內(nèi)容對你有幫助,需要這份全套學(xué)習(xí)資料的朋友可以戳我獲?。?!
由于文件比較大,這里只是將部分目錄截圖出來,每個節(jié)點(diǎn)里面都包含大廠面經(jīng)、學(xué)習(xí)筆記、源碼講義、實(shí)戰(zhàn)項(xiàng)目、講解視頻,并且會持續(xù)更新!
柚子快報激活碼778899分享:
推薦閱讀
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。