Android扩展选项卡视图的视图

时间:2012-11-30 13:08:49

标签: android android-fragments android-tabhost actionbarsherlock

enter image description here

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

  1. 带有操作栏和操作项的活动
  2. 标签位于操作栏下方
  3. 每个标签都有内容。
  4. 我使用Tab片段和TabListeners进行编码。现在,当我运行应用程序时,我正在获取选项卡视图,但缺少视图寻呼机和指示器。我正在进行如下所示的活动。 enter image description here

    我的代码如下所示。

    TabViewActivity .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。 请帮我在哪里放置此代码。 我也实现了view pager。这是我的查看分页器代码的详细信息选项卡。

    DetailsActivity.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);
        }
    }
    

1 个答案:

答案 0 :(得分:1)

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

中的示例代码