日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android: BaseAdapter 实现分页

發布時間:2025/6/15 Android 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android: BaseAdapter 实现分页 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
小記記代碼 ?
  • ?列表的顯示需要三個元素:??
  • 1.ListVeiw?用來展示列表的View。??
  • 2.適配器?用來把數據映射到ListView上的中介。??
  • 3.數據????具體的將被映射的字符串,圖片,或者基本組件。??
  • ??
  • 根據列表的適配器類型,列表分為三種,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter??
  • ??
  • 其中以ArrayAdapter最為簡單,只能展示一行字。??
  • ??
  • SimpleAdapter有最好的擴充性,可以自定義出各種效果。???
  • ??
  • SimpleCursorAdapter可以認為是SimpleAdapter對數據庫的簡單結合,可以方便的把數據庫的內容以列表的形式展示出來。??
  • ?

    ?

    分頁效果圖:


    ?

    Xml代碼 ?
  • ?<ListView??android:id="@+id/list"??
  • ?????android:layout_width="fill_parent"???
  • ?????android:layout_height="wrap_content"??/>??
  • ??
  • lt;LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  • android:orientation="horizontal"?android:layout_width="fill_parent"??
  • android:layout_height="wrap_content"?android:gravity="bottom">??
  • ??
  • ?<Button??android:id="@+id/btnLeft"??
  • ?????android:layout_width="150dip"???
  • ?????android:layout_height="wrap_content"???
  • ?????android:text="Previous?Page"?/>??
  • ???????
  • ?<Button??android:id="@+id/btnRight"??
  • ?????android:layout_width="150dip"???
  • ?????android:layout_height="wrap_content"???
  • ?????android:text="Next?Page"?/>??
  • </LinearLayout>??
  • ?

    Java代碼 ?
  • import?android.app.Activity;??
  • import?android.os.Bundle;??
  • import?android.view.Gravity;??
  • import?android.view.View;??
  • import?android.view.ViewGroup;??
  • import?android.widget.BaseAdapter;??
  • import?android.widget.Button;??
  • import?android.widget.ListView;??
  • import?android.widget.TextView;??
  • ??
  • public?class?listMoreTest?extends?Activity?{??
  • ??????
  • ????int?VIEW_COUNT?=?10;??
  • ????int?index?=?0;??
  • ??????
  • ????ListView?listView;??
  • ????Button?btnLeft,?btnRight;??
  • ????View.OnClickListener?clickListener;??
  • ????MyAdapter?myAdapter;??
  • ????String[]?data?=?{?"0",?"1",?"2",?"3",?"4",?"5",?"6",?"7",?"8",?"9",?"10",??
  • ????????????"11",?"12",?"13",?"14",?"15",?"16",?"17",?"18",?"19",?"20",?"21",??
  • ????????????"22",?"23",?"24",?"25",?"26",?"27",?"28",?"29",?"30",?"31",?"32",??
  • ????????????"33",?"34",?"35",?"36",?"37"?};??
  • ??
  • ????@Override??
  • ????public?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????super.onCreate(savedInstanceState);??
  • ????????setContentView(R.layout.listview);??
  • ??
  • ????????initView();??
  • ??
  • ????????//?設置ListView的Adapter??
  • ????????myAdapter?=?new?MyAdapter(this);??
  • ????????//?TODO?此處是雙向綁定嗎???
  • ????????listView.setAdapter(myAdapter);??
  • ??
  • ????????clickListener?=?new?Button.OnClickListener()?{??
  • ????????????@Override??
  • ????????????public?void?onClick(View?v)?{??
  • ????????????????switch?(v.getId())?{??
  • ????????????????case?R.id.btnLeft:??
  • ????????????????????leftView();??
  • ????????????????????break;??
  • ????????????????case?R.id.btnRight:??
  • ????????????????????rightView();??
  • ????????????????????break;??
  • ????????????????}??
  • ????????????}??
  • ????????};??
  • ????????btnLeft.setOnClickListener(clickListener);??
  • ????????btnRight.setOnClickListener(clickListener);??
  • ??
  • ????????changeButtonStatus();??
  • ????}??
  • ??
  • ????public?void?initView()?{??
  • ????????listView?=?(ListView)?findViewById(R.id.list);??
  • ????????btnLeft?=?(Button)?findViewById(R.id.btnLeft);??
  • ????????btnRight?=?(Button)?findViewById(R.id.btnRight);??
  • ????}??
  • ??
  • ????/**?
  • ?????*?點擊左邊的Button,表示向前翻頁,索引值要減1.?
  • ?????*/??
  • ????public?void?leftView()?{??
  • ????????index--;??
  • ????????//?刷新ListView里面的數值。??
  • ????????myAdapter.notifyDataSetChanged();??
  • ????????changeButtonStatus();??
  • ????}??
  • ??
  • ????/**?
  • ?????*?點擊右邊的Button,表示向后翻頁,索引值要加1.?
  • ?????*/??
  • ????public?void?rightView()?{??
  • ????????index++;??
  • ????????//?刷新ListView里面的數值。??
  • ????????myAdapter.notifyDataSetChanged();??
  • ????????changeButtonStatus();??
  • ????}??
  • ??
  • ????/**?
  • ?????*?變更btnLeft與btnRight按鈕是否可用。?
  • ?????*/??
  • ????public?void?changeButtonStatus()?{??
  • ????????if?(index?<=?0)?{??
  • ????????????btnLeft.setEnabled(false);??
  • ????????}?else?if?(data.length?-?index?*?VIEW_COUNT?<=?VIEW_COUNT)?{??
  • ????????????btnRight.setEnabled(false);??
  • ????????}?else?{??
  • ????????????btnLeft.setEnabled(true);??
  • ????????????btnRight.setEnabled(true);??
  • ????????}??
  • ????}??
  • ??
  • ????//?ListView的Adapter,這個是關鍵的導致可以分頁的根本原因。??
  • ????public?class?MyAdapter?extends?BaseAdapter?{??
  • ????????Activity?activity;??
  • ??
  • ????????public?MyAdapter(Activity?a)?{??
  • ????????????activity?=?a;??
  • ????????}??
  • ??
  • ????????/**?
  • ?????????*?設置每一頁的長度,默認的是View_Count的值。?
  • ?????????*/??
  • ????????@Override??
  • ????????public?int?getCount()?{??
  • ????????????//?ori表示到目前為止的前幾頁的總共的個數。??
  • ????????????int?ori?=?VIEW_COUNT?*?index;??
  • ??
  • ????????????if?(data.length?-?ori?<?VIEW_COUNT)?{??
  • ????????????????return?data.length?-?ori;??
  • ????????????}?else?{??
  • ????????????????return?VIEW_COUNT;??
  • ????????????}??
  • ????????}??
  • ??????????
  • ????????/**?
  • ?????????*?顯示TextView?data。?
  • ?????????*/??
  • ????????@Override??
  • ????????public?View?getView(int?position,?View?convertView,?ViewGroup?parent)?{??
  • ????????????//?return?addTestView(position);??
  • ????????????TextView?textView?=?new?TextView(activity);??
  • ????????????textView.setGravity(Gravity.LEFT);??
  • ????????????textView.setText(data[position?+?index?*?VIEW_COUNT]);??
  • ????????????return?textView;??
  • ????????}??
  • ??
  • ????????@Override??
  • ????????public?Object?getItem(int?position)?{??
  • ????????????return?position;??
  • ????????}??
  • ??
  • ????????@Override??
  • ????????public?long?getItemId(int?position)?{??
  • ????????????return?position;??
  • ????????}??
  • ????}??
  • }?
  • 總結

    以上是生活随笔為你收集整理的Android: BaseAdapter 实现分页的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。