简体   繁体   English

带标签的滑动视图:需要修改

[英]Swipe Views with Tabs: Modification required

I have implemented Swipe Views with Tabs from the Android's official website: http://developer.android.com/training/implementing-navigation/lateral.html 我已经从Android的官方网站实现了带有选项卡的滑动视图: http//developer.android.com/training/implementing-navigation/lateral.html

I want to make two modifications to this code: 我想对该代码进行两个修改:

  1. The Tab bar on the top shows three different tabs. 顶部的标签栏显示三个不同的标签。 On top left; 在左上方; the previous tab, on top right; 右上角的上一个标签; the next tab and on top mid; 下一个标签,位于顶部中间; the current/selected tab. 当前/选定的标签。 I want to hide the previous and the next tabs. 我想隐藏上一个和下一个标签。 I want it to show only the current/selected tab bar. 我希望它仅显示当前/选定的标签栏。

  2. In this code, each tab has only one fragment. 在此代码中,每个选项卡只有一个片段。 I want Tab 2 to have 3 fragments lined horizontally. 我希望选项卡2具有水平排列的3个片段。 What I want is, if I swipe on from the top on the Tabs, I will go directly to the next/previous Tabs. 我想要的是,如果从选项卡的顶部滑动,我将直接转到下一个/上一个选项卡。 But if I swipe on the screen, below the Tabs, I should be able to swipe three times for tab two before I reach tab 3. To elaborate: I need horizontally scroll-able List-views in each fragment. 但是,如果我在选项卡下的屏幕上滑动,则在到达选项卡3之前,我应该可以为选项卡2滑动3次。详细说明:每个片段中都需要水平滚动的列表视图。

Is this the right direction for this? 这是正确的方向吗? Any help would be appreciated. 任何帮助,将不胜感激。

I didn't understand your first point, but about grouping several fragments under the same tab, you can do it by adding your own listeners for tabs pressed and for viewPager swipe, instead of letting the view Pager implement that logic. 我不明白您的第一点,但是关于将多个片段分组在同一个选项卡下,您可以通过添加自己的监听器(用于按下的选项卡和viewPager滑动)来实现,而不是让视图Pager实现该逻辑。 Here is an example: 这是一个例子:

//setup tabs
    mTabLayout = (TabLayout) findViewById(R.id.tabs);
    mTabLayout.addTab(mTabLayout.newTab("Prev"));
    mTabLayout.addTab(mTabLayout.newTab("Current"));
    mTabLayout.addTab(mTabLayout.newTab("Next"));

    //Listen for pressed tabs
    mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            if (mDissableTabListener) {
                return;
            }

            if (tab.getPosition() == 0) {
                //go to first position
                mViewPager.setCurrentItem(0);
            } else if (tab.getPosition() == 1){
               //i.e: go to your first tab at the center
               ,ViewPager.setCurrentItem(1);
            } else if (tab.getPosition() == 2) {
                //go to last position
                if (mViewPager.getCurrentItem() == mViewPager.getAdapter().getCount() -1) {
                    //we are in the last tab, so move to first position
                    mViewPager.setCurrentItem(0);
                }
            }
        }

        ...
    });

    //Now use a OnViewPagerListener to change selected tab when there is a swipe
    mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //avoid the TabSelectedMethod to be called (it would change the viewPager current item)
                mDissableTabListener = true;
            if (position == mViewPager.getAdapter().getCount() -1){
                //When swipe to last 
                mTabLayout.getTabAt(2).select();

            } else if (position == 0){
                //select first tab
                mTabLayout.getTabAt(0).select();

            } else {
                //select center tab
                mTabLayout.getTabAt(1).select();
            }
            mDissableTabListener = false;
        }

        ...
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM