2016-04-02 25 views
2

我想按照圖像做。所以基本上佈局架構:標題加上ViewPager使用Tablayout

  1. screen - 全區域是可滾動的。頂部是存在水平卷軸的佈局。低於該選項卡布局和查看尋呼機

    2.Screen - 當用戶向下滾動然後水平滾動的標題部分將會去(不可見)和標籤佈局將粘性標題它總是在頂部,只有他的查看尋呼機當他再次向上滾動,然後頭球頂應該來

交換。但我是裁判這個https://github.com/kmshack/Android-ParallaxHeaderViewPager但它不是按照我的需要

回答

1

試試這個庫:

https://github.com/noties/Scrollable

我認爲這是你想要的。

+0

在這可以我們隱藏工具欄時,用戶滑動和tablayout作爲粘性標題 – andro

+0

@andro你可以嘗試CoordinatorLayout如果你需要這種效果。 –

0

activity_tab_layout.xml

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


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

    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/collapse_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/material_deep_teal_500" 
     android:fitsSystemWindows="true" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

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

     <!--<android.support.v7.widget.Toolbar--> 
       <!--android:id="@+id/toolbar"--> 
       <!--android:layout_width="match_parent"--> 
       <!--android:layout_height="?attr/actionBarSize"--> 
       <!--android:background="?attr/colorPrimary"--> 
       <!--app:layout_scrollFlags="scroll|enterAlways"--> 
        <!--app:popupTheme="@style/ThemeOverlay.AppCompat.Dark">--> 

       <!--</android.support.v7.widget.Toolbar>--> 

      <HorizontalScrollView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

      </HorizontalScrollView> 


      </LinearLayout> 

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

    <android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:foregroundGravity="center_horizontal" 
     app:layout_collapseMode="pin" 
     app:tabGravity="center" 
     app:tabMode="scrollable" 
     app:tabSelectedTextColor="@color/colorAccent" 
     app:tabTextColor="@color/colorPrimary" /> 

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

<android.support.v4.view.ViewPager 
    android:id="@+id/viewpager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

ActivityCode.java

public class ActivityCode extends AppCompatActivity { 

    TabLayout tabs; 
    ViewPager viewPager; 

    Toolbar toolbar; 

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

// your find view by id code 

     toolbar.setTitle(title); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setHomeButtonEnabled(true); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     setupViewPager(viewPager); 
     tabs.setupWithViewPager(viewPager); 
    } 


    private void setupViewPager(ViewPager viewPager) { 

     ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); 

     Fragment facts = new FRAGMENT_CLASS(); 
     adapter.addFragment(facts, "Title"); 
    Fragment facts = new FRAGMENT_CLASS(); 
     adapter.addFragment(facts, "Title"); 
    Fragment facts = new FRAGMENT_CLASS(); 
     adapter.addFragment(facts, "Title"); 


     viewPager.setAdapter(adapter); 
    } 

    public class ViewPagerAdapter extends FragmentPagerAdapter { 
     private final List<Fragment> mFragmentList = new ArrayList<>(); 
     private final List<String> mFragmentTitleList = new ArrayList<>(); 

     public ViewPagerAdapter(FragmentManager manager) { 
      super(manager); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      return mFragmentList.get(position); 
     } 

     @Override 
     public int getCount() { 
      return mFragmentList.size(); 
     } 

     public void addFragment(Fragment fragment, String title) { 
      mFragmentList.add(fragment); 
      mFragmentTitleList.add(title); 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      return mFragmentTitleList.get(position); 
     } 
    } 
    } 
+0

xml不正確 – andro

+0

@andro你應該編譯appcompatv7和android設計庫。 –