选项卡组件和ListView组件结合模拟新浪微博

一.

mainactivity:

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.widget.TabHost;



public class MainActivity extends TabActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.main);
		TabHost tabHost=getTabHost();
		LayoutInflater.from(this).inflate(R.layout.main,
				tabHost.getTabContentView(), true);
		Intent intent=new Intent();
		intent.setClass(this, Tab01Activity.class);
		tabHost.addTab(tabHost
				.newTabSpec("tab1")
				.setIndicator("",
						getResources().getDrawable(R.drawable.p1))
				.setContent(intent));
		tabHost.addTab(tabHost
				.newTabSpec("tab2")
				.setIndicator("",
						getResources().getDrawable(R.drawable.p2))
				.setContent(R.id.tab2));
		tabHost.addTab(tabHost
				.newTabSpec("tab3")
				.setIndicator("",
						getResources().getDrawable(R.drawable.p3))
				.setContent(R.id.tab3));
    }

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

    
}


类Tab01Activity:

 

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

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
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;



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

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab1_main);
		listItem = (ListView) this.findViewById(R.id.list);
		data = getData();
		SimpleAdapter adapter = new SimpleAdapter(this, data,
				R.layout.tab1_item, new String[] { "image", "name", "publish",
						"content" }, new int[] { R.id.image, R.id.name,
						R.id.publish, R.id.content });
		listItem.setAdapter(adapter);
		listItem.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Map<String, Object> item = (Map<String, Object>) data
						.get(position);
				Toast.makeText(getApplicationContext(),
						item.get("name") + "\n\n" + item.get("content"),
						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("image", R.drawable.p1);
		item.put("name", "想想");
		item.put("publish", "1分钟前");
		item.put("content", "我正在学习安卓Listview");
		data.add(item);
		item = new HashMap<String, Object>();
		item.put("image", R.drawable.p2);
		item.put("name", "嘻嘻");
		item.put("publish", "9分钟前");
		item.put("content", "今天很高兴");
		data.add(item);
		item = new HashMap<String, Object>();
		item.put("image", R.drawable.p3);
		item.put("name", "丽丽");
		item.put("publish", "23分钟前");
		item.put("content", "今天遇见一件好玩的事情。");
		data.add(item);
		item = new HashMap<String, Object>();
		item.put("image", R.drawable.p4);
		item.put("name", "西西");
		item.put("publish", "43分钟前");
		item.put("content", "今天天气真好!");
		data.add(item);
		item = new HashMap<String, Object>();
		item.put("image", R.drawable.p5);
		item.put("name", "露露");
		item.put("publish", "1小时前");
		item.put("content", "今天真是太高兴了");
		data.add(item);
		item = new HashMap<String, Object>();
		item.put("image", R.drawable.p6);
		item.put("name", "美美");
		item.put("publish", "2小时前");
		item.put("content", "今天真是太高兴了");
		data.add(item);
		item = new HashMap<String, Object>();
		item.put("image", R.drawable.p7);
		item.put("name", "晕晕");
		item.put("publish", "3小时前");
		item.put("content", "我想告诉你,我的作业写好了");
		data.add(item);
		item = new HashMap<String, Object>();
		item.put("image", R.drawable.p8);
		item.put("name", "佳佳");
		item.put("publish", "3小时前");
		item.put("content", "今天天气真好啊");
		data.add(item);
		item = new HashMap<String, Object>();
		item.put("image", R.drawable.p9);
		item.put("name", "红红");
		item.put("publish", "4小时前");
		item.put("content", "有点不高兴了");
		data.add(item);
		item = new HashMap<String, Object>();
		item.put("image", R.drawable.p10);
		item.put("name", "强强");
		item.put("publish", "4小时前");
		item.put("content", "好好努力吧,加油哟");
		data.add(item);
		return data;
	}

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

}


二.

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/tab2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/p2"/>
    <ImageView
        android:id="@+id/tab3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/p3"/>


</RelativeLayout>


activity_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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Tab01Activity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>


tab1_item.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
     >
    <ImageView
        android:id="@+id/image"
        android:padding="10dp"
        android:layout_width="48dp"
        android:layout_height="48dp" />

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

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

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

            <TextView
                android:id="@+id/publish"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingTop="10dp"
                android:gravity="right" />
        </LinearLayout>

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

</LinearLayout>


tab1_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >
    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</LinearLayout>


 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值