搜索條,其背景色隨著界面的上下滑動而漸變。在這里考慮到搜索條的樣式會根據(jù)UI進行設計,沒有固定的樣式,所以就直接寫在了布局文件中。文中涉及到自定義輪播圖控件ADViewPager
具體應用請閱讀:http://www.lxweimin.com/p/3564b08db10e
效果圖:
SearchBar.gif
一:原理
不透明度的范圍0~255,根據(jù)輪播圖的高度和輪播圖滑出屏幕的高度+搜索條的高度求出百分比,然后計算出不透明度值,再設置搜索條的不透明度就行了。
二:布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.github.phoenix.widget.SlidingScrollView
android:id="@+id/slidingscrollview_search"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.github.phoenix.widget.ADViewPager
android:id="@+id/ad_search_view_pager"
android:layout_width="match_parent"
android:layout_height="280dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="600dp"
android:text="搜索條"
android:gravity="center"
android:textColor="@android:color/holo_red_light"
android:textSize="22dp"/>
</LinearLayout>
</com.github.phoenix.widget.SlidingScrollView>
<!--搜索條,放在文件尾就會浮在最上層;如果放在文件首就會被SlidingScrollView蓋住-->
<RelativeLayout
android:id="@+id/rl_search_bar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:background="@android:color/holo_red_light">
<TextView
android:id="@+id/tv_search_city"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="貴陽市"
android:textColor="@android:color/darker_gray"
android:layout_alignParentLeft="true"
android:gravity="center"
android:textSize="14sp"/>
<LinearLayout
android:id="@+id/ll_search"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:layout_centerInParent="true"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:gravity="center_vertical"
android:layout_toRightOf="@+id/tv_search_city"
android:layout_toLeftOf="@+id/iv_search_message"
android:orientation="horizontal"
android:background="@drawable/shape_bg_edit">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/img_search"
android:layout_marginLeft="4dp"/>
<EditText
android:id="@+id/edt_search_info"
android:layout_width="0dp"
android:layout_height="match_parent"
android:paddingLeft="5dp"
android:textColor="#333"
android:layout_weight="1"
android:hint="搜索"
android:textSize="18sp"
android:maxLines="1"
android:background="@android:color/transparent"
android:gravity="center_vertical"/>
<ImageView
android:id="@+id/iv_clear_search_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/img_edit_clear"
android:visibility="gone"
android:layout_marginRight="8dp"/>
</LinearLayout>
<ImageView
android:id="@+id/iv_search_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/img_message"/>
</RelativeLayout>
</RelativeLayout>
三:滑動監(jiān)聽
在布局文件中我們看到了SlidingScrollView,這是繼承了ScrollView并重寫了onScrollChanged(int l, int t, int oldl, int oldt)方法。從ScrollView的源碼中我們找到了onScrollChanged這個方法,再仔細一看是包級私有的,我們無法使用,Google為什么這樣做呢?雖然不可以直接用官方的控件,但是我們可以重寫它,修改它的權限,這樣我們就可以在外部使用了。
onScrollChanged里面有4個參數(shù),l代表滑動后當前ScrollView可視界面的左上角在整個ScrollView的X軸中的位置,oldi也就是滑動前的X軸位置。同理,t也是當前可視界面的左上角在整個ScrollView的Y軸上的位置,oldt也就是移動前的Y軸位置。
public class SlidingScrollView extends ScrollView {
private OnScrollChangedListener onScrollChangedListener;
public SlidingScrollView(Context context) {
this(context, null);
}
public SlidingScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SlidingScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (onScrollChangedListener != null) {
onScrollChangedListener.onScrollChanged(l, t, oldl, oldt);
}
}
public interface OnScrollChangedListener {
void onScrollChanged(int l, int t, int oldl, int oldt);
}
/**
* 滑動監(jiān)聽,對外提供的方法
* @param onScrollChangedListener
*/
public void setOnScrollChangedListener(OnScrollChangedListener onScrollChangedListener) {
this.onScrollChangedListener = onScrollChangedListener;
}
}
四:實際應用
//在界面加載的時候,初始化搜索條背景為透明
rlSearchBar.getBackground().setAlpha(0);
scrollView.setOnScrollChangedListener(new SlidingScrollView.OnScrollChangedListener() {
@Override
public void onScrollChanged(int l, int t, int oldl, int oldt) {
int height = adViewPager.getHeight() - rlSearchBar.getHeight();
float fraction = (float) Math.min(Math.max(t, 0), height) / height;
int alpha = (int) (fraction * 255);
rlSearchBar.getBackground().setAlpha((int) (fraction * 255));
}
});
在RelativeLayout 布局文件中,搜索條放在文件末尾,并且不設置相對位置,就會浮在界面最頂端,不然會被SlidingScrollView覆蓋的。