android listview实现新闻列表展示效果

这篇文章主要为大家详细介绍了android listview实现新闻列表展示效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了android listview列表展示效果的具体代码,供大家参考,具体内容如下

1.封装一些新闻数据 
2.使用listview展示出来 
3.设置条目点击事件,点击后跳转浏览器查看新闻

package com.itheima74.newscustom.domain;
 
import android.graphics.drawable.Drawable;
 
/**
  * Created by My on 2016/11/8.
  */
 
public class NewsBean {
   public String title;
   public String des;
   public Drawable icon;
   public String news_url;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.itheima74.newscustom.utils;
 
import android.content.Context;
 
import com.itheima74.newscustom.R;
import com.itheima74.newscustom.domain.NewsBean;
 
import java.util.ArrayList;
 
/**
  * Created by My on 2016/11/8.
  */
 
public class NewsUtils {
   /**
    * @param context 上下文环境
    * @return 新闻集合
    */
   public static ArrayList<NewsBean> getAllNews(Context context) {
     ArrayList<NewsBean> arrayList = new ArrayList<>();
     for ( int i = 0 ; i < 5 ; i++) {
       NewsBean newsBean1 = new NewsBean();
       newsBean1.title = "鸟瞰暴雨后的武汉 全市已转移16万人次" ;
       newsBean1.des = "7月5-6日,武汉普降暴雨-大暴雨,中心城区、蔡甸部分地区出现特大暴雨。江夏大道汤逊湖大桥段,被湖水冲破的路障。记者贾代腾飞 陈卓摄" ;
       newsBean1.icon = context.getResources().getDrawable(R.drawable.wuhan);
       newsBean1.news_url = "http://slide.news.sina.com.cn/s/slide_1_2841_101020.html#p=1" ;
       arrayList.add(newsBean1);
 
       NewsBean newsBean2 = new NewsBean();
       newsBean2.title = "安徽暴雨 三四十条鳄鱼逃至附近农田" ;
       newsBean2.des = "因强降雨造成内涝,安徽省芜湖市芜湖县花桥镇鳄鱼湖农庄所养鳄鱼逃跑至附近农田。。据悉,溜出来的鳄鱼为散养的扬子鳄,比较温驯。初步预计有三四十条,具体数量未统计,其中最大的约1.8米长。图为网友拍摄到的农田中的鳄鱼。" ;
       newsBean2.icon = context.getResources().getDrawable(R.drawable.eyu);
       newsBean2.news_url = "http://slide.news.sina.com.cn/s/slide_1_2841_101024.html#p=1" ;
       arrayList.add(newsBean2);
 
       NewsBean newsBean3 = new NewsBean();
       newsBean3.title = "暴雨过后 南京理工大学变“奇幻森林”" ;
       newsBean3.des = "近日,持续强降雨,导致地势低洼的南京理工大学出现严重积水。这一组几张照片,南理工恍若童话世界中。网友:泡在水中的南理工,也可以倔强地刷出颜值新高度。" ;
       newsBean3.icon = context.getResources().getDrawable(R.drawable.qihuan);
       newsBean3.news_url = "http://slide.news.sina.com.cn/s/slide_1_2841_101010.html#p=1" ;
       arrayList.add(newsBean3);
     }
     return arrayList;
   }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.itheima74.newscustom.activity;
 
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
 
import com.itheima74.newscustom.R;
import com.itheima74.newscustom.domain.NewsBean;
import com.itheima74.newscustom.utils.NewsUtils;
 
import java.util.ArrayList;
 
public class MainActivity extends AppCompatActivity {
 
   private ListView lv;
   private ArrayList<NewsBean> mList;
 
   @Override
   protected void onCreate(Bundle savedInstanceState) {
     super .onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
 
     initUI();
     initData();
     initAdapter();
   }
 
   private void initAdapter() {
     lv.setAdapter( new NewsAdapter());
   }
 
   private void initData() {
     mList = NewsUtils.getAllNews( this );
   }
 
   private void initUI() {
     lv = (ListView) findViewById(R.id.lv);
 
     lv.setOnItemClickListener( new AdapterView.OnItemClickListener() {
       @Override
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
         Intent intent = new Intent();
         intent.setAction(Intent.ACTION_VIEW);
         intent.setData(Uri.parse(mList.get(position).news_url));
         startActivity(intent);
       }
     });
   }
 
   private class NewsAdapter extends BaseAdapter {
 
     @Override
     public int getCount() {
       return mList.size();
     }
 
     @Override
     public NewsBean getItem( int position) {
       return mList.get(position);
     }
 
     @Override
     public long getItemId( int position) {
       return position;
     }
 
     @Override
     public View getView( int position, View convertView, ViewGroup parent) {
       ViewHolder holder;
       if (convertView == null ) {
         holder = new ViewHolder();
         convertView = View.inflate(getApplicationContext(), R.layout.listview_item, null );
         holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
         holder.tv_des = (TextView) convertView.findViewById(R.id.tv_des);
         holder.iv_icon = (ImageView) convertView.findViewById(R.id.iv_icon);
         convertView.setTag(holder);
       } else {
         holder = (ViewHolder) convertView.getTag();
       }
       NewsBean item = getItem(position);
       holder.tv_title.setText(item.title);
       holder.tv_des.setText(item.des);
       holder.iv_icon.setImageDrawable(item.icon);
       return convertView;
     }
   }
 
   private static class ViewHolder {
     TextView tv_title;
     TextView tv_des;
     ImageView iv_icon;
 
   }
}

listview_item.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   android:layout_width = "match_parent"
   android:layout_height = "wrap_content"
   android:gravity = "center"
   android:orientation = "horizontal"
   android:padding = "10dp" >
 
   < ImageView
     android:id = "@+id/iv_icon"
     android:layout_width = "100dp"
     android:layout_height = "80dp"
     android:layout_marginEnd = "10dp"
     android:layout_marginRight = "10dp"
     android:src = "@mipmap/ic_launcher" />
 
   < LinearLayout
     android:layout_width = "wrap_content"
     android:layout_height = "wrap_content"
     android:orientation = "vertical" >
 
     < TextView
       android:id = "@+id/tv_title"
       android:layout_width = "wrap_content"
       android:layout_height = "wrap_content"
       android:layout_marginBottom = "3dp"
       android:maxLines = "1"
       android:text = "新闻标题"
       android:textColor = "#000000"
       android:textSize = "16sp" />
 
     < TextView
       android:id = "@+id/tv_des"
       android:layout_width = "wrap_content"
       android:layout_height = "wrap_content"
       android:maxLines = "2"
       android:text = "新闻内容"
       android:textColor = "#666666"
       android:textSize = "13sp" />
   </ LinearLayout >
 
</ LinearLayout >

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

listview_item.xml:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值