欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-11-27 08:56:46
本文实例为大家分享了android listview列表展示效果的具体代码,供大家参考,具体内容如下 1.封装一些新闻数据 2.使用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;
}

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>

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