您的位置:首页 > 其它

Tab组件和ListView组件结合

2012-10-11 14:46 357 查看
效果图:







布局文件:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image2" />
<ImageView
android:id="@+id/image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image3" />

</RelativeLayout>

tab01_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<!-- 左边是图片 -->

<ImageView
android:id="@+id/photo"
android:layout_gravity="top"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="10dp" />
<!-- 右边是个布局 -->

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<!-- 上边是个布局 -->

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TextView>

<TextView
android:id="@+id/publish"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right" >
</TextView>
</LinearLayout>
<!-- 发布内容 -->

<TextView
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>

</LinearLayout>

tab01.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

</RelativeLayout>

MainActivity.java

package cn.bzu.listviewtabwidget;

import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.support.v4.app.NavUtils;

public class MainActivity extends TabActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 获取选项卡组
TabHost tabHost = getTabHost();

// 引用布局文件
LayoutInflater inflater = LayoutInflater.from(this);
inflater.inflate(R.layout.activity_main,
tabHost.getTabContentView());
// 创建Tab对象
Intent intent=new Intent();
intent.setClass(this, tab01Activity.class);
Resources res = getResources();
TabHost.TabSpec tab01 = tabHost.newTabSpec("tab01")
.setIndicator("", res.getDrawable(R.drawable.a))
.setContent(intent);
TabHost.TabSpec tab02 = tabHost.newTabSpec("tab02")
.setIndicator("", res.getDrawable(R.drawable.b))
.setContent(R.id.image2);
TabHost.TabSpec tab03 = tabHost.newTabSpec("tab03")
.setIndicator("", res.getDrawable(R.drawable.c))
.setContent(R.id.image3);
//将创建好的tab对象放入到tabhost
tabHost.addTab(tab01);
tabHost.addTab(tab02);
tabHost.addTab(tab03);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

tab01Activity.java

package cn.bzu.listviewtabwidget;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

public class tab01Activity extends Activity {
private ListView listView;
private List<Map<String, ?>> data;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab01);
listView = (ListView) this.findViewById(R.id.listview);
data = getData();
SimpleAdapter adapter = new SimpleAdapter(this, data,
R.layout.tab01_item, new String[] { "photo", "name", "publish",
"content" }, new int[] { R.id.photo, R.id.name,
R.id.publish, R.id.content });
listView.setAdapter(adapter);
listView.setOnItemClickListener(new ListViewHandler());
}

private class ListViewHandler implements OnItemClickListener {

@Override
public void onItemClick(AdapterView<?> listview, View view,
int position, long id) {
Map<String, ?> item = data.get(position);
Toast.makeText(tab01Activity.this, item.get("content").toString(),
Toast.LENGTH_LONG).show();
}
}

private List<Map<String, ?>> getData() {
List<Map<String, ?>> data = new ArrayList<Map<String, ?>>();
Map<String, Object> item = new HashMap<String, Object>();
item.put("photo", R.drawable.a);
item.put("name", "念");
item.put("publish", "1分钟前");
item.put(
"content",
"杯子寂寞,被倒进开水,滚烫的感觉,这就是恋爱的感觉。水变温了,杯子很舒服,想这是生活的感觉。水变凉了,杯子害怕,也许这就是失去的感觉。水彻底的凉,杯子难受,把水倒出。杯子很舒服,杯子也掉在地上摔成一片一片的。发现每一片都有水的痕迹,它知道心里还爱着水,想再爱一次水,却不可能了。");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.b);
item.put("name", "向往美好生活");
item.put("publish", "5分钟前");
item.put(
"content",
"哭的时候没人哄,我学会了坚强;怕的时候没人陪,我学会了勇敢;烦的时候没人问,我学会了承受;累的时候没人可以依靠,我学会了自立。就这样我找到了自己,原来我很优秀,更可贵的是,世界上,只有一个我!渐渐地,我成熟了,因为没有更大的不如意,所以现在偶尔的不如意也是幸福的!");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.c);
item.put("name", "好好的");
item.put("publish", "10分钟前");
item.put("content",
"如果真的有一天,某个回不来的人消失了,某个离不开的人离开了,也没关系。时间会把最正确的人带到你的身边,在此之前,你所要做的,是好好的照顾自己。");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.d);
item.put("name", "某天");
item.put("publish", "刚刚");
item.put(
"content",
"女孩儿,你要和阳光的人在一起,和快乐的人在一起,和进取的人在一起,和大方的人在一起,和睿智的人在一起,和聪明的人在一起,借人之智,完善自己。学最好的别人,做最好的自己。");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.e);
item.put("name", "love");
item.put("publish", "15分钟前");
item.put(
"content",
"因为不想受伤,我们戴上面具,穿上武裝,设法把自己脆弱的一面藏起來。可是,我们隠藏的愈多,被发现的时候,暴露的就更多。我们愈不敢面对伤痛,被刺伤的时候,伤口就愈痛。最不容易受伤的人,不是最坚强的人,而是最坦诚面对自己的人。");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.f);
item.put("name", "美好生活");
item.put("publish", "5分钟前");
item.put(
"content",
"别人拥有的,你不必羡慕,只要努力,你也会拥有;自己拥有的,你不必炫耀,因为别人也在奋斗,也会拥有。多一点快乐,少一点烦恼,不论富或穷,地位高或低,知识浅或深。");
data.add(item);
return data;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

注意:记得在ListViewTabWidgetManifest.xml

<activity android:name=".tab01Activity"></activity>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: