Actionbarsherlock +选项卡+多个片段?

我非常努力地使actionbarsherlock +选项卡+片段正常工作。

我只能将其设置为静态,我想创建一个类似android market app(滑动运动)的应用。

当您需要为内部包含多个片段的布局充气时,我会陷入困境。

在Support4demos中,我以FragmentsTabsPager为例。


MMMHUHU
浏览 463回答 3
3回答

当年话下

您需要正确的库来实现所需的库。基本上,ViewPager图书馆是您所缺少的。我的建议:1. ActionbarSherlock使用起来非常简单,我不会解释。2. ViewPagerExtensions你可以在这里找到它。下载ZIP文件并从中创建一个新项目。我只能将此设置设为静态,我想创建一个类似android market app(Swype Movement)的应用。com.astuetz.viewpager.extensions.SwipeyTabsView从该项目实施。有易于理解的示例。它确实满足您的需求。甚至还有其他选项卡样式可供选择(包括ICS附带的新“人员”选项卡)。此外,对其进行样式设置以使其与您的应用程序身份匹配非常容易。3。 FragmentPagerAdapter最后,来自支持库(v4)的该类。祝您好运,如果您需要更多帮助,请随时问我。如果您使用的是我建议的内容instantiateItem,FragmentPagerAdapter则无需覆盖。你只需要为构造函数提供a FragmentManager并调用super实现;重写getCount以返回寻呼机中的片段数,并且getItem,这将用于返回您的片段以进行创建。这是我在这里的代码中的一个示例(完整的工作示例)。这是实现寻呼机的活动的内部类:static class MyFragmentPagerAdapter extends FragmentPagerAdapter {    public MyFragmentPagerAdapter(FragmentManager fm) {        super(fm);    }    @Override    public int getCount() {        return 2;    }    @Override    public Fragment getItem(int position) {        Fragment f;        switch(position) {        case 0:            f= new ItemSalesDataFragment();            break;        case 1:            f= new DepartmentChooserFragment();            break;        default:            throw new IllegalArgumentException("not this many fragments: " + position);        }        return f;    }}如您所见,此寻呼机处理两个“页面”,左侧显示销售数据。右边的允许用户选择哪个部门。这些将在getItem方法中返回。适当的片段(您可以使用现有的任何片段,无需修改)。如预期的那样,您可以通过以下两种方式将所有这些结合在一起:1)创建一个实例化的对象MyFragmentPagerAdapter,以及2)将适配器设置为您的ViewPager对象,使其成为您刚刚实例化的类。如您所见,其getItem方法将“赋予”寻呼机所需的所有片段。例:mFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());mViewPager.setAdapter(mFragmentPagerAdapter);当然,还有其他事情要做,例如自己创建选项卡按钮并将所有这些都结合在一起进行交叉通信。我建议查看从GitHub下载的ViewPagerExtensions ZIP文件中提供的示例。这只是您评论中问题的答案。如您所见,使用该库的代码并不多。

慕斯709654

实际上,除了ABS库和支持库之外,我实际上没有其他任何问题。这是我的代码:public class ActionBarTabs extends SherlockFragmentActivity {CustomViewPager mViewPager;TabsAdapter mTabsAdapter;TextView tabCenter;TextView tabText;@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WindowManager.LayoutParams.FLAG_FULLSCREEN);&nbsp; &nbsp; mViewPager = new CustomViewPager(this);&nbsp; &nbsp; mViewPager.setId(R.id.pager);&nbsp; &nbsp; setContentView(mViewPager);&nbsp; &nbsp; ActionBar bar = getSupportActionBar();&nbsp; &nbsp; bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);&nbsp; &nbsp; bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);&nbsp; &nbsp; mTabsAdapter = new TabsAdapter(this, mViewPager);&nbsp; &nbsp; mTabsAdapter.addTab(bar.newTab().setText("Home"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ToolKitFragment.class, null);&nbsp; &nbsp; mTabsAdapter.addTab(bar.newTab().setText("FujiSan"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FujiFragment.class, null);}public static class TabsAdapter extends FragmentPagerAdapter implements&nbsp; &nbsp; &nbsp; &nbsp; ActionBar.TabListener, ViewPager.OnPageChangeListener {&nbsp; &nbsp; private final Context mContext;&nbsp; &nbsp; private final ActionBar mActionBar;&nbsp; &nbsp; private final ViewPager mViewPager;&nbsp; &nbsp; private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();&nbsp; &nbsp; static final class TabInfo {&nbsp; &nbsp; &nbsp; &nbsp; private final Class<?> clss;&nbsp; &nbsp; &nbsp; &nbsp; private final Bundle args;&nbsp; &nbsp; &nbsp; &nbsp; TabInfo(Class<?> _class, Bundle _args) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clss = _class;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; args = _args;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {&nbsp; &nbsp; &nbsp; &nbsp; super(activity.getSupportFragmentManager());&nbsp; &nbsp; &nbsp; &nbsp; mContext = activity;&nbsp; &nbsp; &nbsp; &nbsp; mActionBar = activity.getSupportActionBar();&nbsp; &nbsp; &nbsp; &nbsp; mViewPager = pager;&nbsp; &nbsp; &nbsp; &nbsp; mViewPager.setAdapter(this);&nbsp; &nbsp; &nbsp; &nbsp; mViewPager.setOnPageChangeListener(this);&nbsp; &nbsp; }&nbsp; &nbsp; public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {&nbsp; &nbsp; &nbsp; &nbsp; TabInfo info = new TabInfo(clss, args);&nbsp; &nbsp; &nbsp; &nbsp; tab.setTag(info);&nbsp; &nbsp; &nbsp; &nbsp; tab.setTabListener(this);&nbsp; &nbsp; &nbsp; &nbsp; mTabs.add(info);&nbsp; &nbsp; &nbsp; &nbsp; mActionBar.addTab(tab);&nbsp; &nbsp; &nbsp; &nbsp; notifyDataSetChanged();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int getCount() {&nbsp; &nbsp; &nbsp; &nbsp; return mTabs.size();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public Fragment getItem(int position) {&nbsp; &nbsp; &nbsp; &nbsp; TabInfo info = mTabs.get(position);&nbsp; &nbsp; &nbsp; &nbsp; return Fragment.instantiate(mContext, info.clss.getName(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info.args);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onPageScrolled(int position, float positionOffset,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int positionOffsetPixels) {&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onPageSelected(int position) {&nbsp; &nbsp; &nbsp; &nbsp; mActionBar.setSelectedNavigationItem(position);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onPageScrollStateChanged(int state) {&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onTabSelected(Tab tab, FragmentTransaction ft) {&nbsp; &nbsp; &nbsp; &nbsp; Object tag = tab.getTag();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < mTabs.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (mTabs.get(i) == tag) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mViewPager.setCurrentItem(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onTabUnselected(Tab tab, FragmentTransaction ft) {&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onTabReselected(Tab tab, FragmentTransaction ft) {&nbsp; &nbsp; }}}您可以根据以下内容加载选项卡内容。mTabsAdapter.addTab(bar.newTab().setText("Home"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; YOURFRAGMENTHERE.class, null);&nbsp;&nbsp;如您所指,这将为您带来ABS,支持库和片段的可爱“滑动选项卡”效果。ABS确实使这几乎与处理本机库相同。实际上,我直接从Google的分页选项卡示例中复制了此代码,并为ABS进行了少许修改。希望这可以帮助!
打开App,查看更多内容
随时随地看视频慕课网APP