1. 程式人生 > >新聞案例本地版(listview實現)

新聞案例本地版(listview實現)

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;

/**
 * 1.封裝一些新聞資料
 * 2.使用listview展示出來
 * 3.設定條目點選事件,點選後跳轉瀏覽器檢視新聞
 */
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 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>