【开源】Scrollable

Scrollable

使用说明:

将你的view包裹进u.noties.scrollable.ScrollableLayout,大致是这样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<ru.noties.scrollable.ScrollableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/scrollable_layout"
    app:scrollable_maxScroll="@dimen/header_height"> --!(1)
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
 
        <TextView
            android:layout_width="match_parent"
            android:layout_height="@dimen/header_height" --!(2)
            android:background="@color/header_background" 
            android:textColor="@color/white"
            android:textSize="30sp"
            android:text="Header"
            android:id="@+id/header"
            android:gravity="center"/>
 
        <ru.noties.scrollable.sample.TabsLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/tabs_height" --!(3)
            android:background="@color/tabs_background"
            android:id="@+id/tabs" />
 
    </LinearLayout>
 
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent" --!(4)
        android:layout_marginTop="@dimen/tabs_height" --!(5)
        android:id="@+id/view_pager" />
 
</ru.noties.scrollable.ScrollableLayout>

下面是代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
final View header = findViewById(R.id.header);
final TabsLayout tabs = findView(this, R.id.tabs);
 
mScrollableLayout = findView(this, R.id.scrollable_layout);
mScrollableLayout.setDraggableView(tabs);
final ViewPager viewPager = findView(this, R.id.view_pager);
final ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), getResources(), getFragments());
viewPager.setAdapter(adapter);
 
tabs.setViewPager(viewPager);
 
// Note this bit, it's very important
mScrollableLayout.setCanScrollVerticallyDelegate(new CanScrollVerticallyDelegate() {
    @Override
    public boolean canScrollVertically(int direction) {
        return adapter.canScrollVertically(viewPager.getCurrentItem(), direction);
    }
});
 
mScrollableLayout.setOnScrollChangedListener(new OnScrollChangedListener() {
  @Override
  public void onScrollChanged(int y, int oldY, int maxY) {
 
    // Sticky behavior
    final float tabsTranslationY;
    if (y < maxY) {
        tabsTranslationY = .0F;
    else {
        tabsTranslationY = y - maxY;
    }
 
    tabs.setTranslationY(tabsTranslationY);
 
    header.setTranslationY(y / 2);
  }
});

 

相关代码

  • android-layout-samples
  • ArcLayout
  • android-parallax-recyclerview
  • Android-PullToNextLayout

posted on 2015-05-05 11:01  wasdchenhao  阅读(252)  评论(0)    收藏  举报

导航