android基礎開發之RecycleView

2021-07-25 00:23:06 字數 4127 閱讀 8754

recycleview是google為了優化listview,gridview 提供的乙個新的控制項。

dependencies 

@override

public recycleviewholder oncreateviewholder(viewgroup parent, int

viewtype)

@override

public

void onbindviewholder(recycleviewholder holder, int

position)

@override

public

intgetitemcount()

class recycleviewholder extends

recyclerview.viewholder

}}

首先 需要定義乙個holder,這個holder是用來優化復用recycleview的item的。此與listview已經非常類似。

其次adapter要實現3個方法:

public recycleviewholder oncreateviewholder(viewgroup parent, int viewtype) //viewholder 布局**
public void onbindviewholder(recycleviewholder holder, int position)  資料處理**
public int getitemcount()   得到item size
相當熟悉的**。

layoutmanager:

recycleview 預設支援linearlayoutmanager & gridlayoutmanager

其實就是對應listview & gridview

mrcyv.setlayoutmanager(new linearlayoutmanager(this));
void

afterview()

itemdectoration:

本質上是每個itemview的子view。很多時候用途就是定義為 分隔線使用。

1.listview.

public

class recycleitemdecoration extends

recyclerview.itemdecoration ;

public

static

final

int horizontal_list =linearlayoutmanager.horizontal;

public

static

final

int vertical_list =linearlayoutmanager.vertical;

private

drawable mdivider;

private

intmorientation;

public recycleitemdecoration(context context, int

orientation)

public

void setorientation(int

orientation)

morientation =orientation;

}@override

public

void

ondraw(canvas c, recyclerview parent)

else

}public

void

drawvertical(canvas c, recyclerview parent)

}public

void

drawhorizontal(canvas c, recyclerview parent)

}@override

public

void getitemoffsets(rect outrect, int

itemposition, recyclerview parent)

else

}}

2.gridview 樣式:

public

class recyclegriditemdecoration extends

recyclerview.itemdecoration ;

private

drawable mdivider;

public

recyclegriditemdecoration(context context)

@override

public

void

ondraw(canvas c, recyclerview parent, recyclerview.state state)

private

intgetspancount(recyclerview parent)

else

if (layoutmanager instanceof

staggeredgridlayoutmanager)

return

spancount;

}public

void

drawhorizontal(canvas c, recyclerview parent)

}public

void

drawvertical(canvas c, recyclerview parent)

}private

boolean islastcolum(recyclerview parent, int pos, int

spancount,

intchildcount)

} else

if (layoutmanager instanceof

staggeredgridlayoutmanager)

} else

}return

false

; }

private

boolean islastraw(recyclerview parent, int pos, int

spancount,

intchildcount)

else

if (layoutmanager instanceof

staggeredgridlayoutmanager)

else

//staggeredgridlayoutmanager 且橫向滾動}}

return

false

; }

@override

public

void getitemoffsets(rect outrect, int

itemposition,

recyclerview parent)

else

if (islastcolum(parent, itemposition, spancount, childcount))//

如果是最後一列,則不需要繪製右邊

else

}}

最後是activity頁面:

@eactivity(r.layout.activity_recycleview_demo)

public

class recycleviewdemo extends

activity

}

itemanimator:

recycleview的每乙個item的add & delete都是可配置動畫的。defaultitemanimator是google提供的預設實現方式。

mrcyv.setitemanimator(new defaultitemanimator());
public

void adddata(int

position)

public

void removedata(int

position)

在adapter中實現這2個方法,關鍵在於notifyitem***,這2個方法可以觸發動畫。

recycleview比起listview來說,應該是可以定製更多的東西,尤其是瀑布模式,實現起來效果很炫。

參考:

Android 遊戲開發基礎

一 首先我們應該知道android遊戲開發的一些基本框架。例如下面乙個簡單的su ceview類框架 public class gamesu ceview extends su ceview implements su ceholder.callback,runnable public void s...

Android程式開發基礎之 頁面傳值

activity跳轉與傳值,主要是通過intent類來連線多個activity,通過bundle類來傳遞資料。示例 如下 1.1 使用intent.putextra 方法賦值 1 public class menu extends activity 23 2425 26 2728 1.2 在另乙個頁...

Android開發之簡訊

1.sms主要結構 id 簡訊序號,如100 thread id 對話的序號,如100,與同乙個手機號互發的簡訊,其序號是相同的 person 發件人,如果發件人在通訊錄中則為具體姓名,陌生人為null date 日期,long型,如1346988516,可以對日期顯示格式進行設定 protocol...