1. 程式人生 > >【Android】使用BaseAdapter實現複雜的ListView

【Android】使用BaseAdapter實現複雜的ListView

步驟

使用BaseAdapter實現複雜的ListView的步驟:

1. 資料你要準備好 List getData()。

2. 繼承ListActivity專有屏,不再需要setContentView(xxx)。 

3. 建立一個繼承自BaseAdapter的類。

4. 為List繫結介面卡 setListAdapter(adapter)。

5. 用傳統的方式來覆寫介面卡的getView函式  (從引數convertView裡對映佈局檔案,find各個控制元件填充資料)。

6. 改寫:加入ViewHolder類(定義n個控制元件的宣告) 。  用convertView.setTag(viewHolder)在View和Object之間進行關聯.。

7. 給按鈕註冊點選監聽器。可以用Toast或AlertDialogue彈出選擇項的資料。

friend_list.xml檔案

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <!-- 這是範例ListView的佈局檔案,出了ListView,還可以放置其他控制元件 -->
  3. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:layout_width="match_parent"
  5.     android:layout_height
    ="fill_parent"
  6.     android:background="#fff"
  7.     android:orientation="vertical">
  8.     <TextView
  9.         android:id="@+id/textView1"
  10.         android:layout_width="match_parent"
  11.         android:layout_height="50dp"
  12.         android:text="微信"
  13.         android:background="#2B3439"
  14.         android:gravity
    ="center"
  15.         android:textSize="20sp"
  16.         android:textColor="#FFFFFF"/>
  17.     <LinearLayout
  18.         android:layout_width="match_parent"
  19.         android:layout_height="wrap_content"
  20.         android:layout_marginLeft="18dp"
  21.         android:layout_marginRight="18dp"
  22.         android:layout_marginTop="2dp"
  23.         android:layout_marginBottom="2dp"
  24.         android:background="@drawable/btn_style_four_normal">
  25.         <ImageView
  26.             android:id="@+id/imageView1"
  27.             android:layout_width="wrap_content"
  28.             android:layout_height="wrap_content"
  29.             android:src="@drawable/sm_searchbtn"
  30.             android:layout_marginRight="10dp"/>
  31.         <EditText
  32.             android:id="@+id/editText1"
  33.             android:layout_width="match_parent"
  34.             android:layout_height="35dp"
  35.             android:background="@null"
  36.             android:ems="10">
  37.             <requestFocus/>
  38.         </EditText>
  39.     </LinearLayout>
  40.     <ListView
  41.         android:id="@+id/listView1"
  42.         android:layout_width="match_parent"
  43.         android:paddingBottom="50dp"
  44.         android:cacheColorHint="#00000000"
  45.         android:layout_height="match_parent">
  46.     </ListView>
  47. </LinearLayout>

friend_list_item.xml檔案

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <!-- 這是列表項的佈局檔案,每一行長什麼樣子,修改這裡 -->
  3. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="80dp"
  6.     android:orientation="horizontal"
  7.     android:padding="5dip"
  8.     android:paddingBottom="15dp">
  9.     <ImageView
  10.         android:id="@+id/img"
  11.         android:layout_width="wrap_content"
  12.         android:layout_height="wrap_content"
  13.         android:layout_margin="5dp"/>
  14.     <LinearLayout
  15.         android:layout_width="fill_parent"
  16.         android:layout_height="wrap_content"
  17.         android:orientation="vertical">
  18.         <LinearLayout
  19.             android:layout_width="fill_parent"
  20.             android:layout_height="wrap_content"
  21.             android:orientation="horizontal">
  22.             <TextView
  23.                 android:id="@+id/title"
  24.                 android:layout_width="wrap_content"
  25.                 android:layout_height="wrap_content"
  26.                 android:textColor="#000"
  27.                 android:textSize="20sp"/>
  28.             <TextView
  29.                 android:id="@+id/time"
  30.                 android:layout_width="wrap_content"
  31.                 android:layout_height="wrap_content"
  32.                 android:layout_marginLeft="110dp"
  33.                 android:textColor="#000"
  34.                 android:textSize="18sp"/>
  35.         </LinearLayout>
  36. 相關推薦

    Android如何實現Android發送短信

    ted param close ase find array 短信 red phone 第一種:調用系統短信接口直接發送短信;主要代碼如下: /** * 直接調用短信接口發短信 * @param phoneNumber * @

    Android簡單實現使用WebView元件實現在App內開啟web

    關於WebView元件 如何實現在App內嵌web 在新建的android專案裡,如果要實現內嵌Web,一定要在AndroidManifest.xml設定訪問網路許可權: <uses-permission android:name="andr

    AndroidEditText實現搜尋功能,把鍵盤迴車鍵改為搜尋;EditText隱藏游標

    1、xml檔案中設定屬性 android:imeOptions="actionSearch"                 android:singleLine="true" 注:android:singleLine 已過期,不過設定為maxlines = 1  不會將回

    Android- OkHttp實現斷點續傳

    1.匯入依賴包 //retrofit, 基於Okhttp,考慮到專案中經常會用到retrofit,就匯入這個了。 compile 'com.squareup.retrofit2:retrofit:2.1.0' //ButterKnife compile 'com.jakew

    Android快速實現仿美團選擇城市介面,微信通訊錄介面

    概述 本文是這個系列的第三篇,不出意外也是終結篇。因為使用經過重構後的控制元件已經可以快速實現市面上帶 索引導航、懸停分組的列表介面了。 在前兩篇裡,我們從0開始,一步一步實現了仿微信通訊錄、餓了麼選餐介面。 (第一篇戳我 第二篇戳我) 這篇文章作為終結

    Android使用BaseAdapter實現複雜ListView

    步驟 使用BaseAdapter實現複雜的ListView的步驟: 1. 資料你要準備好 List getData()。 2. 繼承ListActivity專有屏,不再需要setContentView(xxx)。  3. 建立一個繼承自Ba

    Android RecyclerView、ListView實現單選列表的優雅之路.

    一 概述: 這篇文章需求來源還是比較簡單的,但做的優雅仍有值得挖掘的地方。 需求來源:一個類似餓了麼這種電商優惠券的選擇介面: 其實就是 一個普通的列表,實現了單選功能, 效果如圖: (不要怪圖渣了,我擼了四五遍,公司錄出來的GIF就這麼渣

    Android用RecycleView實現可以橫向滾動的ListView效果

    終於閒下來了,總結一下RecycleView的使用。 一、概述 與常見的ListView和GridView一樣,RecycleView也用來在有限的介面上展示大量的資料。它提供了一種插拔式的體驗,高度的解耦,使用非常靈活,可以通過support-v7包進行匯入。先看以下Re

    AndroidAndroid聊天機器人實現

    小米 div bottom 曾經 圖靈 .9.png sdn http 歡迎界面 昨天看到一個Android視頻教程講圖靈機器人。那個API接口用起來還是挺方便的,就準備自己動手做一個了。另外自己還使用了高德地圖的API接口用於定位(曾經用過高德的接口,比X度方便) 大

    Android界面實現使用PagerTabStrip實現有滑動標簽的Viewpager

    n) range over 遊戲娛樂 and ring linear manager ray 在ViewPager這樣的能夠滑動的控件上,總是有非常多的文章能夠做。上次的文章。我們實現了一個自己定義的ViewPager的指示器。這篇文章,我們主要是想利用Andr

    Android界面實現SlidingMenu最新版本號使用具體解釋

    -1 引入 rem ade eas str 技術分享 state offset 轉載請註明出處:http://blog.csdn.net/zhaokaiqiang1992 在非常久之前的一篇文章中,簡單的介紹了一下開源項目SlidingMenu控件

    Android實現線程異步小技巧

    使用 msg xtend util rri wsh ride 執行 java 方式不止一種,這裏使用的是Timer類,創建一個定時器。我們經常需要獲得移動設備端口的顯示屏信息,但是onCreate()方法執行的時候,OnShow()方法不一定執行了,也就是說,在執行Oncr

    Android界面實現View Animation 使用介紹

    ref 轉載 HA ceshi 很多 ons 伸縮 ets 時間設置 ? ? 轉載請註明出處:http://blog.csdn.net/zhaokaiqiang19

    Android原始碼分析 - LRUCache快取實現原理

    一、Android中的快取策略 一般來說,快取策略主要包含快取的新增、獲取和刪除這三類操作。如何新增和獲取快取這個比較好理解,那麼為什麼還要刪除快取呢?這是因為不管是記憶體快取還是硬碟快取,它們的快取大小都是有限的。當快取滿了之後,再想其新增快取,這個時候就需要刪除一些舊的快取

    Android實現XML解析的幾種技術

    轉載地址:http://www.cnblogs.com/hanyonglu/archive/2012/02/28/2370675.html  謝謝。 本文介紹在Android平臺中實現對XML的三種解析方式。   XML在各種開發中

    Android打電話的兩種實現方式

    第一種實現方式:直接給聯絡人打電話 Intent intent=new Intent(); intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:110")); startActivity(intent);

    android如何用RedioGroup實現ios中TabBar的效果·

    一、xml設定 <RadioGroup android:id="@+id/rg_tab" android:layout_width="fill_parent" android:layo

    Android功能設計儲存帳號密碼,自動登入,離線登入實現方案

    勾選【記住密碼】【自動登入】複選框時: 什麼也不做,所有操作放在點選【登入】按鈕時執行 點選【登入】按鈕時: 為了簡化程式碼和實現邏輯,不管密碼對錯,登入資訊統一儲存到【上次登入帳號】【上次登入密碼】【是否儲存密碼】【是否自動登入】配置 登入成功

    Android中微信搶紅包助手的實現(程式碼整理)

    實現原理   通過利用AccessibilityService輔助服務,監測螢幕內容,如監聽狀態列的資訊,螢幕跳轉等,以此來實現自動拆紅包的功能。關於AccessibilityService輔助服務,可以自行百度瞭解更多。 程式碼基礎: 1.首先宣告一個RedPac

    Android一、Progress進度條實現的三種方式:主執行緒實現,Service載入,動態建立

    前言 更新版本,上傳資料到服務端,都是需要進度顯示的,Android進度顯示兩種方式 ProgressDialog 和 ProgressBar 新版本中ProgressDialog不被推薦使用,所以專案採用ProgressBar 分為三種實現方式: 1、MainAct