首页 > 代码库 > 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 消息机制与仿新闻客户端