1. 程式人生 > >android 消息機制與仿新聞客戶端

android 消息機制與仿新聞客戶端

android 消息機制 仿新聞客戶端

效果圖如下:

技術分享

具體步驟如下:

1 布局文件中控件的設計

2 訪問遠程服務器的資源xml文件,該文件包含新聞的內容等信息

3 訪問到內容後把訪問內容顯示到頁面上


具體代碼如下:

1 MainActivity

package com.yuanlp.newsclient;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.yuanlp.newsclient.bean.NewsBean;
import com.yuanlp.newsclient.utils.XMLToBean;
import com.yuanlp.newsclient.view.SmartImageView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final int LOAD_ERROR =2 ;
    private static final int LOAD_SUCCESS =1 ;
    private ListView mLv_news;
    private LinearLayout mLoading;
    //new 一個Handler來做android的消息機制,處理子線程數據
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            //不管加載成功失敗,進度條應該隱藏
            mLoading.setVisibility(View.INVISIBLE);
            switch (msg.what){
                case LOAD_ERROR:
                    Toast.makeText(MainActivity.this, "加載失敗", Toast.LENGTH_SHORT).show();
                    break;
                case LOAD_SUCCESS:
                    //顯示listView的內容
                    mLv_news.setAdapter(new MyNewsAdapter());
                    break;
            }
        }
    };
    private List<NewsBean> mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /**
         * 初始化獲取界面元素
         */
        initView();

        /**
         * 加載遠程資源到本地文件
         */
        loadMessage();

        //readMessage();
    }




    /**
     * 初始化界面
     */
    private void initView() {
        setContentView(R.layout.activity_main);
        mLv_news = (ListView) findViewById(R.id.lv_news);
        mLoading = (LinearLayout) findViewById(R.id.ll_loading);
        //打開客戶端後,設置ll_loading可見
        mLoading.setVisibility(View.VISIBLE);
    }

    /**
     * 加載遠程資源到本地
     */
    private void loadMessage() {
        NewsBean newsBean=null;

        //訪問網絡不能再主線程中進行,需要一個新線程
        new Thread(){
            @Override
            public void run() {

                try {
                    Thread.sleep(5000);
                    NewsBean newsBean=null;
                    URL url = new URL("http://192.168.1.107:8080/WebServer/news.xml");
                    HttpURLConnection conn= (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    int code = conn.getResponseCode();
                    if (code==200){
                        //獲取到資源
                        InputStream is = conn.getInputStream();
                        //調用utils方法,將xml文件轉為list類型返回
                        mList = XMLToBean.readStream(newsBean,is);

                        //利用android的循環消息機制,放到UI線程中執行結果
                        Message msg = Message.obtain();
                        msg.what=LOAD_SUCCESS;
                        handler.sendMessage(msg);

                    }else{
                        Message msg = Message.obtain();
                        msg.what=LOAD_ERROR;
                        handler.sendMessage(msg);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    Message msg = Message.obtain();
                    msg.what=LOAD_ERROR;
                    handler.sendMessage(msg);
                }
            }
        }.start();
    }


    /**
     * 顯示listView中的數據
     */
    private class MyNewsAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //布局打氣筒,把xml資源文件轉變為view     即加載另外的一個布局文件
            View view = View.inflate(MainActivity.this, R.layout.news, null);
            //獲取子布局文件中的各個控件
            SmartImageView img = (SmartImageView) view.findViewById(R.id.iv_img);
            TextView tv_title= (TextView) view.findViewById(R.id.tv_title);
            TextView tv_desc= (TextView) view.findViewById(R.id.tv_desc);
            TextView tv_type= (TextView) view.findViewById(R.id.tv_type);


            //獲取當前位置的對象
            NewsBean item = getItem(position);

            //設置圖片顯示
            img.showImgByPath(item.getImg());

            tv_title.setText(item.getTitle());
            tv_desc.setText(item.getDescription());

            String type=item.getType();
            if ("1".equals(type)){ //當類型為1時,表示有評論,而且顯示評論數
                tv_type.setText("評論:"+item.getComment());
                tv_type.setTextColor(Color.BLACK);
                tv_type.setBackgroundColor(Color.TRANSPARENT);
            }else if ("2".equals(type)){
                tv_type.setText("專題");
                tv_type.setBackgroundColor(Color.RED);
                tv_type.setTextColor(Color.WHITE);
            }else if ("3".equals(type)){
                tv_type.setText("直播");
                tv_type.setBackgroundColor(Color.RED);
                tv_type.setTextColor(Color.WHITE);
            }

            return view;
        }

        @Override
        public NewsBean getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }


    }
}

2 utils 中的將xml轉為list,可以參考前面幾天寫的博客,android解析xml。

package com.yuanlp.newsclient.utils;

import android.util.Xml;

import com.yuanlp.newsclient.bean.NewsBean;

import org.xmlpull.v1.XmlPullParser;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 原立鵬 on 2017/6/21.
 */

public class XMLToBean {

    public static List<NewsBean> readStream(NewsBean newsBean, InputStream is){
        List<NewsBean> list=null;
        try {


            XmlPullParser xmlPullParser = Xml.newPullParser();
            xmlPullParser.setInput(is,"utf-8");


            int event=xmlPullParser.getEventType();
            while(event!=XmlPullParser.END_DOCUMENT){
                switch (event){  //根據eventType來區分,分為START_DOCUMENT,START_TAG,END_TAG,END_DOCUMENT
                    case XmlPullParser.START_DOCUMENT:
                        list=new ArrayList<NewsBean>();
                        break;
                    case XmlPullParser.START_TAG:
                        String tagName=xmlPullParser.getName();  //獲取標簽名稱
                        if (tagName.equalsIgnoreCase("item")){
                            newsBean=new NewsBean();
                        }else if (tagName.equalsIgnoreCase("title")){
                            newsBean.setTitle(xmlPullParser.nextText());
                        }else if (tagName.equalsIgnoreCase("description")){
                            newsBean.setDescription(xmlPullParser.nextText());
                        }else if (tagName.equalsIgnoreCase("image")){
                            newsBean.setImg(xmlPullParser.nextText());
                        }else if (tagName.equalsIgnoreCase("type")){
                            newsBean.setType(xmlPullParser.nextText());
                        }else if (tagName.equalsIgnoreCase("comment")){
                            newsBean.setComment(xmlPullParser.nextText());
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if (xmlPullParser.getName().equalsIgnoreCase("item")&&newsBean!=null){
                            list.add(newsBean);
                            newsBean=null;

                        }

                }
                event=xmlPullParser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }
}

3 獲取遠程新聞的方法

package com.yuanlp.newsclient.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.ImageView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by 原立鵬 on 2017/6/21.
 */

/**
 * 根據url地址去獲取遠程圖片,並顯示到新聞標題的左側
 */
public class SmartImageView extends ImageView {
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            Bitmap bitmap= (Bitmap) msg.obj;
            setImageBitmap(bitmap);
        }
    };

    public SmartImageView(Context context) {
        super(context);
    }

    public SmartImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public SmartImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public  void showImgByPath(final String imgURL){
        new Thread(){
            @Override
            public void run() {
                try {
                    URL url = new URL(imgURL);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    int code = conn.getResponseCode();
                    if (code==200){
                        InputStream is = conn.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(is);
                        Message msg = Message.obtain();
                        msg.what=1;
                        msg.obj=bitmap;
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }.start();
    }
}

4 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/ll_loading"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:visibility="invisible">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView"
            tools:text="正在拼命加載"/>

        <ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    <ListView
        android:id="@+id/lv_news"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

5 news.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <com.yuanlp.newsclient.view.SmartImageView
        android:id="@+id/iv_img"
        android:layout_width="72dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="3dp"

        app:srcCompat="@mipmap/ic_launcher"/>

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_marginLeft="3dp"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@+id/iv_img"
        android:text="新聞標題"/>

    <TextView
        android:id="@+id/tv_desc"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_title"
        android:layout_toRightOf="@+id/iv_img"
        android:ellipsize="end"
        android:lines="2"
        android:text="TextView"
        android:textColor="#99000000"
        android:textSize="12sp"
        tools:text="新聞描述新聞描述新聞描述新聞描述新聞描述新聞描述新聞描述新聞描述新聞描述新聞描述新聞描述新聞描述新聞描述新聞描述"
        />

    <TextView
        android:id="@+id/tv_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="新聞類型"
        android:layout_below="@+id/tv_desc"
        android:layout_alignParentRight="true"
        android:textSize="10sp"
        android:textColor="#99000000"/>

</RelativeLayout>


本文出自 “YuanGuShi” 博客,請務必保留此出處http://cm0425.blog.51cto.com/10819451/1940740

android 消息機制與仿新聞客戶端