0

在此处输入图像描述

我想在我的应用程序中实现此功能,如下所示。

  1. 带有操作栏和操作项的活动
  2. 操作栏下方的选项卡
  3. 每个选项卡都会有内容。

我使用 Tab 片段和 TabListeners 进行编码。现在,当我运行该应用程序时,我得到了选项卡视图,但缺少视图寻呼机和指示器。我得到了如下所示的活动。 在此处输入图像描述

我的代码如下所示。

选项卡视图活动.java

public class TabViewActivity extends SherlockFragmentActivity {
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            copyPrivateRawResuorceToPubliclyAccessibleFile();

            ActionBar actionBar = getSupportActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            // add tabs
            Tab tab1 = actionBar
                    .newTab()
                    .setText("Details")
                    .setTabListener(
                            new TabListener<TabFragment>(this, "tab1",
                                    TabFragment.class));
            actionBar.addTab(tab1);

            Tab tab2 = actionBar
                    .newTab()
                    .setText("Maps")
                    .setTabListener(
                            new TabListener<TabFragment>(this, "tab2",
                                    TabFragment.class));
            actionBar.addTab(tab2);

            // check if there is a saved state to select active tab
            if (savedInstanceState != null) {
                getSupportActionBar().setSelectedNavigationItem(
                        savedInstanceState.getInt(ACTIVE_TAB));
            }
        }

TabListener.java

public class TabListener<T extends SherlockFragment> implements
        ActionBar.TabListener {
    private SherlockFragment mFragment;
    private final SherlockFragmentActivity mActivity;
    private final String mTag;
    private final Class<T> mClass;

    public TabListener(Activity activity, String tag, Class<T> clz) {
        mActivity = (SherlockFragmentActivity) activity;
        mTag = tag;
        mClass = clz;
    }


    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // Check if the fragment has already been initialised
        SherlockFragment preInitializedFragment = (SherlockFragment) mActivity
                .getSupportFragmentManager().findFragmentByTag(mTag);
        if (mFragment != null) {
            // If it exists, simply attach it in order to show it
            ft.attach(mFragment);
        } else if (preInitializedFragment != null) {
            mFragment = preInitializedFragment;
            ft.attach(mFragment);
        } else {
            // Not found, so instantiate and add it to the activity
            mFragment = (SherlockFragment) SherlockFragment.instantiate(
                    mActivity, mClass.getName());
            ft.add(android.R.id.content, mFragment, mTag);
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            // Detach the fragment, because another one is being attached
            ft.detach(mFragment);
        }
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // User selected the already selected tab. Usually do nothing.
    }

TabFragment.java

public class TabFragment extends SherlockFragment {
      // your member variables here
      @Override
      public View onCreateView(LayoutInflater inflater, 
                     ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.main, container, false);
        // do your view initialization here
        return view;
      }

    }

对于每个选项卡单击,我有两个不同的类,名为 details.java 和 maps.java。请帮我把这段代码放在哪里。我还实现了视图寻呼机。这是我查看详细信息选项卡的寻呼机代码。

详细信息Activity.java

public class DetailsActivity extends BaseSampleActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.place_details_layout);

        mAdapter = new TestFragmentAdapter(getSupportFragmentManager());

        mPager = (ViewPager)findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        mIndicator = (CirclePageIndicator)findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);


    }
}

BaseSampleActivity.java

public abstract class BaseSampleActivity extends FragmentActivity {
    private static final Random RANDOM = new Random();

    TestFragmentAdapter mAdapter;
    ViewPager mPager;
    PageIndicator mIndicator;
    //FragmentTabHost mTabHost;


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.random:
                final int page = RANDOM.nextInt(mAdapter.getCount());
                Toast.makeText(this, "Changing to page " + page, Toast.LENGTH_SHORT);
                mPager.setCurrentItem(page);
                return true;

            case R.id.add_page:
                if (mAdapter.getCount() < 10) {
                    mAdapter.setCount(mAdapter.getCount() + 1);
                    mIndicator.notifyDataSetChanged();
                }
                return true;

            case R.id.remove_page:
                if (mAdapter.getCount() > 1) {
                    mAdapter.setCount(mAdapter.getCount() - 1);
                    mIndicator.notifyDataSetChanged();
                }
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
4

1 回答 1

1

我没有看到任何代码ViewPager,请查看 Jake Wharton 的FragmentTabsPager中的示例代码

于 2012-12-01T05:40:02.420 回答