viewholder_item.xml 子布局文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent"> 6 7 8 9 <ImageView 10 android:id="@+id/iv" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" /> 13 14 <TextView 15 android:id="@+id/tv" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" /> 18 </LinearLayout>
ViewHolderAdapter.java
1 package sowell.oracle.com.listviewholder; 2 3 import android.content.Context; 4 import android.view.LayoutInflater; 5 import android.view.View; 6 import android.view.ViewGroup; 7 import android.widget.BaseAdapter; 8 import android.widget.ImageView; 9 import android.widget.TextView; 10 11 import java.util.List; 12 13 /** 14 * Created by 94965 on 2017/11/11. 15 */ 16 17 public class ViewHolderAdapter extends BaseAdapter { 18 19 private List<String> mData; //储存数据 20 private LayoutInflater mInflater; //实例化布局 21 22 public ViewHolderAdapter(Context context,List<String> data){ 23 super(); 24 this.mData=data; 25 mInflater=LayoutInflater.from(context); 26 } 27 28 @Override 29 public int getCount() { 30 return mData.size(); 31 } 32 33 @Override 34 public Object getItem(int position) { 35 return mData.get(position); 36 } 37 38 @Override 39 public long getItemId(int position) { 40 return position; 41 } 42 43 @Override 44 public View getView(int position, View convertView, ViewGroup parent) { 45 ViewHolder holder=null; 46 //判断是否缓存 47 if(convertView==null){ 48 holder=new ViewHolder(); 49 //通过LayoutInflater实例化布局 50 convertView=mInflater.inflate(R.layout.viewholder_item,null); 51 holder.img=(ImageView)convertView.findViewById(R.id.iv); 52 holder.title=(TextView)convertView.findViewById(R.id.tv); 53 convertView.setTag(holder); 54 }else { 55 //通过tag找到缓存‘的布局 56 holder=(ViewHolder)convertView.getTag(); 57 } 58 59 //设置布局中控件要显示的视图 60 holder.img.setBackgroundResource(R.mipmap.ic_launcher); 61 holder.title.setText(mData.get(position)); 62 return convertView; 63 } 64 65 66 67 public final class ViewHolder{ 68 public ImageView img; 69 public TextView title; 70 } 71 }
activity.xml 总布局文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 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="wrap_content" 6 android:orientation="horizontal" 7 tools:context="sowell.oracle.com.listviewholder.MainActivity"> 8 9 <ListView 10 android:id="@+id/lv" 11 android:layout_width="match_parent" 12 android:layout_height="match_parent"></ListView> 13 14 15 </LinearLayout>
MainActivity.java
1 package sowell.oracle.com.listviewholder; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.util.Log; 6 import android.widget.ListView; 7 import android.widget.Toast; 8 9 import java.util.ArrayList; 10 import java.util.List; 11 12 public class MainActivity extends AppCompatActivity { 13 14 public ListView listView; 15 public ViewHolderAdapter adapter; 16 public List<String> list; 17 public String string; 18 19 20 public void init(){ 21 22 list=new ArrayList<String>(); 23 for(int i=0;i<=20;i++) 24 { 25 string=""+i; 26 list.add(string); 27 } 28 adapter=new ViewHolderAdapter(this,list); 29 listView=(ListView)findViewById(R.id.lv); 30 listView.setAdapter(adapter); 31 } 32 33 34 @Override 35 protected void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 setContentView(R.layout.activity_main); 38 init(); 39 } 40 }