制作listView高效率万能适配器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了制作listView高效率万能适配器相关的知识,希望对你有一定的参考价值。
本案例学习来自 _鸿洋_ 大神,自己在写一遍,加深记忆http://blog.csdn.net/lmj623565791/article/details/38902805
我们都是以前写listView 都是这个样子
1 package com.example.listviewdemo; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import android.app.Activity; 7 import android.os.Bundle; 8 import android.widget.ListView; 9 10 public class MainActivity extends Activity { 11 12 private ListView lv ; 13 private List<String> list = new ArrayList<String>(); 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 initData(); 20 lv = (ListView) findViewById(R.id.id_main_lv); 21 MyAdapter<String> adapter = new MyAdapter<String>(list,this); 22 lv.setAdapter(adapter); 23 } 24 25 private void initData() { 26 for(int i=0;i<30;i++){ 27 list.add("qqqqq"+i); 28 } 29 } 30 31 32 }
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 > 6 7 <ListView 8 android:id="@+id/id_main_lv" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="@string/hello_world" /> 12 13 </RelativeLayout>
1 package com.example.listviewdemo; 2 3 import java.util.List; 4 5 import android.content.Context; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.BaseAdapter; 9 import android.widget.TextView; 10 11 public class MyAdapter<T> extends BaseAdapter { 12 13 private List<T> list; 14 private Context mContext; 15 public MyAdapter(List<T> list,Context mContext) { 16 super(); 17 this.list = list; 18 this.mContext = mContext; 19 } 20 21 22 @Override 23 public int getCount() { 24 // TODO Auto-generated method stub 25 return list.size(); 26 } 27 28 @Override 29 public Object getItem(int position) { 30 // TODO Auto-generated method stub 31 return list.get(position); 32 } 33 34 @Override 35 public long getItemId(int position) { 36 // TODO Auto-generated method stub 37 return position; 38 } 39 40 @Override 41 public View getView(int position, View convertView, ViewGroup parent) { 42 43 ViewHolder holder = null; 44 if(convertView == null){ 45 holder = new ViewHolder(); 46 convertView = View.inflate(mContext, R.layout.item_listview, null); 47 holder.tv = (TextView) convertView.findViewById(R.id.item_tv); 48 convertView.setTag(holder); 49 }else{ 50 holder = (ViewHolder) convertView.getTag(); 51 } 52 holder.tv.setText((String)list.get(position)); 53 System.out.println((String)list.get(position)); 54 return convertView; 55 } 56 57 class ViewHolder{ 58 TextView tv ; 59 } 60 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:layout_gravity="center" 6 android:orientation="vertical" > 7 8 <TextView 9 android:id="@+id/item_tv" 10 android:textSize="15sp" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content"/> 13 14 </LinearLayout>
以上是关于制作listView高效率万能适配器的主要内容,如果未能解决你的问题,请参考以下文章
android 打造ListView和Gridview万能adapter适配器(附源码)
Android 快速开发系列 打造万能的ListView GridView 适配器