效果图如下:
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 29 30 @Override 31 public int getCount() { 32 return mData.size(); 33 } 34 35 @Override 36 public Object getItem(int position) { 37 return mData.get(position); 38 } 39 40 @Override 41 public long getItemId(int position) { 42 return position; 43 } 44 45 @Override 46 public View getView(int position, View convertView, ViewGroup parent) { 47 ViewHolder holder=null; 48 //判断是否缓存 49 if(convertView==null){ 50 holder=new ViewHolder(); 51 //通过LayoutInflater实例化布局 52 convertView=mInflater.inflate(R.layout.viewholder_item,null); 53 holder.img=(ImageView)convertView.findViewById(R.id.iv); 54 holder.title=(TextView)convertView.findViewById(R.id.tv); 55 convertView.setTag(holder); 56 }else { 57 //通过tag找到缓存‘的布局 58 holder=(ViewHolder)convertView.getTag(); 59 } 60 61 //设置布局中控件要显示的视图 62 holder.img.setBackgroundResource(R.mipmap.ic_launcher); 63 holder.title.setText(mData.get(position)); 64 return convertView; 65 } 66 67 68 69 public final class ViewHolder{ 70 public ImageView img; 71 public TextView title; 72 } 73 }
activity_main.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="vertical" 7 tools:context="sowell.oracle.com.listviewholder.MainActivity"> 8 9 10 11 12 <ListView 13 android:id="@+id/lv" 14 android:layout_width="match_parent" 15 android:layout_height="450dp" 16 android:divider="@android:color/darker_gray" 17 android:dividerHeight="1dp" 18 > 19 </ListView> 20 21 22 23 24 <Button 25 android:id="@+id/btn" 26 android:layout_width="match_parent" 27 android:layout_height="wrap_content" 28 android:text="add" /> 29 30 31 <LinearLayout 32 android:layout_width="match_parent" 33 android:layout_height="wrap_content" 34 android:orientation="horizontal" 35 > 36 <EditText 37 android:id="@+id/et" 38 android:layout_weight="1" 39 android:layout_width="wrap_content" 40 android:layout_height="wrap_content" /> 41 <Button 42 android:id="@+id/btn1" 43 android:layout_weight="1" 44 android:onClick="moveto" 45 android:text="移动" 46 android:layout_width="wrap_content" 47 android:layout_height="wrap_content" /> 48 </LinearLayout> 49 50 51 </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.view.View; 7 import android.view.WindowManager; 8 import android.widget.Button; 9 import android.widget.EditText; 10 import android.widget.ListView; 11 import android.widget.Toast; 12 13 import java.util.ArrayList; 14 import java.util.List; 15 16 public class MainActivity extends AppCompatActivity { 17 18 public ListView listView; 19 public ViewHolderAdapter adapter; 20 public List<String> list; 21 public String string; 22 public Button bt; 23 public Button move; 24 private EditText et; 25 public void init(){ 26 27 move=(Button)findViewById(R.id.btn1); 28 et=(EditText)findViewById(R.id.et); 29 bt=(Button)findViewById(R.id.btn); 30 bt.setOnClickListener(new View.OnClickListener() { 31 @Override 32 public void onClick(View v) { 33 add(); 34 } 35 }); 36 list=new ArrayList<String>(); 37 for(int i=0;i<=20;i++) 38 { 39 string=""+i; 40 list.add(string); 41 } 42 adapter=new ViewHolderAdapter(this,list); 43 listView=(ListView)findViewById(R.id.lv); 44 listView.setAdapter(adapter); 45 46 } 47 48 49 @Override 50 protected void onCreate(Bundle savedInstanceState) { 51 super.onCreate(savedInstanceState); 52 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); //防止被输入框被遮盖 53 setContentView(R.layout.activity_main); 54 init(); 55 } 56 57 public void moveto(View view){ 58 int position=Integer.parseInt(et.getText().toString()); 59 if(position>=0&&position<=list.size()-1) 60 listView.smoothScrollByOffset(position);//平滑移动到布局第一个元素为position 61 //listView.smoothScrollToPosition(position); 62 } 63 64 public void add(){ 65 list.add("new"); 66 adapter.notifyDataSetChanged(); //动态添加listview中的数据 67 //listView.setSelection(list.size()-1);//瞬间移动 68 listView.smoothScrollToPosition(list.size()-1);//平滑移动到布局中有该位置 69 70 } 71 }