Android学习--ListView和Tab

产生一个ListView 其中包含很多items,第一个item启动另一个实现了Tab的Activity。

关于tab的使用方式,参见下面blog

http://oldshark.blog.163.com/blog/static/97167342011729112640919/

http://erbo2008.iteye.com/blog/1542733

源码框架如下:

MainActivity.java:

 1 package com.example.listviewdemo;
 2 
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.app.ListActivity;
 6 import android.content.Intent;
 7 import android.view.Menu;
 8 import android.view.View;
 9 import android.widget.AdapterView.OnItemClickListener;
10 import android.widget.ArrayAdapter;
11 import android.widget.ListAdapter;
12 import android.widget.ListView;
13 import android.widget.TextView;
14 import android.widget.Toast;
15 
16 public class MainActivity extends ListActivity {
17     private TextView textView;
18     private String[] items = {"item1","item2","item3","item4","item5","item6"};
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_main);
23         textView = (TextView) findViewById(R.id.TextView);
24         setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
25                 items));
26     }
27 
28     @Override
29     protected void onListItemClick(ListView l, View v, int position, long id) {
30         // TODO Auto-generated method stub
31         super.onListItemClick(l, v, position, id);
32 
33         textView.setText(items[position]);
34         Toast.makeText(this, position+"", Toast.LENGTH_LONG).show();
35         switch (position) {
36         case 0:
37             Intent intent = new Intent();
38             intent.setClass(this, TabDemo.class);
39             startActivity(intent);
40             break;
41 
42         default:
43             break;
44         }
45 
46     }
47 
48     @Override
49     public boolean onCreateOptionsMenu(Menu menu) {
50         // Inflate the menu; this adds items to the action bar if it is present.
51         getMenuInflater().inflate(R.menu.main, menu);
52         return true;
53     }
54 
55 }

TabDemo.java:

package com.example.listviewdemo;

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

public class TabDemo extends TabActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        
        TabHost tabHost = getTabHost();
    //    LayoutInflater.from(this).inflate(R.layout.tab1, 
    //            tabHost.getTabContentView(),true);
        
        LayoutInflater.from(this).inflate(R.layout.tab2, 
                tabHost.getTabContentView(),true);
        tabHost.addTab(tabHost.newTabSpec("TAB1").setIndicator("布局1")
                .setContent(new Intent(this,Tab1.class)));
        tabHost.addTab(tabHost.newTabSpec("TAB2").setIndicator("布局2")
                .setContent(R.id.tab2));
        }
}

tab1.java

package com.example.listviewdemo;

import android.app.Activity;
import android.os.Bundle;

public class Tab1 extends Activity{
    
@Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab1);
    }
}

mainActivity.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=".MainActivity" >

    <TextView
        android:id="@+id/TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
 <!-- **********NOTICE********** -->
    <ListView 
        android:id="@android:id/list"  
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false"
        android:layout_below="@id/TextView"
        >
        
    </ListView>

</RelativeLayout>

Tab1.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="match_parent"
    android:id="@+id/tab1"
    android:orientation="vertical" >
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/app_name"/>


</LinearLayout>

Tab2.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="match_parent"
    android:id="@+id/tab2"
    android:orientation="vertical" >
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/hello_world"/>
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.listviewdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="10" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.listviewdemo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.example.listviewdemo.TabDemo"></activity>
        <activity android:name="com.example.listviewdemo.Tab1"></activity>
    </application>

</manifest>

posted on 2013-06-19 13:24  Tmacy  阅读(645)  评论(0编辑  收藏  举报

导航