請看下面的例子:
下載ViewFlow的源代碼,然後把ViewFlow這個類放在自己項目的src的壹個包下。
2.下載的源代碼中有兩個項目,view flow,viewflow-example。查看流程項目中的屬性
3.布局文件如下所示:
& lt?xml版本="1.0 "編碼="utf-8 "?& gt
& ltlinear layout xmlns:Android = "/apk/RES/Android "
android:id="@+id/home_layout "
Android:layout _ width = " fill _ parent "
Android:layout _ height = " fill _ parent "
Android:orientation = " vertical " & gt;
& lt相對布局
Android:id = " @+id/home _ header layout "
Android:layout _ width = " fill _ parent "
Android:layout _ height = " fill _ parent "
Android:layout _ weight = " 9 " & gt;
& lt紐扣
Android:id = " @+id/memo _ type _ edit _ BTN "
Android:layout _ width = " wrap _ content "
Android:layout _ height = " wrap _ content "
Android:layout _ alignParentLeft = " true "
Android:layout _ center vertical = " true "
android:layout_margin="4dip "
Android:background = " @ drawable/button _ BG _ select _ type "
Android:padding = " 3d IP "/& gt;
& lt紐扣
android:id="@+id/memo_type_btn "
Android:layout _ width = " 100 dip "
android:layout_height="35dip "
Android:layout _ centerInParent = " true "
android:layout_margin="4dip "
Android:background = " @ drawable/button _ BG _ down "
安卓:重力= "中心"
android:singleLine="true "
Android:text= "Work (10)"
Android:text color = " @ color/white "
android:textSize="18sp "
Android:text style = " bold "/& gt;
& lt紐扣
android:id="@+id/new_memo_btn "
Android:layout _ width = " wrap _ content "
Android:layout _ height = " wrap _ content "
Android:layout _ alignParentRight = " true "
Android:layout _ center vertical = " true "
android:layout_margin="4dip "
Android:background = " @ drawable/button _ BG _ add "/& gt;
& lt/relative layout & gt;
& lt線性布局
Android:layout _ width = " fill _ parent "
Android:layout _ height = " fill _ parent "
android:layout_weight="1 "
Android:orientation = " vertical " & gt;
& lt相對布局
android:id="@+id/kitty "
Android:layout _ width = " fill _ parent "
Android:layout _ height = " fill _ parent "
android:layout_weight="1 "
Android:orientation = " vertical " & gt;
& lt框架布局
xmlns:app = "/apk/RES/com . BST . memo . activity "
Android:layout _ width = " fill _ parent "
Android:layout _ height = " fill _ parent "
Android:orientation = " vertical " & gt;
& ltcom . LX . view flow . view flow & lt;!-這個類是復制下載的ViewFlow類->;
android:id="@+id/home_viewflow "
Android:layout _ width = " fill _ parent "
Android:layout _ height = " fill _ parent "
Android:layout _ margin top = " 25 dip "/& gt;
& ltcom . LX . view flow . circle flow indicator & lt;!-這個類也是從項目view flow->;
android:id="@+id/viewflowindic "
Android:layout _ width = " wrap _ content "
Android:layout _ height = " wrap _ content "
Android:layout _ gravity = " center _ horizontal "
app:fadeOut="1000 "
app:inactiveType="fill "
Android:padding top = " 10 dip "/& gt;
& lt/frame layout & gt;
& lt圖像視圖
android:id="@+id/setting "
android:layout_width="24dip "
android:layout_height="24dip "
Android:layout _ alignParentBottom = " true "
Android:layout _ alignParentRight = " true "
Android:layout _ margin bottom = " 10 dip "
Android:layout _ margin right = " 10 dip "
Android:src = " @ drawable/I "/& gt;
& lt/relative layout & gt;
& lt線性布局
xmlns:Android = "/apk/RES/Android "
android:id="@+id/adlayout "
Android:layout _ width = " wrap _ content "
android:layout_height="50dip "
android:layout_gravity="center "
Android:background = " @ drawable/adbg " & gt;
& lt/linear layout & gt;
& lt/linear layout & gt;
& lt/linear layout & gt;
4.為ViewFlow編寫適配器:
公共類ViewFlowAdapter擴展BaseAdapter實現OnItemClickListener {
私人LayoutInflater mInflater
私有內存活動內存活動;//主活動
private int selected position = 0;
個人分發名單& lt列表& ltMemoBean & gt& gtlist = new ArrayList & lt列表& ltMemoBean & gt& gt();//要在ViewFlow中顯示的數據
公共畫廊適配器(MemoActivity activity,List & ltMemoBean & gtmemoBeanList) {
memoActivity =活動;
min flater = layoutinflater . from(activity);
initList(memoBeanList);
}
public void setSelectedPosition(int position){
this . selected position = position;
}
public void update data(List & lt;MemoBean & gtmemoBeanList) {
如果(列表!=null){
list . clear();
}
initList(memoBeanList);
}
@覆蓋
public int getCount() {
返回list . size();
}
@覆蓋
公共對象getItem(int position) {
返回list . get(position);
}
@覆蓋
public long getItemId(int position){
返回位置;
}
//初始化ViewFlow每個頁面的數據。
私有void initList(List & lt;MemoBean & gtmemoBeanList){
if(memoBeanList==null){
返回;
}
int total count = memobeanlist . size();
int pages = 0;
if(totalCount%Config。PAGE_SIZE==0){
頁數=總數/配置。PAGE _ SIZE//Config。PAGE_SIZE控制每頁顯示多少。
}否則{
頁數=總數/配置。PAGE _ SIZE+1;
}
for(int I = 1;我& lt=頁數;i++){
list.add(getCurrentPageList(i,memoBeanList));
}
}
//獲取當前頁面的數據
個人分發名單& ltMemoBean & gtgetCurrentPageList(int page,List & ltMemoBean & gtmemoBeanList) {
列表& ltMemoBean & gtlist = new ArrayList & ltMemoBean & gt();
if(memoBeanList!= null & amp& ampmemobeanlist . size()& gt;0){
int total count = memobeanlist . size();
int start = 0;
int end = 0;
開始= (page - 1) *配置。PAGE _ SIZE
結束=開始+配置。PAGE _ SIZE
if(end & gt;totalCount) {
end = totalCount
}
for(int I = start;我& lt結束;i++) {
list . add(memobean list . get(I));
}
}
退貨單;
}
//釋放ViewFlow占用的資源。
public void releaseViewFlow(int current position,ViewFlow viewFlow) {
列表& lt視圖& gtviews = viewFlow.mLoadedViews
int size = views . size();
for(int I = 0;我& lt尺寸;i++) {
如果(我!= currentPosition){ //我的ViewFlow每壹頁對應的布局都是線性布局,裏面有壹個GridView。
linear layout layout =(linear layout)views . get(I);
GridView GridView =(GridView)layout . get child at(0);
GridView adapter adapter =(GridView adapter)GridView . get adapter();
adapter . clear();//釋放GridView占用的資源
system . GC();
}
}
}
@覆蓋
公共視圖getView(int position,View convertView,ViewGroup parent) {
if (convertView == null) {
列表& ltMemoBean & gtmemos = list . get(position);
//viewflow_page這個布局對應的是如何顯示每個頁面,包含GridView的LinearLayout。
convert view = min flater . inflate(r . layout . view flow _ page,null);
GridView GridView =(GridView)convert view . findview byid(r . id . GridView);
GridView . setonitemclicklistener(this);
GridView adapter adapter =(GridView adapter)GridView . get adapter();
if (adapter == null) {
adapter = new GallaryItemAdapter(memo activity,memos);
gridView.setAdapter(適配器);
}否則{
adapter.setMemoList(備忘錄);
adapter . notifydatasetchanged();
}
}
返回convertView
}
//點擊每個頁面中GridView的項的處理。
@覆蓋
public void onItemClick(adapter view & lt;?& gt父,視圖視圖,int位置,
長id) {
memo activity . finish();
Intent Intent = new Intent();
intent.setClass(memoActivity,edit activity . class);
MemoBean MemoBean =(MemoBean)((GridView)parent)
。getItemAtPosition(位置);
MemoTypeBean type bean = new MemoTypeBean(memo activity);
type bean = type bean . get bean(memo bean . getmemotypeid());
Bundle Bundle = new Bundle();
bundle . putserializable(" memo _ bean ",memo bean);
bundle . put serializable(" type _ id ",memo activity . selected typeid);
int start = selected position * Config。PAGE _ SIZE+位置;
bundle.putInt("當前位置",start);
intent . put extras(bundle);
memo activity . start activity(intent);
}
公共void clear(){
list . clear();
list = null
}
}
5.主活動MemoActivity的初始化視圖流代碼:
mview flow =(view flow)findViewById(r . id . home _ view flow);
列表& ltMemoBean & gtmemo list = mymemobean . getmemolistbytype(selected typeid);
view flow adapter adapter = new gallery adapter(this,memo list);
mViewFlow.setAdapter(適配器);
如果(memoList!= null & amp& ampmemo list . size()& gt;0){
mview flow . set selection(0);
}
註意:
因為在ViewFlow的onMeasure方法中,有如下代碼:
if (widthMode!= MeasureSpec。正是& amp& amp!isInEditMode()) {
拋出新的IllegalStateException(
" ViewFlow只能在精確模式下使用.");
}
final int height mode = measure spec . get mode(heightmaurespec);
if (heightMode!= MeasureSpec。正是& amp& amp!isInEditMode()) {
拋出新的IllegalStateException(
" ViewFlow只能在精確模式下使用.");
}
因此,ViewFlow的layout _ width和layout _ height在布局時屬於某種模式。按layout_weight寫,或者按dip寫死。