首页 技术 正文
技术 2022年11月16日
0 收藏 832 点赞 4,238 浏览 7778 个字

xml设计

<?xml version="1.0"?>-<RelativeLayout tools:context=".MainActivity" android:paddingTop="@dimen/activity_vertical_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"><ListView android:id="@+id/lv_news" android:layout_height="fill_parent" android:layout_width="fill_parent"/></RelativeLayout>
<?xml version="1.0"?>-<RelativeLayout tools:context=".MainActivity" android:paddingTop="@dimen/activity_vertical_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"><ListView android:id="@+id/lv_news" android:layout_height="fill_parent" android:layout_width="fill_parent"/></RelativeLayout>

ListView返回的xml设计

java

Activity

package com.itheima.news_listview.utils;import java.util.ArrayList;import android.content.Context;import com.itheima.news_listview.R;
import com.itheima.news_listview.bean.NewsBean;public class NewsUtils { //封装新闻的假数据到list中返回
public static ArrayList<NewsBean> getAllNews(Context context) { ArrayList<NewsBean> arrayList = new ArrayList<NewsBean>(); for(int i = 0 ;i <100;i++)
{
NewsBean newsBean = new NewsBean();
newsBean.title ="谢霆锋经纪人:偷拍系侵权行为:";
newsBean.des= "称谢霆锋隐私权收到侵犯,将保留追究法律责任";
newsBean.news_url= "http://www.sina.cn";
newsBean.icon = context.getResources().getDrawable(R.drawable.ic_launcher);//通过context对象将一个资源id转换成一个Drawable对象。
arrayList.add(newsBean); NewsBean newsBean1 = new NewsBean();
newsBean1.title ="知情人:王菲是谢霆锋心头最爱的人";
newsBean1.des= "身边的人都知道谢霆锋最爱王菲,二人早有复合迹象";
newsBean1.news_url= "http://www.baidu.cn";
newsBean1.icon = context.getResources().getDrawable(R.drawable.icon);//通过context对象将一个资源id转换成一个Drawable对象。
arrayList.add(newsBean1); NewsBean newsBean2 = new NewsBean();
newsBean2.title ="热烈祝贺黑马74高薪就业";
newsBean2.des= "74期平均薪资20000,其中有一个哥们超过10万,这些It精英都迎娶了白富美.";
newsBean2.news_url= "http://www.itheima.com";
newsBean2.icon = context.getResources().getDrawable(R.drawable.icon2);//通过context对象将一个资源id转换成一个Drawable对象。
arrayList.add(newsBean2);
}
return arrayList;
}}

utls

package com.itheima.news_listview.bean;import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;public class NewsBean { public String title;
public String des;
public Drawable icon;
public String news_url;}

bean

package com.itheima.news_listview.adapter;import java.util.ArrayList;import com.itheima.news_listview.R;
import com.itheima.news_listview.bean.NewsBean;import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;public class NewsAdapter extends BaseAdapter { private ArrayList<NewsBean> list;
private Context context; //通过构造方法接受要显示的新闻数据集合
public NewsAdapter(Context context,ArrayList<NewsBean> list){
this.list = list;
this.context = context;
} @Override
public int getCount() {
return list.size();
} @Override
public Object getItem(int position) {
return list.get(position);
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
//1.复用converView优化listview,创建一个view作为getview的返回值用来显示一个条目
if(convertView != null){
view = convertView;
}else {
//context:上下文, resource:要转换成view对象的layout的id, root:将layout用root(ViewGroup)包一层作为codify的返回值,一般传null
// view = View.inflate(context, R.layout.item_news_layout, null);//将一个布局文件转换成一个view对象 //通过LayoutInflater将布局转换成view对象
// view = LayoutInflater.from(context).inflate(R.layout.item_news_layout, null); //通过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_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.将数据设置给这些子控件做显示
item_img_icon.setImageDrawable(newsBean.icon);//设置imageView的图片
item_tv_title.setText(newsBean.title);
item_tv_des.setText(newsBean.des); return view;
}}

Adapter

老师笔记

复杂listview界面显示 ,黑马新闻(***********重要***********)

1.布局写listview

2.找到listview

3.获取新闻数据封装到list集合中(才用模拟数据),作为adapter的显示数据,怎么将获取的新闻数据给adapter???

4.创建一个adapter继承BaseAdapter,实现4个方法
        getcount: 有多少条新闻数据,就有多少个条目。
        getView:将返回一个复杂的布局作为条目的内容展示;并且显示的数据是新闻的信息。 ?????
        
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        //1.复用converView优化listview,创建一个view作为getview的返回值用来显示一个条目
        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上的子控件对象
        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.将数据设置给这些子控件做显示
        item_img_icon.setImageDrawable(newsBean.icon);//设置imageView的图片
        item_tv_title.setText(newsBean.title);
        item_tv_des.setText(newsBean.des);
        
        return view;
    }
        
    5.创建一个adapter对象设置给listview

6.设置listview的条目的点击事件,并封装点击事件,去查看新闻详情。 ?????????
        //设置listview条目的点击事件
        lv_news.setOnItemClickListener(this);
    
            //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);
    }

1.布局写listview ok

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

4.封装一个Adapter类继承BaseAdatper,写一个构造方法接受list集合数据,复写四个方法
        a.创建一个构造方法  ok
        b.封装getCount方法   ok
        c.getView方法:   不ok
            1.复用convertview,模板代码,如果不都能空,需要将一个布局文件转换为view对象作为getview的返回对象。
                view = View.inflater(Context context, int resuorceId,ViewGroup root)
            2.找到view上的这些子控件,目的是将list集合中的bean数据一一对应设置给这些子控件

3.从list集合中获取postion条目上要显示的数据Bean
            
            4.将获取的bean中的数据设置给这些子控件
        d.getItem方法:将list集合中指定postion上的bean对象返回
        e.getItemId,直接返回postion

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

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

#10 常用获取inflate的写法

1.
            //context:上下文, resource:要转换成view对象的layout的id, root:将layout用root(ViewGroup)包一层作为codify的返回值,一般传null
                //view = View.inflate(context, R.layout.item_news_layout, null);//将一个布局文件转换成一个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);

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,946
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,472
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,285
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,101
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,733
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,768