码迷,mamicode.com
首页 > 移动开发 > 详细

Android笔记:ListView

时间:2015-08-18 15:39:51      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

listview属性

  android:divider属性,可以指定ListView 分隔线的颜色,#0000 表示将分隔线设为透明色。

listview效率的问题

  adapter的三个参数int position, View convertView, ViewGroup parent

    1,直接使用view 每次打开每次加载,效率最低。

      LayoutInflater inflater = LayoutInflater.from(mContext);

      View view1 = inflater.inflate(R.layout.fruit_item, null);

    2,使用convertView(缓存)

      if (convertView == null) {
         view = LayoutInflater.from(getContext()).inflate(resourceId, null);
       } else {
         view = convertView;
       }

    3,将控件的加载也加入缓存

      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, null);
          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
        }
        viewHolder.fruitImage.setImageResource(fruit.getImageId());
        viewHolder.fruitName.setText(fruit.getName());
        return view;
      }
      class ViewHolder {
        ImageView fruitImage;
        TextView fruitName;
      }

listview点击事件

  ListView listView = (ListView) findViewById(R.id.list_view);   

  listView.setAdapter(adapter);   

  listView.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent, View view,      int position, long id) {  

      Fruit fruit = fruitList.get(position);     

      Toast.makeText(MainActivity.this, fruit.getName(), 0).show();

    }

  });

 

Android笔记:ListView

标签:

原文地址:http://www.cnblogs.com/lxwy1992/p/4739305.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!