欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

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

来源: 开发者 投稿于  被查看 46446 次 评论:35

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


 

一.TabHost的实现

之前的一篇文章讲的就是TabHost,但是那个是用Fragment实现TabHost,这里我就尝试用另一种方式,继承TabActivity的方式实现TabHost。
MainActivity.java

 

public class MainActivity extends TabActivity{
	private TabHost tabHost;

	private static final String HOME_TAB=home; 
	private static final String AT_TAB=at; 
	private static final String MSG_TAB=msg; 
	private static final String MORE_TAB=more; 

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

	    tabHost = this.getTabHost();
	    TabSpec homeSpec=tabHost.newTabSpec(HOME_TAB).setIndicator(HOME_TAB).setContent(new Intent(this,HomeActivity.class));
	    TabSpec atSpec=tabHost.newTabSpec(AT_TAB).setIndicator(AT_TAB).setContent(new Intent(this,AtActivity.class));
	    TabSpec msgSpec=tabHost.newTabSpec(MSG_TAB).setIndicator(MSG_TAB).setContent(new Intent(this,MsgActivity.class));
	    TabSpec moreSpec=tabHost.newTabSpec(MORE_TAB).setIndicator(MORE_TAB).setContent(new Intent(this,MoreActivity.class));

	    tabHost.addTab(homeSpec);
	    tabHost.addTab(atSpec);
	    tabHost.addTab(msgSpec);
	    tabHost.addTab(moreSpec);

	    tabHost.setCurrentTabByTag(HOME_TAB);

	    RadioGroup radioGroup =  (RadioGroup) this.findViewById(R.id.main_radio);
	    final RadioButton rb=(RadioButton)this.findViewById(R.id.rb_home);
	    rb.setBackgroundResource(R.drawable.tabhost_press);

	    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
			public void onCheckedChanged(RadioGroup group, int checkedId)
			{
				rb.setBackgroundResource(R.drawable.tabhost_bg_selector);
				switch (checkedId)
				{
					case R.id.rb_home:
						tabHost.setCurrentTabByTag(HOME_TAB);
						break;

					case R.id.rb_at:
						tabHost.setCurrentTabByTag(AT_TAB);
						break;

					case R.id.rb_mess:
						tabHost.setCurrentTabByTag(MSG_TAB);
						break;

					case R.id.rb_more:
						tabHost.setCurrentTabByTag(MORE_TAB);
						break;

					default:
						break;
				}
			}
		});

	}
}

 

布局文件:main.xml

 



    android:background=#ffffff
    >
	
	 	 android:layout_width=fill_parent
	     android:layout_height=wrap_content 
	     android:visibility=gone/>
	<framelayout android:id="@android:id/tabcontent<!--{cke_protected}{C}%3C!%2D%2D%E6%B3%A8%E6%84%8F%E8%BF%99%E9%87%8C%20%2D%2D%3E--">
		 android:layout_width=fill_parent
	     android:layout_height=fill_parent/>
	
	    
	    
	    
	    	    	    
	
</framelayout>

 

其中的每一个Tab选项的内容都是一个Activity,然后再去针对性的写每一个Tab下的内容。

二.WeiboAPI WeiboUtil.java

这个类的操作主要是发Http请求,然后解析数据,封装到bean中。这里我只写好了 读取所关注人的微博列表。下面的HttpRequest是我写的一个HTTP请求的方法,因为经常涉及到Http请求,就把这部分代码抽出来写成一个方法。

WeiboUtil.java的全部代码

 

package com.fangjie.weibo.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

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

import com.fangjie.weibo.bean.User;
import com.fangjie.weibo.bean.Weibo;
public class WeiboUtil {

	public List getWeiboList(String token, long i) {
		List weibos=new ArrayList();
		String url=https://api.weibo.com/2/statuses/home_timeline.json;
		String params=access_token=+token+&max_id=+i;
		String result=HttpRequest(1, url, params);
		try {
			JSONObject json_res=new JSONObject(result);
			JSONArray json_weibos=json_res.getJSONArray(statuses);
			for(int j=0;j欢迎各位关注我的个人站点:http://fangjie.sinaapp.com/

 

用户评论