BaseAdapter和ListView的设计
步骤:1新建activity_main.xml布局,包括ListView控件
2新建main.xml布局,为每个子控件设置属性
3新建数据源ItemBean类,里面包括子控件需要的东西,
4新建MainActivity类,加载数据源,用List集合,之后初始化适配器,加载适配器
5新建适配器MyAdapt,继承BaseAdapter
注意:把xml文件转换为view视图,用View view=LayouInflater.from(context).inflater(R.layout.main,null);
Main
1 package baseadaptdemo.example.administrator.baseadaptdemo; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.widget.ListView; 6 7 import java.util.ArrayList; 8 import java.util.List; 9 10 public class MainActivity extends AppCompatActivity { 11 private List<ItemBean> beanList; 12 private MyAdapt myAdapt; 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_main); 17 ListView listView= (ListView) findViewById(R.id.list_item); 18 beanList=new ArrayList<>(); 19 for (int i=0;i<20;i++){ 20 beanList.add(new ItemBean(R.mipmap.ic_launcher, 21 "我是标题"+i, 22 "我是内容"+i)); 23 } 24 myAdapt=new MyAdapt(this,beanList); 25 listView.setAdapter(myAdapt); 26 } 27 }
ItemBean类
1 package baseadaptdemo.example.administrator.baseadaptdemo; 2 3 import android.widget.ImageView; 4 import android.widget.TextView; 5 6 /** 7 * Created by Administrator on 2016/5/9. 8 */ 9 /*alt+insert:调用出构造方法*/ 10 public class ItemBean { 11 12 public int itemImageResid; 13 public String itemTitle; 14 public String itemContent; 15 16 public ItemBean(int itemImageResid, String itemTitle, String itemContent) { 17 this.itemImageResid = itemImageResid; 18 this.itemTitle = itemTitle; 19 this.itemContent = itemContent; 20 } 21 }
MyAdapt
1 package baseadaptdemo.example.administrator.baseadaptdemo; 2 3 import android.content.Context; 4 import android.provider.Settings; 5 import android.util.Log; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.BaseAdapter; 10 import android.widget.ImageView; 11 import android.widget.TextView; 12 13 import java.util.List; 14 15 /** 16 * Created by Administrator on 2016/5/9. 17 */ 18 public class MyAdapt extends BaseAdapter { 19 private List<ItemBean> mList; 20 private LayoutInflater mLayoutInflater; 21 22 public MyAdapt(Context context,List<ItemBean> beanList) { 23 this.mList=beanList; 24 mLayoutInflater=LayoutInflater.from(context);/*为minflater初始化*/ 25 } 26 27 @Override 28 public int getCount() { 29 return mList.size(); 30 } 31 32 @Override 33 public Object getItem(int position) { 34 return mList.get(position); 35 } 36 37 @Override 38 public long getItemId(int position) { 39 return position; 40 } 41 42 @Override 43 public View getView(int position, View convertView, ViewGroup parent) { 44 long start=System.nanoTime();/*获取纳秒时间*/ 45 /*逗比式*/ 46 /*View view=mLayoutInflater.inflate(R.layout.main,null); 47 ImageView imageView= (ImageView) view.findViewById(R.id.iv_image); 48 TextView tv_title= (TextView) view.findViewById(R.id.tv_title); 49 TextView tv_context= (TextView) view.findViewById(R.id.tv_context); 50 imageView.setImageResource(mList.get(position).itemImageResid); 51 tv_title.setText(mList.get(position).itemTitle); 52 tv_context.setText(mList.get(position).itemContent); 53 return view;*/ 54 /*普通式*/ 55 /*if (convertView == null) { 56 convertView = mLayoutInflater.inflate(R.layout.main, null); 57 } 58 ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_image); 59 TextView tv_title = (TextView) convertView.findViewById(R.id.tv_title); 60 TextView tv_context = (TextView) convertView.findViewById(R.id.tv_context); 61 imageView.setImageResource(mList.get(position).itemImageResid); 62 tv_title.setText(mList.get(position).itemTitle); 63 tv_context.setText(mList.get(position).itemContent); 64 return convertView;*/ 65 66 ViewHolder viewHolder=null; 67 if (convertView == null) { 68 convertView = mLayoutInflater.inflate(R.layout.main, null); 69 viewHolder=new ViewHolder(); 70 viewHolder.imageView = (ImageView) convertView.findViewById(R.id.iv_image); 71 viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title); 72 viewHolder.tv_context = (TextView) convertView.findViewById(R.id.tv_context); 73 convertView.setTag(viewHolder); 74 }else{ 75 viewHolder= (ViewHolder) convertView.getTag(); 76 } 77 78 viewHolder. imageView.setImageResource(mList.get(position).itemImageResid); 79 viewHolder. tv_title.setText(mList.get(position).itemTitle); 80 viewHolder.tv_context.setText(mList.get(position).itemContent); 81 long end= System.nanoTime(); 82 long dValue=end-start; 83 long sum=0; 84 sum=sum+dValue; 85 Log.d("Main","sum"); 86 return convertView; 87 88 89 } 90 class ViewHolder{ 91 public ImageView imageView; 92 public TextView tv_title; 93 public TextView tv_context; 94 } 95 }
activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="baseadaptdemo.example.administrator.baseadaptdemo.MainActivity"> 11 12 <ListView 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:id="@+id/list_item"></ListView> 16 </RelativeLayout>
main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 > 7 <ImageView 8 android:layout_width="80dp" 9 android:layout_height="80dp" 10 android:id="@+id/iv_image" 11 android:background="@mipmap/ic_launcher"/> 12 <TextView 13 android:layout_width="match_parent" 14 android:layout_height="40dp" 15 android:layout_toEndOf="@+id/iv_image" 16 android:gravity="center" 17 android:textSize="25sp" 18 android:id="@+id/tv_title" 19 android:text="Title"/> 20 <TextView 21 android:layout_width="match_parent" 22 android:layout_height="40dp" 23 android:gravity="center" 24 android:id="@+id/tv_context" 25 android:layout_below="@+id/tv_title" 26 android:layout_toEndOf="@+id/iv_image" 27 android:text="context" 28 android:textSize="20sp"/><!--layout_below用于设置在一控件之下 29 layout_toEndOf用于设置在一控件末尾--> 30 </RelativeLayout>