2016-08-14 122 views
1

我想實現類似於維基百科和netflix應用程序的效果,其中工具欄在細節活動中是透明的,但隨着用戶向下滾動內容而淡入淡出。工具欄的菜單項始終可見。更改滾動條上的工具欄阿爾法android

這是我的佈局文件:

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

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="?android:attr/actionBarSize"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:fitsSystemWindows="true"/> 

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



    <android.support.v4.widget.NestedScrollView 
     android:id="@+id/detail_view_container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     android:fitsSystemWindows="true"/> 

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

的活動內容使用的是片段的事務放在NestedScrollView內。

這是我的個人信息的活動:

public class DetailActivity extends SearchBaseActivity { 

    private static final String RESOURCE = "resource"; 

    @BindView(R.id.detail_view_container) 
    NestedScrollView mContainer; 
    @BindView(R.id.toolbar) 
    Toolbar mToolbar; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_detail); 
     ButterKnife.bind(this); 

     Media media = getIntent().getParcelableExtra(RESOURCE); 

     setSupportActionBar(mToolbar); 
     ActionBar actionBar = getSupportActionBar(); 
     if (actionBar != null) { 
      actionBar.setDisplayHomeAsUpEnabled(true); 
      actionBar.setTitle(null); 
     } 

    } 

    public static Intent getStartIntent(Context context, Media media) { 
     return new Intent(context, DetailActivity.class).putExtra(RESOURCE, media); 
    } 
} 

你能不能幫我算出這個,好嗎?

謝謝!

回答

1

我想通過簡單地使用CoordinatorLayout來滾動內容時,找到一種很好的方式來從半透明狀態欄到不透明狀態欄進行相同的轉換,但到目前爲止我還沒有找到它。

我做了一個非常基本的實現通過增加scrolllistener到NestedScrollView:

NestedScrollView nsv = (NestedScrollView) findViewById(R.id.detail_view_container); 
nsv.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() { 
    @Override 
    public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { 
     int color = Color.parseColor("#FF2D2D2D"); // ideally a global variable 
     if (scrollY < 256) { 
      int alpha = (scrollY << 24) | (-1 >>> 8) ; 
      color &= (alpha); 
     } 
     toolbar.setBackgroundColor(color); 
    } 
}); 

此實現將開始不透明度到工具欄上的NestedScrollView增加的Y滾動。