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

android listview实现新闻列表展示效果,第1张

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

本文实例为大家分享了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;}
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;  }}
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:

<?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>

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

总结

以上是内存溢出为你收集整理的android listview实现新闻列表展示效果全部内容,希望文章能够帮你解决android listview实现新闻列表展示效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1146734.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存