安卓ListView使用SimpleAdapter快速实现简易新闻客户端(详解)

上一篇主要介绍了RecycleView的用法,其中展示的界面中也包含了listView的使用,因为我还是用listView比较多,就在这里回顾一下,另外做了一个简单的Demo,详细介绍一下如何通过SimpleAdapter适配listView快速实现一个简易版的新闻客户端。

一、效果图

在这里插入图片描述

这里做的是一个模拟,点击某一条新闻,跳转至不同的新闻详情界面,主要是展示实现的效果,数据并没有全部放上去,如果后续有需要我会将完整的代码示例上传。

二、实现思路与代码

2.1指定布局news_item.xml

首先需要给每一个子项指定一个布局,代码如下。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp">
    <ImageView
        android:id="@+id/iv_hotspot_picture"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="5dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="5dp"
        android:scaleType="centerCrop" />
    <TextView
        android:id="@+id/tv_hotspot_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
       android:layout_alignParentLeft="true"
        android:maxLength="16"
        android:singleLine="false"
        android:maxLines="2"
        android:text="我是标题"
        android:textColor="#000000"
        android:textSize="16sp" />
    <TextView
        android:id="@+id/tv_hotspot_hint"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:text="200阅读   1天前"
        android:textColor="#99000000"
        android:textSize="12sp" />
</RelativeLayout>

其中ImageView表示新闻的主要图片,第一个TextView是新闻标题,第二个TextView是阅读量和时间。

2.2 在MainActivity中调用

2.2.1 布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <ListView
        android:id="@+id/lv_news"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>
</LinearLayout>

2.2.2 定义全局变量

    private String[] titles;
    private String[] hints;
    private Integer[] images;

这里定义了三个数组,titles用来存放新闻标题,hints存放阅读量和时间,images存放图片对应的资源,后续方法中需要用到。

2.2.3 初始化数据

  
    /**
     * 初始化数据
     */
    private void initNews() {
    
    
        hints = new String[]{
    
    "100阅读量  1天前", "150阅读量  1天前", "200阅读量  2天前", "221阅读量  2天前", "221阅读量  2天前",
                "333阅读量  4天前", "321阅读量  5天前", "458阅读量  6天前", "1100阅读量  7天前"};
        titles = new String[]{
    
    getString (R.string.demo_news_title1), getString (R.string.demo_news_title2), getString (R.string.demo_news_title3)
                , getString (R.string.demo_news_title4), getString (R.string.demo_news_title5), getString (R.string.demo_news_title6),
        getString (R.string.demo_news_title7),getString (R.string.demo_news_title8),getString (R.string.demo_news_title9)};
        images=new Integer[]{
    
    R.mipmap.demo_news_image1,R.mipmap.demo_news_image2,R.drawable.ic_launcher_background,R.drawable.ic_launcher_background,
                R.drawable.ic_launcher_background,R.drawable.ic_launcher_background,R.drawable.ic_launcher_background,0,0};
    }

这里定义了一个方法用来初始化数据,需要在onCreate()方法中调用

2.2.4 listView适配

这一步将数据设置到了我们指定的布局中,并给每个子项添加了一个点击事件,点击某一条新闻后,将会跳转至对应的新闻详情界面。

  /**
     *新闻ListView适配
     */
    private void setAdapter(){
    
    
        List<Map<String, Object>> listItems = new ArrayList<> ();
        for (int i = 0; i < titles.length; i++) {
    
    
            Map<String, Object> listItem = new HashMap<> ();
            listItem.put ("title", titles[i]);
            listItem.put ("content", hints[i]);
            listItem.put ("image",images[i]);
            listItems.add (listItem);

        }
        SimpleAdapter simpleAdapter = new SimpleAdapter (this, listItems, R.layout.news_item, new
                String[]{
    
    "title", "content","image"}, new int[]{
    
    R.id.tv_news_title, R.id.tv_news_hint,R.id.iv_news_picture});
        ListView lv_news = findViewById (R.id.lv_news);
        lv_news.setAdapter (simpleAdapter);
        //setListViewHeight (lv_news);
        lv_news.setOnItemClickListener (new AdapterView.OnItemClickListener () {
    
    
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    
    
                Intent intent= new Intent (view.getContext (), DetailPageActivity.class);
                intent.putExtra ("ID", i);
                startActivity (intent);

            }
        });

    }

这里也是写在方法中,需要在onCreate()方法中初始化数据后调用
代码中首先定义了一个Map的集合(map中的元素由键值对组成),然后通过for循环将每一个子项的标题、阅读量和时间、图片的键值分别添加到集合中。然后通过SimpleAdapter将集合中的每个键值数据映射到对应的布局文件上,再调用ListView的setAdapter()方法。
因为我们每个新闻的详情页面不一样,所以这里需要对子项的点击事件进行监听,通过positsion来判断点击的子项,这里将得到的数据放入了intent中。

2.3 新闻详情界面

在新闻详情界面获得了主活动中传入的position值,通过position值来设置详情界面的新闻内容。同样需要在onCreate方法中调用。

    /**
     *获得新闻内容数据
     */
    private void getNews() {
    
    
        Intent intent = getIntent ();
        int id = intent.getIntExtra ("ID", 0)+1;
        tv_detailNews.setText ("我是新闻: "+id);
    }

三、结语

最近终于把之前做过的软件有关部分总结得差不多了,后续将进行一些知识点的总结。这篇博客如果有什么问题,欢迎大家评论指正。

猜你喜欢

转载自blog.csdn.net/qq_44706002/article/details/112741601