android listview實(shí)現(xiàn)新聞列表展示效果
本文實(shí)例為大家分享了android listview列表展示效果的具體代碼,供大家參考,具體內(nèi)容如下

1.封裝一些新聞數(shù)據(jù)
2.使用listview展示出來(lái)
3.設(shè)置條目點(diǎn)擊事件,點(diǎn)擊后跳轉(zhuǎn)瀏覽器查看新聞
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 上下文環(huán)境
* @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 = "鳥(niǎo)瞰暴雨后的武漢 全市已轉(zhuǎn)移16萬(wàn)人次";
newsBean1.des = "7月5-6日,武漢普降暴雨-大暴雨,中心城區(qū)、蔡甸部分地區(qū)出現(xiàn)特大暴雨。江夏大道湯遜湖大橋段,被湖水沖破的路障。記者賈代騰飛 陳卓攝";
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 = "安徽暴雨 三四十條鱷魚(yú)逃至附近農(nóng)田";
newsBean2.des = "因強(qiáng)降雨造成內(nèi)澇,安徽省蕪湖市蕪湖縣花橋鎮(zhèn)鱷魚(yú)湖農(nóng)莊所養(yǎng)鱷魚(yú)逃跑至附近農(nóng)田。。據(jù)悉,溜出來(lái)的鱷魚(yú)為散養(yǎng)的揚(yáng)子鱷,比較溫馴。初步預(yù)計(jì)有三四十條,具體數(shù)量未統(tǒng)計(jì),其中最大的約1.8米長(zhǎng)。圖為網(wǎng)友拍攝到的農(nóng)田中的鱷魚(yú)。";
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 = "暴雨過(guò)后 南京理工大學(xué)變“奇幻森林”";
newsBean3.des = "近日,持續(xù)強(qiáng)降雨,導(dǎo)致地勢(shì)低洼的南京理工大學(xué)出現(xiàn)嚴(yán)重積水。這一組幾張照片,南理工恍若童話世界中。網(wǎng)友:泡在水中的南理工,也可以倔強(qiáng)地刷出顏值新高度。";
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="新聞標(biāo)題"
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="新聞內(nèi)容"
android:textColor="#666666"
android:textSize="13sp" />
</LinearLayout>
</LinearLayout>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android RecyclerView實(shí)現(xiàn)拼團(tuán)倒計(jì)時(shí)列表實(shí)例代碼
- Android如何利用RecyclerView實(shí)現(xiàn)列表倒計(jì)時(shí)效果實(shí)例代碼
- Android 列表倒計(jì)時(shí)的實(shí)現(xiàn)的示例代碼(CountDownTimer)
- android二級(jí)listview列表實(shí)現(xiàn)代碼
- Android通過(guò)LIstView顯示文件列表的兩種方法介紹
- Android ExpandableListView展開(kāi)列表控件使用實(shí)例
- Android編程使用ListView實(shí)現(xiàn)數(shù)據(jù)列表顯示的方法
- android開(kāi)發(fā)教程之使用listview顯示qq聯(lián)系人列表
- Android自定義ListView實(shí)現(xiàn)仿QQ可拖拽列表功能
- Android ListView列表實(shí)現(xiàn)倒計(jì)時(shí)
相關(guān)文章
Android實(shí)現(xiàn)仿通訊錄側(cè)邊欄滑動(dòng)SiderBar效果代碼
這篇文章主要介紹了Android實(shí)現(xiàn)仿通訊錄側(cè)邊欄滑動(dòng)SiderBar效果代碼,實(shí)例分析了通訊錄側(cè)邊欄滑動(dòng)效果的實(shí)現(xiàn)技巧,并附帶完整實(shí)例代碼供讀者下載參考,需要的朋友可以參考下2015-10-10
Android開(kāi)機(jī)自啟動(dòng)服務(wù)的實(shí)現(xiàn)方法
Android開(kāi)機(jī)自啟動(dòng)服務(wù)的實(shí)現(xiàn)方法,需要的朋友可以參考一下2013-05-05
Android Studio 配置:自定義頭部代碼注釋及添加模版方式
這篇文章主要介紹了Android Studio 配置:自定義頭部代碼注釋及添加模版方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Retrofit源碼之請(qǐng)求對(duì)象的轉(zhuǎn)換筆記
這篇文章主要介紹了Retrofit源碼之請(qǐng)求對(duì)象的轉(zhuǎn)換筆記,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
android利用ContentResolver訪問(wèn)者獲取手機(jī)聯(lián)系人信息
這篇文章主要介紹了android利用ContentResolver訪問(wèn)者獲取手機(jī)聯(lián)系人信息,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-02-02
Android開(kāi)發(fā)實(shí)現(xiàn)的圖片瀏覽功能示例【放大圖片】
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)的圖片瀏覽功能,結(jié)合實(shí)例形式分析了Android針對(duì)圖片的切換顯示、透明度、大小調(diào)整等相關(guān)操作技巧,需要的朋友可以參考下2019-04-04

