package com.ncsyeyy.YeyyQQTabHost.moudle;
/**
* Created by yeyy on 10/6/2015.
*/
public class MyDiscussion {
// 定义讨论组的实体类
public String Name;//名字
public int ImgId;//图片
public String Info;//信息
public MyDiscussion() {
super();
}
public MyDiscussion(String name, int imgId, String info) {
super();
Name = name;
ImgId = imgId;
Info = info;
}
}
package com.ncsyeyy.YeyyQQTabHost.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.ncsyeyy.YeyyQQTabHost.R;
import com.ncsyeyy.YeyyQQTabHost.activity.MyDiscussionActivity;
import com.ncsyeyy.YeyyQQTabHost.moudle.MyDiscussion;
import java.util.ArrayList;
import java.util.List;
/**
* Created by yeyy on 10/6/2015.
*/
public class MyDiscussionListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List list=new ArrayList();
private Context context;
public MyDiscussionListAdapter(List l, Context c) {
list = l;
context = c;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder=null;
if (convertView==null){
// 初始化讨论组item视图
convertView= LayoutInflater.from(context).inflate(R.layout.activity_mydiscussion_item,null);
viewHolder=new ViewHolder();
// 获得视图中的对象控件
viewHolder.Image=(ImageView)convertView.findViewById(R.id.iv_dis);
viewHolder.Name=(TextView)convertView.findViewById(R.id.tv_dis_title);
viewHolder.Number=(TextView)convertView.findViewById(R.id.tv_dis_info);
convertView.setTag(viewHolder);
} else {
viewHolder=(ViewHolder)convertView.getTag();
}
// 设置控件的属性
viewHolder.Image.setBackgroundResource(list.get(position).ImgId);
viewHolder.Name.setText(list.get(position).Name);
viewHolder.Number.setText(list.get(position).Info);
return convertView
}
// 定义内部类作为占位符组合
class ViewHolder {
ImageView Image;
TextView Name;
TextView Number;
}
}
package com.ncsyeyy.YeyyQQTabHost.activity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import com.ncsyeyy.YeyyQQTabHost.R;
import com.ncsyeyy.YeyyQQTabHost.adapter.MyDiscussionListAdapter;
import com.ncsyeyy.YeyyQQTabHost.moudle.MyDiscussion;
import java.util.ArrayList;
import java.util.List;
/**
* Created by yeyy on 10/6/2015.
*/
public class MyDiscussionActivity extends Activity {
private List l=new ArrayList();
private ListView dis_lv;
private MyDiscussionListAdapter myDiscussionListAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mydiscussion);
getData();
dis_lv = (ListView) findViewById(R.id.lv_myDiscussion);
myDiscussionListAdapter = new MyDiscussionListAdapter(l,MyDiscussionActivity.this);
dis_lv.setAdapter(myDiscussionListAdapter);
}
private void getData(){
l.add(new MyDiscussion("临时讨论组",R.drawable.ic_launcher,"张三,李四,王五"));
l.add(new MyDiscussion("项目讨论组",R.drawable.ic_launcher,"小一,小二,小七"));
l.add(new MyDiscussion("自定义研讨组",R.drawable.ic_launcher,"a,b,c"));
l.add(new MyDiscussion("课程讨论组",R.drawable.ic_launcher,"wo,ni,ta"));
}
}
package com.ncsyeyy.YeyyQQTabHost.activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import com.ncsyeyy.YeyyQQTabHost.R;
import com.ncsyeyy.YeyyQQTabHost.moudle.MyDiscussion;
public class MyActivity extends TabActivity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 得到activity中的TabHost对象
TabHost tabHost=getTabHost();
// 定义tabHost中的tabHost对象
TabHost.TabSpec spec;
Intent intent;
// 设置第一个标签页的布局
intent=new Intent(this,MyFriendActivity.class);
//setIndicator()可以设置选项卡得图标和文字。
spec =tabHost.newTabSpec("0").setIndicator("好友").setContent(intent);
// 添加到tabHost中
tabHost.addTab(spec);
// 设置第二个标签页的布局
intent=new Intent(this,MyGroupActivity.class);
spec =tabHost.newTabSpec("1").setIndicator("群").setContent(intent);
// 添加到tabHost中
tabHost.addTab(spec);
// 设置第三个标签页的布局
intent=new Intent(this,MyDiscussionActivity.class);
spec =tabHost.newTabSpec("2").setIndicator("讨论组").setContent(intent);
tabHost.addTab(spec);
// 设置当前的tabHost的选中标签
tabHost.setCurrentTab(1);
}
}
package com.ncsyeyy.YeyyXLTabHost;
import android.app.Activity;
import android.os.Bundle;
/**
* Created by yeyy on 10/6/2015.
*/
public class OneActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
}
}
第五步,功能实现
package com.ncsyeyy.YeyyXLTabHost;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
public class MyActivity extends TabActivity implements RadioGroup.OnCheckedChangeListener {
private RadioGroup radioGroup;
private TabHost tabHost;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 实例化TabHost
tabHost = this.getTabHost();
// 添加选项卡,并且设置跳转intent
tabHost.addTab(tabHost.newTabSpec("ONE").setIndicator("ONE").setContent(new Intent(this, OneActivity.class)));
tabHost.addTab(tabHost.newTabSpec("TWO").setIndicator("TWO").setContent(new Intent(this, TwoActivity.class)));
tabHost.addTab(tabHost.newTabSpec("THREE").setIndicator("THREE").setContent(new Intent(this, ThreeActivity.class)));
tabHost.addTab(tabHost.newTabSpec("FOUR").setIndicator("FOUR").setContent(new Intent(this, FourActivity.class)));
tabHost.addTab(tabHost.newTabSpec("FIVE").setIndicator("FIVE").setContent(new Intent(this, FiveActivity.class)));
// 得到radioGroup对象
radioGroup = (RadioGroup) findViewById(R.id.main_radio);
// 设置radioGroup对象的切换监听器
radioGroup.setOnCheckedChangeListener(this);
}
//实现OnCheckedChangeListener中的RadioGroup的选项切换回调函数
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// 根据所选中的RadioGroup的选项id,设置TabHost选项卡
switch (checkedId){
case R.id.radio_btn0:
tabHost.setCurrentTabByTag("ONE");
break;
case R.id.radio_btn1:
tabHost.setCurrentTabByTag("TWO");
break;
case R.id.radio_btn2:
tabHost.setCurrentTabByTag("THREE");
break;
case R.id.radio_btn3:
tabHost.setCurrentTabByTag("FOUR");
break;
case R.id.radio_btn4:
tabHost.setCurrentTabByTag("FIVE");
break;
}
}
}
源码地址:http://download.csdn.net/detail/csdnyuandaimaxuexi/9177503