【问题标题】:Navigation Type (Fixed Tabs + Swipe)导航类型(固定标签+滑动)
【发布时间】:2013-05-02 19:44:33
【问题描述】:

在我的应用程序中,我有一个 tabhost,它带有一个带有多个选项卡的 tabwidget。 现在我需要一个选项卡,它在选项卡内容内向我显示一个时间表网格,允许向右和向左滑动以在几个月中移动。但我需要标签保持固定,只有时间表滑动。

导航类型(固定标签+滑动)允许我这样做吗?据我了解,此导航允许滑动,但标签不会保持不变。

我需要的是可能的吗? 感谢您的帮助和关注。

【问题讨论】:

    标签: android android-viewpager android-tabhost swipe


    【解决方案1】:

    我会说可能,请在 tabhost 和 tabwidget 上保留您的代码。

    所以我假设其中一个选项卡正在调用一个可能名为 Schedule.class 的 Activity,默认情况下,tabhost 不允许任何滑动来更改选项卡功能,这很好。

    因此,在您的 Schedule Activity 中,您将使用 ViewPager,我从这篇文章中学会了如何使用它:http://thepseudocoder.wordpress.com/2011/10/05/android-page-swiping-using-viewpager/

    这很容易理解。您可以尝试使用它,希望我回答了您的问题

    更新:这是一个示例

    Schedule.class

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.schedule);
    
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
    
        List<Fragment> fragments = new ArrayList<Fragment>();
        fragments.add(Fragment.instantiate(this, Fragment1.class.getName()));
        fragments.add(Fragment.instantiate(this, Fragment2.class.getName()));
        fragments.add(Fragment.instantiate(this, Fragment3.class.getName()));
    
        MyFragmentAdapter miscFragmentAdapter = new MyFragmentAdapter(getSupportFragmentManager(), fragments);
    
        viewPager.setAdapter(miscFragmentAdapter);
    }
    

    MyFragmentAdapter.class

    public class MyFragmentAdapter extends FragmentPagerAdapter {
    
        private List<Fragment> fragments;
    
        public MiscFragmentAdapter(FragmentManager fragmentManager, List<Fragment> fragments) {
            super(fragmentManager);
            this.fragments = fragments;
        }
    
        @Override
        public Fragment getItem(int position) {
            return this.fragments.get(position);
        }
    
        @Override
        public int getCount() {
            return this.fragments.size();
        }
    }
    

    schedule.xml

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

    Fragment1.class 或 Fragment2.class 或 Fragment3.class

    public class Fragment1 extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            if (container == null) {
                // We have different layouts, and in one of them this
                // fragment's containing frame doesn't exist.  The fragment
                // may still be created from its saved state, but there is
                // no reason to try to create its view hierarchy because it
                // won't be displayed.  Note this is not needed -- we could
                // just run the code below, where we would create and return
                // the view hierarchy; it would just never be used.
                return null;
            }
            return (LinearLayout) inflater.inflate(R.layout.fragment1, container, false);
        }
    }
    

    fragment1 是一个简单的布局,里面有你想要的任何东西。

    【讨论】:

    • 你好 Chor。感谢您的回复。在选项卡上调用 Schedule.class(FragmentActivity) 并且此 FragmentActivity 负责滑动。但是可以将一个 fragmentActivity 放在一个由每个选项卡的 tabcontent 负责的 fragmentLayout 中吗?谢谢
    • 我不太明白你在 fragmentLayout 上的观点,你在你的项目中使用了吗?但据我了解,您使用什么父布局并不重要。你可以尝试通过在你的项目中实现上面的代码来开始测试,看看结果如何。
    猜你喜欢
    • 1970-01-01
    • 2014-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多