ANDROID笔记: ActionBar(一)之ActionBar Tab

主类:

package com.example.android_actionbar;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;

public class ActionBarTabActivity extends Activity implements
        ActionBar.TabListener {
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment);
        ActionBar actionBar = getActionBar();
        // 显示HOME
        actionBar.setDisplayShowHomeEnabled(false);
        // 显示HOME按键
        actionBar.setHomeButtonEnabled(false);
        // 显示APP名称
        actionBar.setDisplayShowTitleEnabled(false);
        // 设置mode
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.addTab(actionBar.newTab().setText("老师").setTabListener(this),
                0, true);
        actionBar.addTab(actionBar.newTab().setText("学生").setTabListener(this),
                1, false);
        actionBar.addTab(actionBar.newTab().setText("家长").setTabListener(this),
                2, false);

    }

    @Override
    public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
        
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction transaction) {

        Bundle bundle = new Bundle();
        bundle.putString("data", tab.getText().toString());
        PersonFragment personFragment = new PersonFragment();
        personFragment.setArguments(bundle);
        transaction.replace(R.id.frame, personFragment);

    }

    @Override
    public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {

    }
}

PersonFragment类:

package com.example.android_actionbar;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.android_actionbar.pojo.People;

/**
 * 碎片
 * 
 * @author Administrator
 * 
 */
public class PersonFragment extends Fragment {
    private List<People> list = new ArrayList<People>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        System.out.println("onCreateView");
        View view = inflater.inflate(R.layout.personlist, null);
        ListView listView = (ListView) view.findViewById(R.id.list_person);
        // 得到传递的数据
        String data = getArguments().getString("data");

        list.clear();
        if (data.equals("学生")) {
            list.add(new People("学生1", "男"));
            list.add(new People("学生2", "男"));
            list.add(new People("学生3", "女"));
        } else if (data.equals("老师")) {
            list.add(new People("老师1", "男"));
            list.add(new People("老师2", "女"));
            list.add(new People("老师3", "男"));
        } else if (data.equals("家长")) {
            list.add(new People("家长1", "女"));
            list.add(new People("家长2", "女"));
            list.add(new People("家长3", "男"));
        }
        listView.setAdapter(baseAdapter);
        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        System.out.println("onAttach");
    }

    private BaseAdapter baseAdapter = new BaseAdapter() {

        @Override
        public View getView(int arg0, View convertView, ViewGroup arg2) {
            LayoutInflater inflater = LayoutInflater.from(getActivity());
            View view = inflater.inflate(R.layout.personlist_item, null);
            TextView textView = (TextView) view.findViewById(R.id.itemtextname);
            TextView textView2 = (TextView) view.findViewById(R.id.itemtextsex);
            // 得到数据
            People people = list.get(arg0);
            textView.setText(people.getName());
            textView2.setText(people.getSex());
            return view;
        }

        @Override
        public long getItemId(int arg0) {
            return arg0;
        }

        @Override
        public Object getItem(int arg0) {
            return list.get(arg0);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    };
}

主类的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</FrameLayout>

fragment的布局文件:

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

    <ListView
        android:id="@+id/list_person"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</LinearLayout>

fragment的list_item文件:

<?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:orientation="horizontal" >

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

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

</LinearLayout>

效果:

 

PS:使用actionbar的TAB时需要设置:

// 显示HOME
actionBar.setDisplayShowHomeEnabled(false);
// 显示HOME按键
actionBar.setHomeButtonEnabled(false);
// 显示APP名称
actionBar.setDisplayShowTitleEnabled(false);

以上三个使activity自身的bar消失.

// 设置mode
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

posted @ 2013-11-06 17:18  skyko  阅读(388)  评论(0编辑  收藏  举报