Android学习笔记_48_若水新闻客户端源码剖析

一、新闻客户端布局代码

1.1 主界面布局

    使用GridView实现左右可滑动菜单项,使用标签HorizontalScrollView实现水平滚动条,将创建的GridView添加到布局文件中。 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@id/main_layout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/main_background"
    >
    <RelativeLayout
        android:id="@id/titlebar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/titlebar_background"
        >
        <TextView
            android:id="@id/titlebar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8.0dip"
            android:layout_marginLeft="15.0dip"
            android:textAppearance="@style/titlebar_title_style"
            android:text="@string/app_name"/>
        <Button
            android:id="@id/titlebar_refresh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/titlebar_btn_refresh_selector"
            android:layout_marginTop="7.0dip"
            android:layout_marginRight="14.0dip"
            android:layout_alignParentRight="true"
            />
        <ProgressBar
            android:id="@id/loadnews_progress"
            android:layout_width="25.0dip"
            android:layout_height="25.0dip"
            android:clickable="false"
            android:visibility="gone"
            android:layout_marginRight="20.0dip"
            android:layout_marginTop="10.0dip"
            android:layout_alignParentRight="true"
            style="?android:attr/progressBarStyleLarge" />
    </RelativeLayout>
    <RelativeLayout
        android:id="@id/categorybar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/categorybar_background"
        android:layout_marginTop="-18.0dip"
        >
        <Button
            android:id="@id/category_arrow_right"
            android:layout_width="6.0dip"
            android:layout_height="10.0dip"
            android:background="@drawable/categorybar_right_arrow"
            android:layout_marginLeft="2.0dip"
            android:layout_marginRight="10.0dip"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true" />
        <HorizontalScrollView   
            android:id="@id/category_scrollview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="6.0dip"
            android:scrollbars="none"
            android:layout_toLeftOf="@id/category_arrow_right"
            android:layout_centerVertical="true">
            <LinearLayout
                android:id="@id/category_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                    />    
        </HorizontalScrollView>        
    </RelativeLayout>
    <ListView 
        android:id="@+id/newslist" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:listSelector="@drawable/newslist_item_selector"
        android:cacheColorHint="#00000000" 
        android:divider="@drawable/list_separator_line"
        />
</LinearLayout>

 

二、ViewFlipper的应用

2.1 界面布局

     进入到新闻详细界面,通过手指滑动实现下一条新闻和上一条新闻的切换。newsdetails.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@id/newsdetails_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/main_background"
    android:layout_marginBottom="0.0dip"
    >
    <RelativeLayout
        android:id="@id/newsdetails_titlebar_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/titlebar_background"
        >
        <Button
            android:id="@id/newsdetails_titlebar_previous"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="7.0dip"
            android:layout_marginLeft="5.0dip"
            android:layout_alignParentLeft="true" 
            android:background="@drawable/newsdetails_title_previous_btn_selector"
            android:textSize="14.0sp"
            android:textStyle="bold"
            />
        <TextView
            android:id="@id/newsdetails_titlebar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15.0dip"
            android:layout_marginTop="10.0dip" 
            android:layout_toRightOf="@id/newsdetails_titlebar_previous"
            android:textSize="18.0sp"
            android:textColor="@color/white"
            android:text="国内"
            />
        <Button
            android:id="@id/newsdetails_titlebar_next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="7.0dip"
            android:layout_marginRight="5.0dip"
            android:layout_alignParentRight="true" 
            android:background="@drawable/newsdetails_title_next_btn_selector"
            />
        <Button
            android:id="@id/newsdetails_titlebar_comments"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="60.0dip"
            android:layout_marginTop="9.0dip"
            android:background="@drawable/newsdetails_titlebar_comments_background"
            android:textColor="@color/white"
            android:text="0跟帖"
            />
        <ProgressBar
            android:id="@id/loadnews_progress"
            android:layout_width="25.0dip"
            android:layout_height="25.0dip"
            android:clickable="false"
            android:visibility="gone"
            android:layout_marginRight="30.0dip"
            android:layout_marginTop="10.0dip"
            android:layout_alignParentRight="true"
            style="?android:attr/progressBarStyleLarge" />
    </RelativeLayout>    
    
    <ViewFlipper
        android:id="@id/news_body_flipper"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/main_background"
        android:layout_marginTop="-12.0dip"
        android:layout_marginBottom="40.0dip"
        android:layout_below="@id/newsdetails_titlebar_layout" />

    <include
        android:id="@id/comments_reply_frame"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        layout="@layout/reply_frame" />
</RelativeLayout>

news_body.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:szy="http://schemas.android.com/apk/res/com.szy.news.activity"
    android:id="@id/news_body_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/newsdetails_titlebar_layout"
    >
    <ScrollView
        android:id="@id/news_body_scrollview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#FFE7E7E7"
        android:fadingEdge="none"
        >
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <TextView
                    android:id="@id/news_body_title"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="12.0dip"
                    android:layout_marginTop="12.0dip"
                    android:layout_marginRight="12.0dip"
                    android:textColor="#FF272727"
                    android:textSize="18.0dip"
                    android:textStyle="bold"
                    />
                <TextView
                    android:id="@id/news_body_ptime_source"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="12.0dip"
                    android:layout_marginTop="9.0dip"
                    android:layout_marginRight="12.0dip"
                    android:textColor="#FF888888"
                    android:textSize="12.0sp"
                    />
                <ImageView
                    android:id="@id/news_body_separator_line"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8.0dip"
                    android:visibility="visible"
                    android:src="@drawable/list_separator_line"
                    />
                <ProgressBar
                    android:id="@id/news_body_details_loading"
                    android:layout_width="16.0dip"
                    android:layout_height="16.0dip"
                    android:layout_marginLeft="152.0dip"
                    android:layout_marginTop="10.0dip"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:visibility="gone"
                    android:clickable="false"
                    style="?android:attr/progressBarStyleLarge" />
            </LinearLayout>
            <!-- 
            <TextView
                android:id="@id/news_body_details"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5.0dip"
                android:textColor="#ff000000" 
                />
             -->
             <com.szy.news.view.CustomTextView  
                android:id="@id/news_body_details"
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                szy:image_width="200dip"
                szy:image_height="52dip"
                />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

详细新闻的activity的实现,通过自定义控件显示新闻内容,监听新闻内容的触摸事件:

package com.szy.news.activity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.szy.news.model.Parameter;
import com.szy.news.service.SyncHttp;
import com.szy.news.view.CustomTextView;

/**
 *@author coolszy
 *@date 2012-3-19
 *@blog http://blog.92coding.com
 */
public class NewsDetailsActivity extends Activity
{
    private final int FINISH = 0;

    private ViewFlipper mNewsBodyFlipper;
    private LayoutInflater mNewsBodyInflater;
    private float mStartX;
    private ArrayList<HashMap<String, Object>> mNewsData;
    private int mPosition = 0;
    private int mCursor;
    private int mNid;
    private CustomTextView mNewsDetails;
    private Button mNewsdetailsTitlebarComm;// 新闻回复数
    private ImageButton mNewsReplyImgBtn;// 发表新闻回复图片
    private LinearLayout mNewsReplyImgLayout;// 发表新闻回复图片Layout
    private LinearLayout mNewsReplyEditLayout;// 发表新闻回复回复Layout
    private TextView mNewsReplyContent;// 新闻回复内容
    private boolean keyboardShow = false; //软件盘是否可见

    private Handler mHandler = new Handler()
    {
        @SuppressWarnings("unchecked")
        @Override
        public void handleMessage(Message msg)
        {
            switch (msg.arg1)
            {
            case FINISH:
                // 把获取到的新闻显示到界面上
                ArrayList<HashMap<String, Object>> bodyList = (ArrayList<HashMap<String,Object>>)msg.obj;
                System.out.println("###:"+bodyList.size());
                mNewsDetails.setText(bodyList);
                break;
            }
        }
    };

    @SuppressWarnings("unchecked")
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newsdetails);

        // 查找新闻回复图片Layout
        mNewsReplyImgLayout = (LinearLayout) findViewById(R.id.news_reply_img_layout);
        // 查找新闻回复回复Layout
        mNewsReplyEditLayout = (LinearLayout) findViewById(R.id.news_reply_edit_layout);
        // 新闻回复内容
        mNewsReplyContent = (TextView) findViewById(R.id.news_reply_edittext);

        NewsDetailsOnClickListener newsDetailsOnClickListener = new NewsDetailsOnClickListener();
        // 上一篇新闻
        Button newsDetailsTitlebarPref = (Button) findViewById(R.id.newsdetails_titlebar_previous);
        newsDetailsTitlebarPref.setOnClickListener(newsDetailsOnClickListener);
        // 下一篇新闻
        Button newsDetailsTitlebarNext = (Button) findViewById(R.id.newsdetails_titlebar_next);
        newsDetailsTitlebarNext.setOnClickListener(newsDetailsOnClickListener);
        // 新闻回复条数Button
        mNewsdetailsTitlebarComm = (Button) findViewById(R.id.newsdetails_titlebar_comments);
        mNewsdetailsTitlebarComm.setOnClickListener(newsDetailsOnClickListener);
        // 发表新闻回复图片Button
        mNewsReplyImgBtn = (ImageButton) findViewById(R.id.news_reply_img_btn);
        mNewsReplyImgBtn.setOnClickListener(newsDetailsOnClickListener);
        // 发表回复
        Button newsReplyPost = (Button) findViewById(R.id.news_reply_post);
        newsReplyPost.setOnClickListener(newsDetailsOnClickListener);

        // 获取传递的数据
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        // 设置标题栏名称
        String categoryName = bundle.getString("categoryName");
        TextView titleBarTitle = (TextView) findViewById(R.id.newsdetails_titlebar_title);
        titleBarTitle.setText(categoryName);
        // 获取新闻集合
        Serializable s = bundle.getSerializable("newsDate");
        mNewsData = (ArrayList<HashMap<String, Object>>) s;
        // 获取点击位置
        mCursor = mPosition = bundle.getInt("position");

        // 动态创建新闻视图,并赋值
        mNewsBodyInflater = getLayoutInflater();
        inflateView(0);
    }

    /**
     * 处理NewsDetailsTitleBar点击事件
     */
    class NewsDetailsOnClickListener implements OnClickListener
    {
        @Override
        public void onClick(View v)
        {
            InputMethodManager m = (InputMethodManager) mNewsReplyContent.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            switch (v.getId())
            {
            // 上一条新闻
            case R.id.newsdetails_titlebar_previous:
                showPrevious();
                break;
            // 下一条新闻
            case R.id.newsdetails_titlebar_next:
                showNext();
                break;
            // 显示评论
            case R.id.newsdetails_titlebar_comments:
                Intent intent = new Intent(NewsDetailsActivity.this, CommentsActivity.class);
                //传递新闻ID
                intent.putExtra("nid", mNid);
                startActivity(intent);
                break;
            // 新闻回复图片
            case R.id.news_reply_img_btn:
                mNewsReplyImgLayout.setVisibility(View.GONE);
                mNewsReplyEditLayout.setVisibility(View.VISIBLE);
                mNewsReplyContent.requestFocus();
                m.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);
                keyboardShow = true;
                break;
            // 发表新闻回复
            case R.id.news_reply_post:
                mNewsReplyEditLayout.post(new PostCommentThread());
                mNewsReplyImgLayout.setVisibility(View.VISIBLE);
                mNewsReplyEditLayout.setVisibility(View.GONE);
                m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                break;
            }
        }
    }

    /**
     * 处理新闻NewsBody触摸事件
     */
    class NewsBodyOnTouchListener implements OnTouchListener
    {
        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            switch (event.getAction())
            {
            // 手指按下
            case MotionEvent.ACTION_DOWN:
                if (keyboardShow)
                {
                    // 设置新闻回复Layout是否可见
                    mNewsReplyImgLayout.setVisibility(View.VISIBLE);
                    mNewsReplyEditLayout.setVisibility(View.GONE);
                    InputMethodManager m = (InputMethodManager) mNewsReplyContent.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                    keyboardShow = false;
                }
                // 记录起始坐标
                mStartX = event.getX();
                break;
            // 手指抬起
            case MotionEvent.ACTION_UP:
                // 往左滑动
                if (event.getX() < mStartX)
                {
                    showPrevious();
                }
                // 往右滑动
                else if (event.getX() > mStartX)
                {
                    showNext();
                }
                break;
            }
            return true;
        }
    }

    /**
     * 显示下一条新闻
     */
    private void showNext()
    {
        // 判断是否是最后一篇win问:mPosition: 当前新闻的索引
        if (mPosition < mNewsData.size() - 1)
        {
            // 设置下一屏动画
            mNewsBodyFlipper.setInAnimation(this, R.anim.push_left_in);
            mNewsBodyFlipper.setOutAnimation(this, R.anim.push_left_out);
            mPosition++;
            HashMap<String, Object> hashMap = mNewsData.get(mPosition);
            mNid = (Integer) hashMap.get("nid");
            // 判断下一屏是否已经创建
            if (mPosition >= mNewsBodyFlipper.getChildCount())
            {
                inflateView(mNewsBodyFlipper.getChildCount());
            }
            // 显示下一屏
            mNewsBodyFlipper.showNext();
        } else
        {
            Toast.makeText(this, R.string.no_next_news, Toast.LENGTH_SHORT).show();
        }
        System.out.println(mCursor + ";" + mPosition);
    }

    private void showPrevious()
    {
        if (mPosition > 0)
        {
            mPosition--;
            // 记录当前新闻编号
            HashMap<String, Object> hashMap = mNewsData.get(mPosition);
            mNid = (Integer) hashMap.get("nid");
            if (mCursor > mPosition)
            {
                mCursor = mPosition;
                inflateView(0);
                System.out.println(mNewsBodyFlipper.getChildCount());
                mNewsBodyFlipper.showNext();// 显示下一页
            }
            mNewsBodyFlipper.setInAnimation(this, R.anim.push_right_in);// 定义下一页进来时的动画
            mNewsBodyFlipper.setOutAnimation(this, R.anim.push_right_out);// 定义当前页出去的动画
            mNewsBodyFlipper.showPrevious();// 显示上一页
        } else
        {
            Toast.makeText(this, R.string.no_pre_news, Toast.LENGTH_SHORT).show();
        }
    }

    private void inflateView(int index)
    {
        // 动态创建新闻视图,并赋值
        View newsBodyLayout = mNewsBodyInflater.inflate(R.layout.news_body, null);
        // 获取点击新闻基本信息
        HashMap<String, Object> hashMap = mNewsData.get(mPosition);
        // 新闻标题
        TextView newsTitle = (TextView) newsBodyLayout.findViewById(R.id.news_body_title);
        newsTitle.setText(hashMap.get("newslist_item_title").toString());
        // 发布时间和出处
        TextView newsPtimeAndSource = (TextView) newsBodyLayout.findViewById(R.id.news_body_ptime_source);
        newsPtimeAndSource.setText(hashMap.get("newslist_item_ptime").toString() + "    " + hashMap.get("newslist_item_source").toString());
        // 新闻编号
        mNid = (Integer) hashMap.get("nid");
        // 新闻回复数
        mNewsdetailsTitlebarComm.setText(hashMap.get("newslist_item_comments") + "跟帖");

        // 把新闻视图添加到Flipper中
        mNewsBodyFlipper = (ViewFlipper) findViewById(R.id.news_body_flipper);
        mNewsBodyFlipper.addView(newsBodyLayout, index);

        // 给新闻Body添加触摸事件
        mNewsDetails = (CustomTextView) newsBodyLayout.findViewById(R.id.news_body_details);
        mNewsDetails.setOnTouchListener(new NewsBodyOnTouchListener());

        // 启动线程
        new UpdateNewsThread().start();
    }

    /**
     * 获取新闻详细信息
     * 
     * @return
     */
    private ArrayList<HashMap<String, Object>> getNewsBody()
    {
//        String retStr = "网络连接失败,请稍后再试";
        ArrayList<HashMap<String, Object>> bodyList = new ArrayList<HashMap<String,Object>>();
        SyncHttp syncHttp = new SyncHttp();
        String url = "http://10.0.2.2:8080/web/getNews";
        String params = "nid=" + mNid;
        try
        {
            String retString = syncHttp.httpGet(url, params);
            JSONObject jsonObject = new JSONObject(retString);
            // 获取返回码,0表示成功
            int retCode = jsonObject.getInt("ret");
            if (0 == retCode)
            {
                JSONObject dataObject = jsonObject.getJSONObject("data");
                JSONObject newsObject = dataObject.getJSONObject("news");
//              测试自定义控件                
//                retStr = newsObject.getString("body");
                JSONArray bodyArray = newsObject.getJSONArray("body");
                for (int i=0;i<bodyArray.length();i++)
                {
                    JSONObject object = (JSONObject)bodyArray.opt(i); 
                    HashMap<String, Object> hashMap = new HashMap<String, Object>();
                    hashMap.put("index", object.get("index"));
                    hashMap.put("type", object.get("type"));
                    hashMap.put("value", object.get("value"));
                    bodyList.add(hashMap);
                }
            }

        } catch (Exception e)
        {
            e.printStackTrace();
        }
//      测试自定义控件    
//        return retStr;
        return bodyList;
    }

    /**
     * 更新新闻内容
     * 
     * @author coolszy
     *@date 2012-4-22
     *@blog http://blog.92coding.com
     */
    private class UpdateNewsThread extends Thread
    {
        @Override
        public void run()
        {
            // 从网络上获取新闻
//            测试自定义控件
//            String newsBody = getNewsBody();
            ArrayList<HashMap<String, Object>> bodyList = getNewsBody();
            Message msg = mHandler.obtainMessage();
            System.out.println("@@@:"+bodyList.size());
            msg.arg1 = FINISH;
            msg.obj = bodyList;
            mHandler.sendMessage(msg);
        }
    }

    /**
     * 发表回复
     * 
     * @author coolszy
     *@date 2012-4-22
     *@blog http://blog.92coding.com
     */
    private class PostCommentThread extends Thread
    {
        @Override
        public void run()
        {
            SyncHttp syncHttp = new SyncHttp();
            String url = "http://10.0.2.2:8080/web/postComment";
            List<Parameter> params = new ArrayList<Parameter>();
            params.add(new Parameter("nid", mNid + ""));
            params.add(new Parameter("region", "江苏省连云港市"));
            params.add(new Parameter("content", mNewsReplyContent.getText().toString()));
            try
            {
                String retStr = syncHttp.httpPost(url, params);
                JSONObject jsonObject = new JSONObject(retStr);
                int retCode = jsonObject.getInt("ret");
                if (0 == retCode)
                {
                    Toast.makeText(NewsDetailsActivity.this, R.string.post_success, Toast.LENGTH_SHORT).show();
                    mNewsReplyImgLayout.setVisibility(View.VISIBLE);
                    mNewsReplyEditLayout.setVisibility(View.GONE);
                    return;
                }

            } catch (Exception e)
            {
                e.printStackTrace();
            }
            Toast.makeText(NewsDetailsActivity.this, R.string.post_failure, Toast.LENGTH_SHORT).show();
        }
    }

}

三、自定义控件实现图文混排

1.1 自定义控件图文混排

 

package com.szy.news.view;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.text.Html;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

import com.szy.news.activity.R;

/**
 *@author coolszy
 *@date 2012-5-1
 *@blog http://blog.92coding.com
 */

public class CustomTextView extends LinearLayout
{
    private Context mContext;
    private TypedArray mTypedArray;
    
    public CustomTextView(Context context)
    {
        this(context, null);
    }

    public CustomTextView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        this.mContext = context;
        
        setOrientation(LinearLayout.VERTICAL);
        mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.customTextView);

    }

    public void setText(ArrayList<HashMap<String, Object>> datas)
    {
        for (HashMap<String, Object> hashMap : datas)
        {
            //如果是图片
            if (hashMap.get("type").equals("image"))
            {
                int imageWidth = mTypedArray.getDimensionPixelOffset(R.styleable.customTextView_image_width, 100);
                int imageheight = mTypedArray.getDimensionPixelOffset(R.styleable.customTextView_image_height, 100);
                //创建ImageView并设置属性
                ImageView imageView = new ImageView(mContext);
                LayoutParams params = new LayoutParams(imageWidth, imageheight);
                params.gravity = Gravity.CENTER_HORIZONTAL;//居中
                imageView.setLayoutParams(params);
                imageView.setImageResource(R.drawable.kuka);//默认图片
                imageView.setScaleType(ScaleType.CENTER_INSIDE);
                addView(imageView);
                //启动线程,异步加载图片
                new DownloadPicThread(imageView,hashMap.get("value").toString()).start();
            } 
            //反之则已文本显示
            else
            {
                //创建TextView并设置属性
                TextView textView = new TextView(mContext);
                textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                float textSize = mTypedArray.getDimension(R.styleable.customTextView_textSize, 16);
                int textColor = mTypedArray.getColor(R.styleable.customTextView_textColor, 0xFF000000);
                textView.setTextSize(textSize);
                textView.setTextColor(textColor);
                textView.setText(Html.fromHtml(hashMap.get("value").toString()));
                addView(textView);
            }
        }
    }

    private Handler mHandler = new Handler()
    {
        @SuppressWarnings("unchecked")
        @Override
        public void handleMessage(Message msg)
        {
            HashMap<String, Object> hashMap = (HashMap<String, Object>)msg.obj;
            ImageView imageView = (ImageView)hashMap.get("imageview");
            LayoutParams params = new LayoutParams(msg.arg1,msg.arg2);
            params.gravity = Gravity.CENTER_HORIZONTAL;//居中
            imageView.setLayoutParams(params);
            Drawable drawable = (Drawable)hashMap.get("drawable");
            imageView.setImageDrawable(drawable);
        }
        
    };
    
    private class DownloadPicThread extends Thread
    {
        private ImageView mImageView;
        private String mUrl;

        public DownloadPicThread(ImageView imageView, String url)
        {
            super();
            this.mImageView = imageView;
            this.mUrl = url;
        }

        public void run()
        {
            Drawable drawable = null;
            int newImgWidth = 0;
            int newImgHeigth = 0;
            try
            {
                drawable = Drawable.createFromStream(new URL(mUrl).openStream(), "image");
                newImgWidth = drawable.getIntrinsicWidth() / 3;
                newImgHeigth = drawable.getIntrinsicHeight() / 3;
            } catch (Exception e)
            {
                e.printStackTrace();
            }
            //为了更好的看到效果,让线程休眠2秒
            SystemClock.sleep(2000);
            //使用Handler更新UI
            Message msg = mHandler.obtainMessage();
            HashMap<String, Object> hashMap = new HashMap<String, Object>();
            hashMap.put("imageview", mImageView);
            hashMap.put("drawable", drawable);
            msg.obj = hashMap;
            msg.arg1 = newImgWidth;
            msg.arg2 = newImgHeigth;
            mHandler.sendMessage(msg);
        }
    }
}

 

 

 

一、新闻客户端布局代码

1.1 使GridView控件第一项默认选中

public class CustomSimpleAdapter extends SimpleAdapter
{

    public CustomSimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
    {
        super(context, data, resource, from, to);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View v = super.getView(position, convertView, parent);
        //更新第一个TextView的背景
        if (position==0)
        {
            TextView categoryTitle = (TextView)v;
            categoryTitle.setBackgroundResource(R.drawable.categorybar_item_background);
            categoryTitle.setTextColor(0XFFFFFFFF);
        }
        return v;
    }
}

 

 

 

一、新闻客户端布局代码

1.1werwe

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值