1

这是我的问题:

我创建了一个有四个选项卡的应用程序(命名为:第 1 天、第 2 天、第 3 天和第 4 天),每个选项卡都有一个布局。问题是这个布局只能包含文本视图和字符串,但我需要在每个选项卡中插入一个更复杂的 ExpandableListView 及其自己的 java 类,我想要的结果类似于“联系人”应用程序。

我的主要活动是:

package tabs;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends FragmentActivity implements
    ActionBar.TabListener {

   CollectionPagerAdapter mCollectionPagerAdapter;

   ViewPager mViewPager;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    mCollectionPagerAdapter = new CollectionPagerAdapter(
        getSupportFragmentManager());

    // Set up action bar.
    final ActionBar actionBar = getActionBar();


    // Specify that we will be displaying tabs in the action bar.
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Set up the ViewPager, attaching the adapter and setting up a listener
    // for when the
    // user swipes between sections.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mCollectionPagerAdapter);
    mViewPager
        .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {

            actionBar.setSelectedNavigationItem(position);
            }
        });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mCollectionPagerAdapter.getCount(); i++) {

        actionBar.addTab(actionBar.newTab()
            .setText(mCollectionPagerAdapter.getPageTitle(i))
            .setTabListener(this));
    }
    }

    public void onTabUnselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    }

    public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
    }

    public void onTabReselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    }


    public class CollectionPagerAdapter extends FragmentPagerAdapter {

    final int NUM_ITEMS = 4; // number of tabs

    public CollectionPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new TabFragment();
        Bundle args = new Bundle();
        args.putInt(TabFragment.ARG_OBJECT, i);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        return NUM_ITEMS;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        String tabLabel = null;
        switch (position) {
        case 0:
        tabLabel = getString(R.string.title_section1);
        break;
        case 1:
        tabLabel = getString(R.string.title_section2);
        break;
        case 2:
        tabLabel = getString(R.string.title_section3);
        break;
        case 3:
        tabLabel = getString(R.string.title_section4);
        break;
        }

        return tabLabel;
    }
    }


    public static class TabFragment extends Fragment {

    public static final String ARG_OBJECT = "object";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        Bundle args = getArguments();
        int position = args.getInt(ARG_OBJECT);

        int tabLayout = 0;
        switch (position) {
        case 0:
        tabLayout = R.layout.tab1;
        break;
        case 1:
        tabLayout = R.layout.tab2;
        break;
        case 2:
        tabLayout = R.layout.tab3;
        break;
        case 3:
        tabLayout = R.layout.tab4;
        break;
        }

        View rootView = inflater.inflate(tabLayout, container, false);

        return rootView;
    }
    }}

主要的xml布局是

<android.support.v4.view.ViewPager 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v4.view.ViewPager>

选项卡布局是(它们都相同但文本不同,这个只是“你好”)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:gravity="center">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="one" />

</LinearLayout>
4

0 回答 0