对学习C++的一些忠告

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

最终效果演示:http://fangjie.sinaapp.com/?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<Weibo> 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<String,Object> params=new HashMap<String,Object>();
		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<String,Object> params=new HashMap<String,Object>();
				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<String,Object> params=new HashMap<String,Object>();
				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<Weibo>)objects[0];
				adapter=new WeiboAdapter(HomeActivity.this,weibos);
				weibolist.setAdapter(adapter);
				break;
			case Task.LOADMORE:
				weibos=(List<Weibo>)objects[0];
				for(int i=1;i<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;
			}

欢迎各位关注我的个人站点:http://fangjie.sinaapp.com/

对学习C++的一些忠告

时间: 2024-08-08 13:16:33

对学习C++的一些忠告的相关文章

对学习编程者的忠告

计算机组成原理→DOS命令→汇编语言→C语言(不包括C++).代码书写规范→数据结构.编译原理.操作系统→计算机网络.数据库原理.正则表达式→其它语言(包括C++).架构…… 对学习编程者的忠告:眼过千遍不如手过一遍!书看千行不如手敲一行!手敲千行不如单步一行!单步源代码千行不如单步对应汇编一行! 单步类的实例“构造”或“复制”或“作为函数参数”或“作为函数返回值返回”或“参加各种运算”或“退出作用域”的语句对应的汇编代码几步后,就会来到该类的“构造函数”或“复制构造函数”或“运算符重载”或“析

关于学习Linux的一些忠告

以下是关于学习Linux的忠告. 1. 不要当"传教士" 很多人在讨论区不断的导致 "Linux vs. Windows" 之类的讨论,乃至争的面红耳赤,这是没有必要的. 这种争辩是浪费时间而没有任何用途的.对,你花了一下午,用很多现实"保卫"了 "Linux 比 Windows 好" 这个说法.可是 Windows 的支持者并不会喜爱上 Linux,他们仅仅略微畏缩一下,然后找一些新的依据来跟你争辩. 世界上的人们都在利用L

全栈工程师学习Linux技术的忠告

随着科技的普及,Linux作为最受欢迎的服务端操作系统,无人不知,无人不晓.当今,不论是服务器搭建,还是客户端开发,Linux系统的基础技能对全栈来说都是必备的,而了解如下几个问题可以更好的帮助你成为一名合格的全栈工程师. 1.Linux体系组织结构 学习一个系统需要了解其体系结构,这样才能更好的学习.Linux有kernel和user两种模式,内核.shell和文件系统统一形成基本的操作系统结构,Linux内核由如下几部分组成:内存管理.进程管理.设备驱动程序.文件系统和网络管理等 学习lin

眼过千遍不如手过一遍!

计算机组成原理→DOS命令→汇编语言→C语言(不包括C++).代码书写规范→数据结构.编译原理.操作系统→计算机网络.数据库原理.正则表达式→其它语言(包括C++).架构…… 对学习编程者的忠告:多用小脑和手,少用大脑.眼睛和嘴,会更快地学会编程!眼过千遍不如手过一遍!书看千行不如手敲一行!手敲千行不如单步一行!单步源代码千行不如单步Debug版对应汇编一行!单步Debug版对应汇编千行不如单步Release版对应汇编一行! http://bbs.csdn.net/topics/39181749

图书-财经:《失落的百年致富圣经》

ylbtech-图书-财经:<失落的百年致富圣经> <失落的百年致富圣经>2007年,美国Prime Time公司隆重推出一部风靡全球的纪录片<秘密>,该片堪称成功学.财富学和人生指导的经典之作.在美国和世界各地广受人们的追捧,原因是该片揭落了一个至大的秘密……据该片制版人朗达·拜恩介绍,<秘密>的灵感来源于一本失落百年的古书——<失落的百年致富圣经>,正是这本书启发了她,让她与那些伟大的励志导师勒斯·华特斯.查尔斯·哈尼尔.罗伯特·柯里尔等相遇

学习c++的50条忠告(转自C++百度贴吧)

1.把C++当成一门新的语言学习(和C没啥关系!真的.): 2.看<Thinking In C++>,不要看<C++变成死相>: 3.看<The C++ Programming Language>和<Inside The C++ Object Model>,不要因为他们很难而我们自己是初学者所以就不看: 4.不要被 VC.BCB.BC.MC.TC等词汇所迷惑——他们都是集成开发环境,而我们要学的是一门语言: 5.不要放过任何一个看上去很简单的小编程问题——他

码农-如果当初学习编程时能有人给我这些忠告该多好

在你学习编程之前思考一下你的目标 要知道编程大多时候就是在创造,当你有最终目标感时道路会更加的清晰.如果你的目标是"学习编程"而不是更具体的学习哪种程序及如何让你的生活更好,那么你可能会发现这不过是一次令人沮丧的实践. 我有点惭愧地承认我学习计算机科学的部分动机是为了证明我聪明,及我想干"聪明人"的工作.我也喜欢思考数学和理论(<哥德尔.艾舍尔.巴赫:集异璧之大成 >这本书在我易受影响的年纪进入了我的脑海),编程是一个不错的选择.当然这并不足以使我坚持这

学习C++语言的50条忠告

50条忠告:(其中有几条觉得写的不够贴切,所以删了,发了余下的部分) 1.把C++当成一门新的语言学习: 2.看<Thinking In C++>,不要看<C++变成死相>: 3.看<The C++ Programming Language>和<Inside The C++ Object Model>,不要因为他们很难而我们自己是初学者所以就不看: 4.不要被VC.BCB.BC.MC.TC等词汇所迷惑——他们都是集成开发环境,而我们要学的是一门语言: 5.不

初学者学习C++的50条忠告

1.把C++当成一门新的语言学习(和C没啥关系!真的.); 2.看<Thinking In C++>,不要看<C++变成死相>; 3.看<The C++ Programming Language>和<Inside The C++ Object Model>,不要因为他们很难而我们自己是初学者所以就不看; 4.不要被VC.BCB.BC.MC.TC等词汇所迷惑--他们都是集成开发环境,而我们要学的是一门语言; 5.不要放过任何一个看上去很简单的小编程问题--他们