android消息机制与仿新闻客户端

具体步骤如下:

创新互联专注于企业成都全网营销、网站重做改版、平和网站定制设计、自适应品牌网站建设、H5高端网站建设商城开发、集团公司官网建设、外贸营销网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为平和等各大城市提供网站开发制作服务。

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 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 readStream(NewsBean newsBean, InputStream is){
        List 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();
                        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("p_w_picpath")){
                            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




    

        

        
    

    

5 news.xml




    

    

    

    


文章题目:android消息机制与仿新闻客户端
链接地址:http://myzitong.com/article/iisegc.html