• Android笔记:ListView


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

        }

      });

  • 相关阅读:
    深度学习
    !gcc !vi
    条件、循环及其他语句
    当索引行不通时
    我的排班日期
    Linux使用storcli工具查看服务器硬盘和raid组信息
    storcli64和smartctl定位硬盘的故障信息
    Shell-四剑客
    iostat
    /VAR/LOG/各个日志文件分析
  • 原文地址:https://www.cnblogs.com/lxwy1992/p/4739305.html
Copyright © 2020-2023  润新知