zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    radio切换,点击方法onclick
    Android Studio 使用小技巧和快捷键
    Linux命令(1)——xargs命令
    Java-多线程基本
    POJ 2031 Building a Space Station(最小生成树)
    《iOS Human Interface Guidelines》——Popover
    Android在使用WebView时,通过Javascript调用JAVA函数
    Mysql文章笔记
    大量高清文字版PDF编程书籍下载
    Scala函数特性
  • 原文地址:https://www.cnblogs.com/yl-saber/p/6395847.html
Copyright © 2011-2022 走看看