• Android 之 ListView的学习


    ListView 是一个控件,一个在垂直滚动的列表中显示条目的一个控件,这些条目的内容来自于一个ListAdapter 。EditText Button TextView ImageView Checkbox 五大布局。

    ListView的使用有点类似mvc的模式:

      model:bean

      view :listview

      control:adapter

    LsitView的使用步骤:


    1.布局写listview 

    2.找到listview 

    3.封装新闻数据到list集合中 ,目的是为adapter提供数据展示。 

    4.封装一个Adapter类继承BaseAdatper,写一个构造方法接受list集合数据,复写四个方法
      a.创建一个构造方法  
      b.封装getCount方法 
      c.getView方法: 
        c.1:复用convertview
          view = View.inflater(Context context, int resuorceId,ViewGroup root)
        c.2:找到view上的这些子控件,目的是将list集合中的bean数据一一对应设置给这些子控件

        c.3:找到内容:从list集合中获取postion条目上要显示的数据Bean

          NewsBean newsBean=list.get(position);
        c.4:设置内容:将获取的bean中的数据设置给这些子控件

     public View getView(int i, View convertview, ViewGroup viewGroup) {
            View view=null;
            //1 复用convertView优化listview,创建一个view作为getview的返回值的来显示一个条目
            if(convertview!=null){
                view=convertview;
            }else{
                view=View.inflate(context, R.layout.item_news_layout,null);//将一个布局文件转为view对象
    
                //获取View对象方式2:通过LayoutInflater将布局转换成view对象
                //view =  LayoutInflater.from(context).inflate(R.layout.item_news_layout, null);
    
                //3:通过context获取系统服务得到一个LayoutInflater,通过LayoutInflater将一个布局转换为view对象
                //LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                //view = layoutInflater.inflate(R.layout.item_news_layout, null);
            }
    
            //2获取View上的 子控件对象
            ImageView item_img_icon = (ImageView)view.findViewById(R.id.item_img_icon);
            TextView item_desc = (TextView) view.findViewById(R.id.item_desc);
            TextView item_tv_title = (TextView) view.findViewById(R.id.item_tv_title);
    
            //3获取position位置上的条目对应的list集合中的数据,Bean对象
            NewsBean newsBean = list.get(i);
    
            //4将数据设置给子控件显示
            item_img_icon.setImageDrawable(newsBean.icon);
            item_tv_title.setText(newsBean.title);
            item_desc.setText(newsBean.des);
    
            return  view;
        }
    

      


      d.getItem方法:将list集合中指定postion上的bean对象返回
      e.getItemId,直接返回postion

    5.创建一个封装的Adapter对象,设置给listview 
    6.设置listview条目的点击事件 
      listview.setOnItem....

    7.复写OnItemClicklistener方法,获取相应条目上的bean对象,最终获取到url,做Intent跳转;

  • 相关阅读:
    堆排序(Heap Sort)
    快速排序(Quick Sort)
    希尔排序(Shell Sort)
    C和C++中的可变参数及宏的使用
    函数中的参数问题小结(&,*,传参与变参)
    C语言基础之struct
    C语言基础之指针
    从名字开始讲——C与C++的编译细节
    二维数组的动态初始化与复制
    《Java程序设计》第二次学习总结
  • 原文地址:https://www.cnblogs.com/DonAndy/p/6160798.html
Copyright © 2020-2023  润新知