美文网首页
深入浅出ListView

深入浅出ListView

作者: Wide_Star | 来源:发表于2017-09-29 14:33 被阅读0次

    1 简单使用ListView:

    在布局文件中添加ListView控件
    通过适配器将数据传递给ListView:

    private String[] data={"zhangsan","lisi","wangwu","dengdeng"};
    
    ArrayAdapter adapter=new ArrayAdapter (MainActivity.this,
                   android.R.layout.simple_list_item_1, data);
    ListView listView=(ListView)findViewById(R.id.list_view);
    listView.setAdapter(adapter);
    

    其中android.R.layout.simple_list_item_1是为ListView指定的子项布局的id

    2 定制ListView界面

    通俗的说:将两种原料(容器中装的数据集和ListView的子项的布局文件)在加工器(适配器Adapter)的加工下一一匹配(布局文件中控件要显示的内容和数据集中的各个数据项一一对应)。

    本例中是将水果和对应的名称作为数据显示在ListView中

    2.1 数据集

    自定义一个类,将要显示的数据作为对象属性封装在类中

    public class Fruit {
       private String name;
       private int imageId;
       public Fruit(String name, int imageId) {
           this.name = name;
           this.imageId = imageId;
       }
       public String getName() {
           return name;
       }
       public int getimageId() {
           return imageId;
       }
    }
    

    2.2 子项布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="horizontal"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <ImageView
       android:id="@+id/fruit_image"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />
       <TextView
           android:id="@+id/fruit_name"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="center_vertical"
           android:layout_margin="10dp"/>
    </LinearLayout>
    

    2.3 自定义适配器

    public class FruitAdapter extends ArrayAdapter<Fruit> {
     private int resourceId;
     public FruitAdapter(Context context, int resource, List<Fruit> objects) {
         super(context, resource, objects);
         resourceId=resource;
     }
    
     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
         Fruit fruit=getItem(position);
         View view= LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
         ImageView fruitImage=(ImageView)view.findViewById(R.id.fruit_image);
         TextView fruitName=(TextView)view.findViewById(R.id.fruit_name);
         fruitImage.setImageResource(fruit.getimageId());
         fruitName.setText(fruit.getName());
         return view;
     }
    }
    

    2.4 为ListView设定适配器,显示数据

    public class MainActivity extends AppCompatActivity {
    
     private List<Fruit> fruitList=new ArrayList<>();
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         initFruits();
         FruitAdapter adapter=new FruitAdapter(MainActivity.this,
                 R.layout.fruit_item,fruitList);
         ListView listView=(ListView)findViewById(R.id.list_view);
         listView.setAdapter(adapter);
     }
     private void initFruits(){
         for(int i=0;i<4;i++) {
             Fruit apple = new Fruit("苹果", R.drawable.apple);
             fruitList.add(apple);
             Fruit caomei = new Fruit("草莓", R.drawable.caomei);
             fruitList.add(caomei);
             Fruit migua = new Fruit("蜜瓜", R.drawable.migua);
             fruitList.add(migua);
             Fruit orange = new Fruit("橘子", R.drawable.orange);
             fruitList.add(orange);
             Fruit putao = new Fruit("葡萄", R.drawable.putao);
             fruitList.add(putao);
             Fruit qiyiguo = new Fruit("奇异果", R.drawable.qiyiguo);
             fruitList.add(qiyiguo);
         }
     }
    }
    

    3 优化,提升ListView效率

    • 为避免快速滑动时重复加载布局重新加载,注意到自定义的适配器中getView()方法中有一个参数convertView,这个参数用于将之前加载好的布局进行缓存,以便之后可以进行重用,所以修改FruitAdapter中的代码如下:
    @Override
       public View getView(int position, View convertView, ViewGroup parent) {
           Fruit fruit=getItem(position);
           View view;
           if(convertView==null){
               view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
           }
           else{
               view=convertView;
           }
           ImageView fruitImage=(ImageView)view.findViewById(R.id.fruit_image);
           TextView fruitName=(TextView)view.findViewById(R.id.fruit_name);
           fruitImage.setImageResource(fruit.getimageId());
           fruitName.setText(fruit.getName());
           return view;    
       }
    

    可以看到,现在我们在getView()方法中进行了判断,如果convertView为null,则使用Layout工nflater去加载布局,如果不为null则直接对convertView进行重用。这样就大大提高了ListView的运行效率,在快速滚动的时候也可以表现出更好的性能。

    • 不过,目前我们的这份代码还是可以继续优化的,虽然现在已经不会再重复去加载布局,但是每次在getView()方法中还是会调用View的findViewById()方法来获取一次控件的实例,我们可以借助一个ViewHolder来对这部分性能进行优化,修改FruitAdapter中的代码,如下所示:
    @Override
       public View getView(int position, View convertView, ViewGroup parent) {
           Fruit fruit=getItem(position);
           View view;
           ViewHolder viewHolder;
           if(convertView==null){
               view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
               viewHolder=new ViewHolder();
               viewHolder.fruitImage=(ImageView)view.findViewById(R.id.fruit_image);
               viewHolder.fruitName=(TextView)view.findViewById(R.id.fruit_name);
               view.setTag(viewHolder);//将viewHolder存储在view中
           }
           else{
               view=convertView;
               viewHolder=(ViewHolder) view.getTag();
           }
           viewHolder.fruitImage.setImageResource(fruit.getimageId());
           viewHolder.fruitName.setText(fruit.getName());
           return view;
       }
       class ViewHolder{
           ImageView fruitImage;
           TextView fruitName;
       }
    

    我们新增了一个内部类ViewHolder,用于对控件的实例进行缓存。当convertView为null 的时候,创建一个ViewHolder对象,并将控件的实例都存放在ViewHolder里,然后调用View 的setTag()方法,将ViewHolder对象存储在View中。当convertView不为null的时候,
    则调用View的getTag()方法,把ViewHolder重新取出。这样所有控件的实例都缓存在了 ViewHolder里,就没有必要每次都通过findViewBy工do方法来获取控件实例了。
    通过这两步优化之后,我们ListView的运行效率就已经非常不错了。

    4 ListView的点击事件

    • 为ListView设置子项监听器,修改代码如下:
    public class MainActivity extends AppCompatActivity {
       private List<Fruit> fruitList=new ArrayList<>();
       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
           ActionBar actionBar=getSupportActionBar();
           if(actionBar!=null){
               actionBar.hide();
           }
           initFruits();
           FruitAdapter adapter=new FruitAdapter(MainActivity.this,
                   R.layout.fruit_item,fruitList);
           ListView listView=(ListView)findViewById(R.id.list_view);
           listView.setAdapter(adapter);
           listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
               @Override
               public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                   Fruit fruit=fruitList.get(position);
                   Toast.makeText(MainActivity.this,fruit.getName(),Toast.LENGTH_SHORT).show();
               }
           });
       }
       private void initFruits(){
           for(int i=0;i<10;i++) {
               Fruit apple = new Fruit("苹果", R.drawable.apple);
               fruitList.add(apple);
               Fruit caomei = new Fruit("草莓", R.drawable.caomei);
               fruitList.add(caomei);
               Fruit migua = new Fruit("蜜瓜", R.drawable.migua);
               fruitList.add(migua);
               Fruit orange = new Fruit("橘子", R.drawable.orange);
               fruitList.add(orange);
               Fruit putao = new Fruit("葡萄", R.drawable.putao);
               fruitList.add(putao);
               Fruit qiyiguo = new Fruit("奇异果", R.drawable.qiyiguo);
               fruitList.add(qiyiguo);
           }
       }
    }
    

    相关文章

      网友评论

          本文标题:深入浅出ListView

          本文链接:https://www.haomeiwen.com/subject/eueiextx.html