FadeActionBar仿美團(tuán)上拉背景漸變
開發(fā)者使用此依賴,只需要2個方法就可以實(shí)現(xiàn)美團(tuán)外賣中上拉titlebar背景漸變,下拉titlebar隱藏效果。
效果圖:
準(zhǔn)備工作:
1.注意actionbar的依賴庫,目前僅支持
import android.app.ActionBar;
2.actionbar背景漸變需要監(jiān)聽headerview的位置,放在添加headerview后調(diào)用該方法
private void initScroll() {
//設(shè)置動態(tài)改變
mlistview.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// [0]代表x坐標(biāo),location [1] 代表y坐標(biāo)。
int[] location = new int[2];
// 實(shí)時設(shè)置actionbar透明度,監(jiān)聽header位置(必須是移除屏幕會產(chǎn)生負(fù)數(shù)的view)
llheaderview.getLocationInWindow(location);
helper.setActionBarAlpha(location[1] - XMSettings.getStatusBarHeight(mContext));
Log.i("tag", "onScroll: " + (location[1] - XMSettings.getStatusBarHeight(mContext)));
}
});
}
3.注意activity對應(yīng)的theme添加屬性
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar"> <item name="android:windowActionBarOverlay">true</item> <item name="android:windowContentOverlay">@null</item> </style>
使用
1.添加Gradle依賴
dependencies {
compile 'com.github.ximencx.fadeactionbar:library:1.0.1'
}
2.activity中獲取actionbar對象,初始化XMFadeBarHelper類
private void initbar() {
//獲取actionbar對象
mActionBar = getActionBar();
mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
mActionBar.setCustomView(R.layout.ab_title);
/**
* actionbar輔助類
* parameter1:action對象
* parameter2:acitonbar背景
* parameter3:初始透明度
*/
helper = new XMFadeBarHelper(mActionBar, getResources().getDrawable(R.drawable.bg_actionbar), 0) {
/**
* 設(shè)置需要隱藏view的透明度
* 注意:是否設(shè)置background的區(qū)別
*
* @param customView actionbar布局對象
* @param alpha 回調(diào)的alpha
*/
@Override
public void setViewAlpha(View customView, int alpha) {
ButterKnife.findById(customView, R.id.tv_info).setAlpha(alpha);
ButterKnife.findById(customView, R.id.rl_bg).getBackground().setAlpha(alpha);
}
/**
* 設(shè)置隱藏速度
* 默認(rèn)返回actionbar布局的高度,當(dāng)然也可以以其它view為參照物
* @param customView actionbar布局
* @return
*/
@Override
public int setHeight(View customView) {
return customView.getHeight();
}
};
}
3.listview的監(jiān)聽中調(diào)用helper.setActionBarAlpha(),注意減去狀態(tài)欄的高度
private void initScroll() {
//設(shè)置動態(tài)改變
mlistview.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// [0]代表x坐標(biāo),location [1] 代表y坐標(biāo)。
int[] location = new int[2];
// 實(shí)時設(shè)置actionbar透明度,監(jiān)聽header位置(必須是移除屏幕會產(chǎn)生負(fù)數(shù)的view)
llheaderview.getLocationInWindow(location);
helper.setActionBarAlpha(location[1] - XMSettings.getStatusBarHeight(mContext));
Log.i("tag", "onScroll: " + (location[1] - XMSettings.getStatusBarHeight(mContext)));
}
});
}
sample中使用到的第三方庫:
評論
圖片
表情
