原文出自:方杰|http://fangjie.info/?p=184转载请注明出处

最终效果演示:http://fangjie.info/?page_id=54
该项目代码已经放到github:https://github.com/JayFang1993/SinaWeibo

一.首先是ListView的adapter。

因为微博列表的Item不是规则的,比如说有些微博有转发子微博,有些没有,有些有图片,有些没有图片,所以说很不固定。这里就采用BaseAdapter,要自己为微博Item设计一个WeiboAdapter.java

package com.fangjie.weibo.ui;

import java.util.Date;
import java.util.List;
import com.fangjie.weibo.R;
import com.fangjie.weibo.bean.Weibo;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class WeiboAdapter extends BaseAdapter {

    private Context context;
    private List<Weibo> weibos;    

    public WeiboAdapter(Context context,List<Weibo> weibos) {
        System.out.println(weibos.get(1).content);
        this.context=context;
        this.weibos=weibos;
    }

    public int getCount() {
        return weibos.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
         //position代表位置  

        //通过View关联自定义Item布局,进行填充  

         if(convertView == null)
         {
             convertView = View.inflate(context, R.layout.wb_item, null);
         }

        System.out.println(position);
        final Weibo weibo =weibos.get(position);

        //获取要显示的组件,注意findViewById的调用对象是上面填充了Item的布局的对象View
        TextView tv_name = (TextView)convertView.findViewById(R.id.txt_wb_item_uname);
        TextView tv_content = (TextView)convertView.findViewById(R.id.txt_wb_item_content);
        TextView tv_time =(TextView)convertView.findViewById(R.id.txt_wb_item_time);
        TextView tv_from =(TextView)convertView.findViewById(R.id.txt_wb_item_from);
        TextView tv_comment =(TextView)convertView.findViewById(R.id.txt_wb_item_comment);
        TextView tv_repost =(TextView)convertView.findViewById(R.id.txt_wb_item_redirect);  

        LinearLayout zlayout=(LinearLayout)convertView.findViewById(R.id.lyt_wb_item_sublayout);
        TextView tv_zcontent=(TextView)convertView.findViewById(R.id.txt_wb_item_subcontent); 

        final ImageView iv_userhead=(ImageView)convertView.findViewById(R.id.img_wb_item_head);
        ImageView iv_isv=(ImageView)convertView.findViewById(R.id.img_wb_item_V);
        ImageView iv_content_pic=(ImageView)convertView.findViewById(R.id.img_wb_item_content_pic);
        ImageView iv_zcontent_pic=(ImageView)convertView.findViewById(R.id.img_wb_item_content_subpic);

        //组件添加内容
        tv_content.setText(weibo.getContent());
        tv_name.setText(weibo.getUser().getName());
        tv_from.setText("来自:"+Html.fromHtml(weibo.getFrom()));
        tv_repost.setText(weibo.getReposts_count()+"");
        tv_comment.setText(weibo.getComments_count()+"");
        tv_time.setText(dealTime(weibo.getTime()));

        loadBitmap(weibo.getUser().getProfile_image_url(), iv_userhead,80,80);  

        if(!weibo.getBmiddle_pic().equals(""))
        {
            loadBitmap(weibo.getBmiddle_pic(), iv_content_pic,0,0);
            iv_content_pic.setVisibility(View.VISIBLE);
        }
        else
        {
            iv_content_pic.setVisibility(View.GONE);
        }

        if(weibo.getUser().isIsv())
            iv_isv.setVisibility(View.VISIBLE);
        else
            iv_isv.setVisibility(View.GONE);

        if(weibo.getWeibo()!=null)
        {
            zlayout.setVisibility(View.VISIBLE);
            tv_zcontent.setText("@"+weibo.getWeibo().getUser().getName()+":"+weibo.getWeibo().getContent());
            if(!weibo.getWeibo().getBmiddle_pic().equals(""))
            {
                loadBitmap(weibo.getWeibo().getBmiddle_pic(), iv_zcontent_pic,0,0);
                iv_zcontent_pic.setVisibility(View.VISIBLE);
            }
        }
        else
            zlayout.setVisibility(View.GONE);

        return convertView;
    }

    public void addItem(Weibo weibo)
    {
        weibos.add(weibo);
    }
}

微博Item的布局文件 wb_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@drawable/list_item">
      <ImageView
          android:id="@+id/img_wb_item_head"
          android:layout_width="48dp"
          android:layout_height="48dp"
          android:src="@drawable/user_head"
          android:layout_marginLeft="3dp"
          android:layout_marginTop="5dp"/>

      <!-- 右边框架 -->
      <LinearLayout
      android:layout_width="match_parent"
      android:orientation="vertical"
      android:layout_height="wrap_content"
      android:layout_margin="5dp">
         <!-- 用户名称、新浪认证部分 -->
        <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal">
              <!-- 用户名称 -->
              <TextView android:id="@+id/txt_wb_item_uname"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textColor="#424d54"
                  android:textSize="15dp"
              />
              <!-- 新浪认证 -->
               <ImageView android:id="@+id/img_wb_item_V"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:visibility="gone"
                      android:src="@drawable/v"/>
              <RelativeLayout
                           android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="right">
                      <!-- 发布时间 -->
                     <TextView android:id="@+id/txt_wb_item_time"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="1小时前"
                      android:textColor="#efa608"
                      android:textSize="12dp"
                      />
            </RelativeLayout>
         </LinearLayout> 

         <!-- 微博正文内容 -->
        <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical">
           <!-- 微博正文内容 -->
             <TextView android:id="@+id/txt_wb_item_content"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="微博正文内容微博正文内容微博正文内容微博正文内容微博正文内容微博正文内容微博正文内容"
              android:textColor="#6b717b"
              android:textSize="13dp"
              />
             <ImageView android:id="@+id/img_wb_item_content_pic"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:src="@drawable/user_head"
              android:layout_marginTop="3dp"
              android:visibility="gone"
             />
         </LinearLayout>

           <!-- 转发的子微博内容 -->
        <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/lyt_wb_item_sublayout"
          android:orientation="vertical"
          android:layout_marginTop="3dp"
          android:visibility="gone"
          android:background="@drawable/popup">

               <!-- 微博正文内容 -->
             <TextView android:id="@+id/txt_wb_item_subcontent"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="微博正文内容微博正文内容微博正文内容微博正文内容微博正文内容微博正文内容微博正文内容"
              android:textColor="#6b717b"
              android:textSize="13dp"
              />
             <ImageView android:id="@+id/img_wb_item_content_subpic"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:src="@drawable/user_head"
              android:layout_marginTop="3dp"
              android:visibility="gone"
             />
      </LinearLayout>
           <!-- 微博来源部分 -->
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:layout_marginTop="3dp"
          android:layout_marginBottom="3dp" >
           <!-- 用户名称 -->
          <TextView android:id="@+id/txt_wb_item_from"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="来自:Touch Android"
              android:textColor="#9ba0aa"
              android:textSize="12dp"
          />
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:gravity="right">
              <TextView android:id="@+id/txt_wb_item_redirect"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:drawableLeft="@drawable/redirect_icon"
                      android:text="10"
                      android:textColor="#9ba0aa"
                      android:textSize="13dp"
                  />
              <TextView android:id="@+id/txt_wb_item_comment"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_marginLeft="5dp"
                      android:drawableLeft="@drawable/comment_icon"
                      android:text="100"
                      android:textColor="#9ba0aa"
                      android:textSize="13dp"/>
           </LinearLayout>
      </LinearLayout>
  </LinearLayout>
</LinearLayout>

WeiboAdapter的作用就是将List<Weibo> weibos的数据绑定到每一个View的控件上去。注:关于图片ImagView控件的加载loadBitmap采用的是异步加载,在下一篇中会讲到。

二.ListView的细节——底部加载更多

首先需要为这个东西写一个布局文件 load_more.xml,布局很简单,就是一个button。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
  <Button
           android:id="@+id/loadMoreButton"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:text="加载更多"
           android:onClick="loadMore"/>
</LinearLayout>

然后在HomeActivity.java中为ListView增加一个底部视图,ListView.addFooterView(View view);

        //设置列表底部视图-加载更多
        View loadMoreView = getLayoutInflater().inflate(R.layout.load_more, null);
        loadMoreButton= (Button) loadMoreView.findViewById(R.id.loadMoreButton);
        weibolist.addFooterView(loadMoreView);

三.ListView的刷新按钮

在点击主界面的刷新按钮时,会出现progressbar,这些都不是很难。首先写好一个progress的布局,在刷新任务开始之前然progressbar显示,任务结束后就View.gone就OK啦,详细请看源代码HomeActivity.

四.注意:我在写ListView的时候,在模拟器测试完全OK,但是在真机上调试时,ListView与上面的TitleBar和TabHost交界处会有阴影。加上这句就可以了。

ListView.setFadingEdgeLength(0);

可能讲的不是很直观,最后附上HomeActivity.java的全部代码,这个就是Home界面的代码

package com.fangjie.weibo.ui;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fangjie.weibo.R;
import com.fangjie.weibo.bean.Task;
import com.fangjie.weibo.bean.Weibo;
import com.fangjie.weibo.logic.MainService;
import com.fangjie.weibo.util.SharePreferencesUtil;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class HomeActivity extends Activity implements IWeiboAcitivity {
    private ListView weibolist;
    private List&lt;Weibo&gt; weibos;
    private WeiboAdapter adapter;

    private TextView tv_title;
    private Button btn_refresh;
    private Button btn_update;

    private LinearLayout progress;

    private Button loadMoreButton;

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        init();
    }

    public void init() {
        weibolist=(ListView)findViewById(R.id.lv_weibos);
        tv_title=(TextView)findViewById(R.id.txt_wb_title);
        btn_refresh=(Button)findViewById(R.id.btn_refresh);
        btn_update=(Button)findViewById(R.id.btn_writer);
        progress=(LinearLayout)findViewById(R.id.layout_progress);

        //设置列表底部视图-加载更多
        View loadMoreView = getLayoutInflater().inflate(R.layout.load_more, null);
        loadMoreButton= (Button) loadMoreView.findViewById(R.id.loadMoreButton);
        weibolist.addFooterView(loadMoreView);   

        weibolist.setFadingEdgeLength(0);

        final String token=SharePreferencesUtil.getLoginUser(HomeActivity.this).getToken();
        tv_title.setText(SharePreferencesUtil.getLoginUser(HomeActivity.this).getUserName());

        Map&lt;String,Object&gt; params=new HashMap&lt;String,Object&gt;();
        params.put("token", token);
        Task task=new Task(Task.GET_WEIBOS, params);
        progress.setVisibility(View.VISIBLE);
        MainService.newTask(task);
        MainService.addActivty(HomeActivity.this);

        loadMoreButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Map&lt;String,Object&gt; params=new HashMap&lt;String,Object&gt;();
                params.put("token", token);
                params.put("max_id", weibos.get(weibos.size()-1).getWid());
                loadMoreButton.setText("正在加载,请稍候...");
                Task task=new Task(Task.LOADMORE, params);
                MainService.newTask(task);
                MainService.addActivty(HomeActivity.this);
            }
        });

        btn_refresh.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Map&lt;String,Object&gt; params=new HashMap&lt;String,Object&gt;();
                params.put("token", token);
                progress.setVisibility(View.VISIBLE);
                Task task=new Task(Task.GET_WEIBOS, params);
                MainService.newTask(task);
                MainService.addActivty(HomeActivity.this);
            }
        });

        btn_update.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(HomeActivity.this, "亲,程序猿还没写好呢...", Toast.LENGTH_LONG).show();
            }
        });
    }

    @SuppressWarnings("unchecked")
    public void refresh(int taskID, Object... objects) {
        switch (taskID)
        {
            case Task.GET_WEIBOS:
                weibos=(List&lt;Weibo&gt;)objects[0];
                adapter=new WeiboAdapter(HomeActivity.this,weibos);
                weibolist.setAdapter(adapter);
                break;
            case Task.LOADMORE:
                weibos=(List&lt;Weibo&gt;)objects[0];
                for(int i=1;i&lt;weibos.size();i++)
                    adapter.addItem(weibos.get(i));
                adapter.notifyDataSetChanged(); //数据集变化后,通知adapter
                loadMoreButton.setText("加载更多");
        }
        progress.setVisibility(View.GONE);
        MainService.reMoveActivty(HomeActivity.this);
    }
}

可能大家在HomeActivity中只看到新开一些任务,但是这些任务具体做什么操纵不清楚,大家可以看看前面的博文,因为有一个 逻辑处理的MainService处理类。针对Task.GET_WEIBOS和Task.LOADMORE,其中的代码又增加了。这里也附上MainService.java关于这两个任务的部分代码。

            //刷新微博
            case Task.GET_WEIBOS:
            {
                String token=(String)task.getParams().get("token");
                WeiboUtil weiboutil=new WeiboUtil();
                List<Weibo> weibos=weiboutil.getWeiboList(token,0);
                msg.obj=weibos;
                break;
            }
            //加载更多
            case Task.LOADMORE:
            {
                String token=(String)task.getParams().get("token");
                long max_id=(Long) task.getParams().get("max_id");
                WeiboUtil weiboutil=new WeiboUtil();
                List<Weibo> weibos=weiboutil.getWeiboList(token,max_id);
                msg.obj=weibos;
                break;
            }

Android新浪微博客户端(六)——Home界面的ListView的更多相关文章

  1. android 新浪微博客户端的表情功能的实现

    这是一篇好文章,我转来收藏,技术的最高境界是分享. 最近在搞android 新浪微博客户端,有一些心得分享弄android客户端表情功能可以用以下思路1.首页把新浪的表情下载到本地一文件夹种,表情图片 ...

  2. Android新浪微博客户端(五)——主界面的TabHost和WeiboUtil

    原文出自:方杰|http://fangjie.info/?p=183转载请注明出处 最终效果演示:http://fangjie.info/?page_id=54 该项目代码已经放到github:htt ...

  3. Android新浪微博客户端(七)——ListView中的图片异步加载、缓存

    原文出自:方杰|http://fangjie.info/?p=193转载请注明出处 最终效果演示:http://fangjie.sinaapp.com/?page_id=54 该项目代码已经放到git ...

  4. Ace教你一步一步做Android新闻客户端(五) 优化Listview

    今天写存货了 调试一些动画参数花了些时间 ,嘿嘿存货不多了就没法做教程了,今天来教大家优化listview,等下我把代码编辑下 这次代码有些多 所以我把条理给大家理清楚.思路就是把加载图片的权利交给O ...

  5. Android新浪微博客户端(四)——添加多个账户及认证

    原文出自:方杰| http://fangjie.info/?p=75转载请注明出处 二.获取用户信息并保存数据库 上面说到加载AuthActivity有两种情况,其中一种就是授权成功回调,在授权回调成 ...

  6. Android新浪微博客户端(二)——添加多个账户及认证

    原文出自:方杰| http://fangjie.info/?p=69  转载请注明出处 先看下实现效果: 欢迎界面: 第一次进入登录界面登录由于在登录界面没有已授权用户信息,所以自动跳转到授权界面. ...

  7. Android新浪微博客户端(一)——主框架搭建

    原文出自:方杰| http://fangjie.info/?p=62 转载请注明出处 提前声明的是,我是按照Ivan的这套教程学下来的. 首先,对于任何应用我们都需要建立一套消息处理机制,就是当用户在 ...

  8. Android新浪微博客户端(三)——添加多个账户及认证

    原文出自:方杰|http://fangjie.info/?p=72 转载请注明出处 一.微博OAuth2.0认证 首先来说说授权过程,我这里授权是通过SDK的,先添加SDK的jar包,微博SDK的de ...

  9. android 在fragment中获取界面的UI组件

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanc ...

随机推荐

  1. 从实践谈iOS生命周期

    从实践谈iOS生命周期 个人感觉生命周期无论在Android,还是iOS都是很重要的概念,因为在每个声明周期的状态下我们可以做很多预加载或者处理的操作.因此在这里主要总结下ViewController ...

  2. asp.net基础概念总结

    1  什么是asp.net?asp.net是一种编程语言吗? asp.net是Microsoft公司推出的新一代建立动态web应用程序的开发平台,是一种建立动态web应用程序的新技术. 不是,asp. ...

  3. sqlserver2008附加数据库——错误3415

    权限问题, 在其文件,右击属性>安全>编辑>添加>加一个everyone单击确定>其完全控制, 这样给每个用户权限 ---来自凌波小屋----冯和超笔记-----

  4. Android热更新开源项目Tinker集成实践总结

    前言 最近项目集成了Tinker,开始认为集成会比较简单,但是在实际操作的过程中还是遇到了一些问题,本文就会介绍在集成过程大家基本会遇到的主要问题. 考虑一:后台的选取 目前后台功能可以通过三种方式实 ...

  5. [转载]Java synchronized详解

    在编写一个类时,如果该类中的代码可能运行于多线程环境下,那么就要考虑同步的问题.在Java中内置了语言级的同步原语--synchronized,这也大大简化了Java中多线程同步的使用.我们首先编写一 ...

  6. 【USACO 1.1.4】破碎的项链

    [题目描述] 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的.这里是 n=29 的二个例子:                 1 2      ...

  7. backbone学习笔记(一)

    因为工作的需要,从今天起对backbone的学习过程做下记录. 学习计划: 1.1周看基本知识(2014/1/18-2014/1/25) 2.基本知识总结(2014/1/26) 3.半周按教程写hel ...

  8. Objective-C中的协议(Protocol)和类别(Category)

    1.什么是协议? 2.协议与类别的声明和使用 1.什么是协议? 在Objective-C中,不支持多继承,即不允许一个类有多个父类,但是OC提供了类似的实现方法,也就是协议.协议有点类似于Java里的 ...

  9. JavaScript设计模式之构造函数模式

    一.构造函数模式概念 构造函数用于创建特定类型的对象——不仅声明了使用过的对象,构造函数还可以接受参数以便第一次创建对象的时候设置对象的成员值.你可以自定义自己的构造函数,然后在里面声明自定义类型对象 ...

  10. Android 多线程:使用Thread和Handler

    当一个程序第一次启动时,Android会同时启动一个对应的主线程(Main Thread),主线程主要负责处理与UI相关的事件,如:用户的按键事件,用户接触屏幕的事件以及屏幕绘图事件,并把相关的事件分 ...