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();

    }

  });

04-15 14:44