0

我有一个CollapsingToolbarLayout及以下,我有一个LinearLayout与2 fragmentsViewPager。但是,当我滚动时,两个fragments都保持粘性,只有包含RecyclerViewviewpager滚动正确。我怎样才能使fragmentsscrollable以及?ViewPager上方的片段不滚动

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

    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/collapsing_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed" 
     android:fitsSystemWindows="true" 
     app:contentScrim="?attr/colorPrimary" 
     app:expandedTitleMarginBottom="90dp" 
     app:expandedTitleMarginEnd="64dp"> 

     <ImageView 
      android:id="@+id/hero" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scaleType="centerCrop" 
      android:fitsSystemWindows="true" 
      app:layout_collapseMode="parallax"/> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:layout_gravity="top" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
      app:layout_collapseMode="pin" /> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tab_layout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      style="@style/MyCustomTabLayout" 
      android:layout_gravity="bottom" 
      app:layout_collapseMode="pin" 
      app:tabMode="scrollable"/> 

     <LinearLayout android:id="@+id/ButtonBar" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_gravity="center|start" 
      app:layout_collapseMode="pin" 
      android:fitsSystemWindows="true" 
      android:layout_marginTop="40dp" 
      android:layout_marginLeft="24dp" 
      android:padding="@dimen/activity_horizontal_margin_half"> 

      <TextView 
       android:id="@+id/SignUpButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Sign Up" 
       android:textAllCaps="true" 
       android:background="@drawable/rounded_border_white_inverted_selector" 
       android:textColor="@drawable/rounded_border_white_text_inverted_selector" 
       android:padding="@dimen/activity_horizontal_margin_half"/> 

      <TextView 
       android:id="@+id/LoginButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Login" 
       android:textAllCaps="true" 
       android:layout_marginLeft="@dimen/activity_horizontal_margin_half" 
       android:background="@drawable/rounded_border_white_selector" 
       android:textColor="@drawable/rounded_border_white_text_selector" 
       android:padding="@dimen/activity_horizontal_margin_half"/> 

     </LinearLayout> 

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

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

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <fragment 
     android:id="@+id/fragmentSponsored" 
     android:name="SponsoredFragment" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     tools:layout="@layout/fragment_sponsored" /> 

    <fragment 
     android:id="@+id/fragmentAnnouncement" 
     android:name="AnnouncementFragment" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     tools:layout="@layout/fragment_announcement" /> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

</LinearLayout> 

回答

0

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/mainactivity" 
    android:orientation="vertical"> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/viewPager" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

</LinearLayout> 

MainActivity

public class MainActivity extends AppCompatActivity { 

    MyPagerAdapter TabAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     final ViewPager pager = (ViewPager) findViewById(R.id.viewPager); 
     pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager())); 
     pager.setOffscreenPageLimit(2); 

    private class MyPagerAdapter extends FragmentStatePagerAdapter { 

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

     @Override 
     public Fragment getItem(int pos) { 
      switch (pos) { 

       case 0: 
        return SHGCreationFragmentFirst.newInstance("SHGCreationFragmentFirst, Instance 1"); 
       case 1: 
        return MemberListFragment.newInstance("MemberListFragment, Instance 2"); 
      } 
      return null; 
     } 

     @Override 
     public int getItemPosition(Object object) { 
      return POSITION_NONE; 
     } 


     @Override 
     public int getCount() { 
      return 2; 
     } 

    } 
} 

FragmentFirst

public class FragmentFirst extends Fragment { 



     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      View v = inflater.inflate(R.layout.fragment_first, container, false); 

     return v; 
    } 

    public static FragmentFirst newInstance(String text) { 

     FragmentFirst f = new FragmentFirst(); 
     Bundle b = new Bundle(); 
     b.putString("msg", text); 

     f.setArguments(b); 

     return f; 
    } 


} 

FragmentSecond

public class FragmentSecond extends Fragment { 



      @Override 
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
       View v = inflater.inflate(R.layout.fragment_second, container, false); 

      return v; 
     } 

     public static FragmentSecond newInstance(String text) { 

      FragmentSecond f = new FragmentSecond(); 
      Bundle b = new Bundle(); 
      b.putString("msg", text); 

      f.setArguments(b); 

      return f; 
     } 


    } 

fragment_first.xml

<?xml version="1.0" encoding="utf-8"?> 


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#000000" 
      android:orientation="vertical" 
      android:padding="10dp"> 


     </LinearLayout> 

fragment_Second.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#fcfcfc" 
      android:orientation="vertical" 
      android:padding="10dp"> 


     </LinearLayout> 
+0

谢谢您的回答,但这并不解决问题。通过滚动我的意思是上下,而不是左右滑动。当我滚动ViewPager中包含的RecyclerView时,ViewPager顶部的两个片段将粘贴到顶部。我想要的是让它们也可以滚动。 – pindleskin