• listview界面显示



        1.布局写listview 

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

        4.封装一个Adapter类继承BaseAdatper,写一个构造方法接受list集合数据,复写四个方法

        getcount: 有多少条新闻数据,就有多少个条目。
            getView:将返回一个复杂的布局作为条目的内容展示;并且显示的数据是新闻的信息。


            a.创建一个构造方法 
            b.封装getCount方法    
            c.getView方法:  

      public View getView(int position, View convertView, ViewGroup parent) {
            View view = null;
            //1.复用converView优化listview,模板代码,,创建一个view作为getview的返回值用来显示一个条目,如果convertview为空,需要将一个布局文件转换为view对象作为getview的返回对象。view = View.inflater(Context context, int resuorceId,ViewGroup root)
                    
            if(convertView != null){
                view = convertView;
            }else {
                //context:上下文, resource:要转换成view对象的layout的id, root:将layout用root(ViewGroup)包一层作为getview的返回值,一般传null
                view = View.inflate(context, R.layout.item_news_layout, null);//将一个布局文件转换成一个view对象
            }
            //2.获取view上的子控件对象,目的是将list集合中的bean数据一一对应设置给这些子控件
            ImageView item_img_icon = (ImageView) view.findViewById(R.id.item_img_icon);
            TextView item_tv_des = (TextView) view.findViewById(R.id.item_tv_des);
            TextView item_tv_title = (TextView) view.findViewById(R.id.item_tv_title);
            //3.获取postion位置条目对应的list集合中的新闻数据,Bean对象
            NewsBean newsBean = list.get(position);
            //4.将获取的bean中的数据设置给这些子控件
            item_img_icon.setImageDrawable(newsBean.icon);//设置imageView的图片
            item_tv_title.setText(newsBean.title);
            item_tv_des.setText(newsBean.des);
            
            return view;
        }

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

        5.创建一个封装的Adapter对象,设置给listview   
        6.设置listview条目的点击事件  
              //设置listview条目的点击事件
            lv_news.setOnItemClickListener(this);
        
        7.复写OnItemClicklistener方法,获取相应条目上的bean对象,最终获取到url,做Intent跳转; 

     //listview的条目点击时会调用该方法 parent:代表listviw  view:点击的条目上的那个view对象   position:条目的位置  id: 条目的id
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            
            //需要获取条目上bean对象中url做跳转
            NewsBean bean = (NewsBean) parent.getItemAtPosition(position);
            
            String url = bean.news_url;
            
            //跳转浏览器
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            startActivity(intent);
        }

  • 相关阅读:
    MySQL索引
    《深度探索C++对象模型》笔记——Data语意学
    《深度探索C++对象模型》笔记——Function语意学
    近期的bug常见[从以前的零散笔记中整理]
    一个小trick
    3月9日-日记
    第一次考试_心得
    第一次考试_笔记
    哈希笔记
    Dp刷版笔记
  • 原文地址:https://www.cnblogs.com/xufengyuan/p/5767699.html
Copyright © 2020-2023  润新知