成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

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

具體步驟如下:

創(chuàng)新互聯(lián)專注于企業(yè)成都全網(wǎng)營銷、網(wǎng)站重做改版、平和網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5高端網(wǎng)站建設(shè)、商城開發(fā)、集團公司官網(wǎng)建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為平和等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

1 布局文件中控件的設(shè)計

2 訪問遠(yuǎn)程服務(wù)器的資源xml文件,該文件包含新聞的內(nèi)容等信息

3 訪問到內(nèi)容后把訪問內(nèi)容顯示到頁面上

具體代碼如下:

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的消息機制,處理子線程數(shù)據(jù)
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            //不管加載成功失敗,進度條應(yīng)該隱藏
            mLoading.setVisibility(View.INVISIBLE);
            switch (msg.what){
                case LOAD_ERROR:
                    Toast.makeText(MainActivity.this, "加載失敗", Toast.LENGTH_SHORT).show();
                    break;
                case LOAD_SUCCESS:
                    //顯示listView的內(nèi)容
                    mLv_news.setAdapter(new MyNewsAdapter());
                    break;
            }
        }
    };
    private List<NewsBean> mList;

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

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

        /**
         * 加載遠(yuǎn)程資源到本地文件
         */
        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);
        //打開客戶端后,設(shè)置ll_loading可見
        mLoading.setVisibility(View.VISIBLE);
    }

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

        //訪問網(wǎng)絡(luò)不能再主線程中進行,需要一個新線程
        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();
                        //調(diào)用utils方法,將xml文件轉(zhuǎn)為list類型返回
                        mList = XMLToBean.readStream(newsBean,is);

                        //利用android的循環(huán)消息機制,放到UI線程中執(zhí)行結(jié)果
                        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中的數(shù)據(jù)
     */
    private class MyNewsAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //布局打氣筒,把xml資源文件轉(zhuǎn)變?yōu)関iew     即加載另外的一個布局文件
            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);


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

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

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

            String type=item.getType();
            if ("1".equals(type)){ //當(dāng)類型為1時,表示有評論,而且顯示評論數(shù)
                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轉(zhuǎn)為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){  //根據(jù)eventType來區(qū)分,分為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();  //獲取標(biāo)簽名稱
                        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 獲取遠(yuǎn)程新聞的方法

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.
 */

/**
 * 根據(jù)url地址去獲取遠(yuǎn)程圖片,并顯示到新聞標(biāo)題的左側(cè)
 */
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="新聞標(biāo)題"/>

    <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>

文章名稱:android消息機制與仿新聞客戶端
文章網(wǎng)址:http://jinyejixie.com/article12/iisegc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、網(wǎng)站改版外貿(mào)建站、網(wǎng)站設(shè)計公司、網(wǎng)站制作、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計
锡林浩特市| 佛冈县| 富宁县| 双城市| 湖南省| 新干县| 南京市| 延长县| 贵定县| 林州市| 大安市| 双辽市| 海兴县| 台南县| 光山县| 广宗县| 洛宁县| 定州市| 林州市| 平昌县| 库尔勒市| 卢氏县| 渭源县| 泰兴市| 保康县| 五家渠市| 丰县| 广东省| 化隆| 武夷山市| 利川市| 忻城县| 揭阳市| 英山县| 汉寿县| 芦溪县| 衢州市| 天峻县| 富川| 麻阳| 定边县|