android開發最常用例子整理----(3)自訂ListView(自訂BaseAdapter實現)

來源:互聯網
上載者:User

標籤:android   listview   baseadapter   

        在上一個教程中,我們使用SimpleAdapter實現自訂ListView,但是有一點不方便的是,如果要對每一個ListView的item選項進行不同的樣式設定,就很麻煩,因為SimpleAdapter使用的是統一的風格樣式。如果要實現不同item使用不同的風格樣式,那麼就要通過整合BaseAdapter來實現。

一、ActivityMainActivity.java源碼:


public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//Step1String[] contentArray = new String[]{"內容1","內容2","內容3","內容4","內容5"};ListView lv = (ListView)findViewById(R.id.lv);//Step2ArrayList<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();for(int i=0; i<contentArray.length; i++){HashMap<String,Object> map = new HashMap<String,Object>();map.put("startTimeText", "22:00");            map.put("endTimeText", "23:00");            map.put("separator", R.drawable.separator1);            map.put("content", contentArray[i]);            data.add(map);}//Step3MyAdapter adapter = new MyAdapter(MainActivity.this,data);//Step4lv.setAdapter(adapter);}class MyAdapter extends BaseAdapter{private Context mContext;private List<HashMap<String,Object>>dataList;private int[] imageArray = {R.drawable.separator1,R.drawable.separator2,R.drawable.separator3,R.drawable.separator4,R.drawable.separator5};public MyAdapter(Context context, List<HashMap<String,Object>>dataList) {this.mContext = context;          this.dataList = dataList;}@Overridepublic int getCount() {return dataList.size();}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {//要想對不同的item選項進行設定,就要重寫getView方法來實現ViewHolder holder = null; if(convertView == null){holder = new ViewHolder();convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, null);holder.separatorImage = (ImageView)convertView.findViewById(R.id.separator);holder.content = (TextView)convertView.findViewById(R.id.content);holder.startTimeText = (TextView)convertView.findViewById(R.id.startTimeText);holder.endTimeText = (TextView)convertView.findViewById(R.id.endTimeText);convertView.setTag(holder);}else{holder = (ViewHolder)convertView.getTag();} //給listview下不同item選項設定不同的圖片樣式 holder.separatorImage.setImageResource(imageArray[position]);holder.content.setText((String)getItem(position).get("content"));holder.startTimeText.setText((String)getItem(position).get("startTimeText"));holder.endTimeText.setText((String)getItem(position).get("endTimeText"));return convertView;}@Overridepublic HashMap<String, Object> getItem(int position) {return dataList.get(position);}final class ViewHolder{ImageView separatorImage;TextView startTimeText;TextView endTimeText;TextView content;}}}

二、xml布局檔案
(1)MainActivity主介面的布局源碼activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ListView    android:id="@+id/lv" android:layout_width="match_parent"     android:layout_height="wrap_content"/></LinearLayout>
(2)ListView列表單元的布局源碼list_item.xml:
<?xml version="1.0" encoding="utf-8" ?> <LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"android:layout_height="match_parent" android:layout_width="match_parent"android:gravity="center_vertical"android:orientation="horizontal">       <LinearLayout         android:layout_width="0dp"        android:layout_height="wrap_content"android:layout_weight="2"android:gravity="center_vertical"android:orientation="vertical">          <TextView              android:id="@+id/startTimeText"              android:layout_height="wrap_content"  android:layout_width="match_parent"/>          <TextView              android:id="@+id/endTimeText"              android:layout_height="wrap_content"  android:layout_width="match_parent"/>      </LinearLayout>      <ImageView     android:id="@+id/separator"    android:layout_weight="1"     android:layout_width="0dp"android:layout_height="wrap_content" />  <TextView    android:id="@+id/content"      android:layout_width="0dp"    android:layout_height="wrap_content" android:layout_weight="12"/> </LinearLayout>

三、相關資源

res/drawable-mdpi/下的檔案:


 separator1.png   separator2.png    separator3.png     separator4.png     separator5.png


四、效果



android開發最常用例子整理----(3)自訂ListView(自訂BaseAdapter實現)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.