2016-07-28 104 views
1

我有兩個viewpagepage(一個在摺疊工具欄內另一個選項卡下面)和一個選項卡布局,所有內部片段如下圖所示。用兩個Viewpager和一個選項卡布局滑動問題

我的問題是,如果我滑動viewpager我想另一個viewpager(自動)與標籤一起輕掃。

enter image description here

我能夠通過使用

tabLayout.setupWithViewPager(mBottomViewPager); 

刷卡底部viewpager和標籤,但不能刷卡協調佈局內的頂部片段。

我已經試過

tabLayout.setupWithViewPager(mBottomViewPager); 

但只揮筆頂視圖尋呼機和標籤。

我只能滑動一個viewpager和標籤,但不是兩個視圖頁和標籤。

我該如何解決這個問題?

我的片段佈局:

<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/main_content" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
tools:context="com.ac.srikar.coupon.LocalFragment"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    android:layout_width="match_parent" 
    android:layout_height="256dp" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/collapsing_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="208dp" 
     android:background="@color/colorPrimary" 
     app:contentScrim="?attr/colorPrimary" 
     app:expandedTitleMarginBottom="20dp" 
     app:expandedTitleMarginEnd="64dp" 
     app:expandedTitleMarginStart="48dp" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

      <Button 
       android:id="@+id/select_location_button" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="@android:color/white" 
       android:onClick="selectLocationButtonHandler" 
       android:text="Select Location" 
       android:textColor="@color/defaultButtonColor" /> 

      <android.support.v4.view.ViewPager 
       android:id="@+id/local_top_pager" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       app:layout_behavior="@string/appbar_scrolling_view_behavior" 
       app:layout_collapseMode="parallax" /> 
     </LinearLayout> 

    </android.support.design.widget.CollapsingToolbarLayout> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tab_layout" 
     android:layout_width="match_parent" 
     android:layout_height="48dp" 
     android:gravity="bottom" 
     app:tabGravity="center" 
     app:tabMode="fixed" /> 
</android.support.design.widget.AppBarLayout> 

<android.support.v4.view.ViewPager 
    android:id="@+id/local_bottom_pager" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="@dimen/fragment_child_local" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 
</android.support.design.widget.CoordinatorLayout> 

回答

0
 mViewPager = (ViewPager) findViewById(R.id.container); 
    mViewPager2 = (ViewPager) findViewById(R.id.viewpager); 
    tabLayout = (TabLayout) findViewById(R.id.tabs); 

    mViewPager.setAdapter(mSectionsPagerAdapter); 
     tabLayout.setupWithViewPager(mViewPager); 
     mViewPager2.setAdapter(mSectionsPagerAdapter1); 


    mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 

     private int mScrollState = ViewPager.SCROLL_STATE_IDLE; 

     @Override 
     public void onPageScrolled(final int position, final float positionOffset, final int positionOffsetPixels) { 
      if (mScrollState == ViewPager.SCROLL_STATE_IDLE) { 
       return; 
      } 
      mViewPager2.scrollTo(mViewPager.getScrollX(), mViewPager2.getScrollY()); 
     } 

     @Override 
     public void onPageSelected(final int position) { 

     } 

     @Override 
     public void onPageScrollStateChanged(final int state) { 
      mScrollState = state; 
      if (state == ViewPager.SCROLL_STATE_IDLE) { 
       mViewPager2.setCurrentItem(mViewPager.getCurrentItem(), false); 
      } 
     } 
    }); 

    mViewPager2.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 

     private int mScrollState = ViewPager.SCROLL_STATE_IDLE; 

     @Override 
     public void onPageScrolled(final int position, final float positionOffset, final int positionOffsetPixels) { 
      if (mScrollState == ViewPager.SCROLL_STATE_IDLE) { 
       return; 
      } 
      mViewPager.scrollTo(mViewPager2.getScrollX(), mViewPager.getScrollY()); 
      tabLayout.setScrollPosition(mViewPager2.getCurrentItem(), 0f, true); 
     } 

     @Override 
     public void onPageSelected(final int position) { 
     } 

     @Override 
     public void onPageScrollStateChanged(int state) { 
      mScrollState = state; 
      if (state == ViewPager.SCROLL_STATE_IDLE) { 

        mViewPager.setCurrentItem(mViewPager2.getCurrentItem(), false); 

      } 
     } 
    }); 

    public class SectionsPagerAdapter extends FragmentStatePagerAdapter { 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     // getItem is called to instantiate the fragment for the given page. 
     // Return a PlaceholderFragment (defined as a static inner class below). 
     return PlaceholderFragment.newInstance(position + 1); 
    } 

    @Override 
    public int getCount() { 
     // Show 7 total pages. 
     return 7; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     switch (position) { 
      case 0: 
       return "SECTION 1"; 
      case 1: 
       return "SECTION 2"; 
      case 2: 
       return "SECTION 3"; 
      case 3: 
       return "SECTION 4"; 
      case 4: 
       return "SECTION 5"; 
      case 5: 
       return "SECTION 6"; 
      case 6: 
       return "SECTION 7"; 
     } 
     return null; 
    } 
} 


    public class SectionsPagerAdapter1 extends FragmentStatePagerAdapter { 

    ArrayList<String> productList; 

    public SectionsPagerAdapter1(FragmentManager fm, ArrayList<String> productList) { 
     super(fm); 
     this.productList = productList; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     // getItem is called to instantiate the fragment for the given page. 
     // Return a PlaceholderFragment (defined as a static inner class below). 
     return PlaceholderFragment1.newInstance(position, productList); 
    } 

    @Override 
    public int getCount() { 
     // Show 7 total pages. 
     return productList.size(); 
    } 
} 
相關問題