• ListView


    ListView使用需要:data、控件、adapter

    1 Protected void onCreate(Bundle savedInstanceState){
    2     super.onCreate(savedInstanceState){
    3     setContentView(R.layout.activity_main){
    4     ArrayAdapter<String> adapter = new ArrayAdapter<String>(
    5         MainActivity.this,android.R.layout.simple_list_item_1,data);
    6     ListView listView = (ListView)findViewById(R.id.list_view);
    7     listView.setadapter(adapter);
    8     }
    9 }

    自定义adapter:

      继承adapter并重写getView()

     1 public class FruitAdapter extends Adapter{
     2     private int resourceId;
     3     public FruitAdapter(Context context,in textViewResourceId,List<Fruit> objects){  //textViewResourceId 子项布局id
     5     super(context,textViewResourceId,objects);
     6     resourceId = textViewResourceId;
     7     }
     8     @Override
     9     public View getView(int position,View convertView,  //重写getView()
    10         ViewGroup parent){
    11     Fruit fruit = getItem(position);  //获得fruit实例
    12     View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
    14     imageView fruitImage = (ImageVIew)view.findViewById(R.id.
    15         fruit_image);
    16     fruitName.setText(fruit.getName());
    17     return view;
    18     }
    19 }

    重用convertView,提升ListView效率

      在getView方法中添加代码:(如果convertView为空则动态加载布局,否则重用convertView)

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

    利用ViewHolder内部类对控件实例进行缓存,避免重复调用findViewById()

    ......
        public View getView(int postition,Veiw 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
            }
            viewHolder.fruitImage.setImageResource(fruit.getImageId());
            viewHolder.fruitName.setText(fruit.getName());
            return view;
    }
    
    class ViewHolder{
        ImageVIew fruitImage;
        TextView fruitName;
    }
  • 相关阅读:
    Mac修改默认的电子邮箱客户端
    记录下生活:ETC卡充值(上海)
    Mac下卸载Mysql数据库
    网络爬虫学习笔记 1
    Clock置换算法
    用random.randint函数时 报错 'str' object cannot be interpreted as an integer问题
    分页式存储管理
    初学C++ vector 容器
    马一个讲devc++调试讲的很好的视频
    一些笔记(xss 跨站脚本攻击)
  • 原文地址:https://www.cnblogs.com/yl-saber/p/6395847.html
Copyright © 2020-2023  润新知