渐变试标题栏

这里主要的两个控件就是 Linelayout(包裹的是搜索框部分) Scrollview(包裹的滑动整体页面)

提醒(一定要用RelativeLayout布局)

设置控件在布局最上边line.bringToFront();

好了直接上代码

第一步:自定义控件

 

?
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
public class ObservableScrollView extends ScrollView {
 
     public interface ScrollViewListener {
 
         void onScrollChanged(ObservableScrollView scrollView, int x, int y,
                              int oldx, int oldy);
     }
     private ScrollViewListener scrollViewListener = null ;
 
     public ObservableScrollView(Context context) {
         super (context);
     }
 
     public ObservableScrollView(Context context, AttributeSet attrs,
                                 int defStyle) {
         super (context, attrs, defStyle);
     }
 
     public ObservableScrollView(Context context, AttributeSet attrs) {
         super (context, attrs);
     }
     public void setScrollViewListener(ScrollViewListener scrollViewListener) {
         this .scrollViewListener = scrollViewListener;
     }
     @Override
     protected void onScrollChanged( int x, int y, int oldx, int oldy) {
         super .onScrollChanged(x, y, oldx, oldy);
         if (scrollViewListener != null ) {
             scrollViewListener.onScrollChanged( this , x, y, oldx, oldy);
         }
     }
}

定义控件添加监听方法

 

第二步:布局


<?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"
    >

    <LinearLayout
        android:id="@+id/line"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="5dp"
        android:background="#fcc"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/sao_iv"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="15dp"
            android:background="@drawable/a_s" />

        <LinearLayout
            android:id="@+id/sou_sou"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_weight="1"
            android:background="@drawable/shape_search"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="周大福礼包大放送" />
        </LinearLayout>
        <ImageView
            android:id="@+id/msg_iv"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:layout_marginRight="15dp"
            android:background="@drawable/my_msg_bai" />
    </LinearLayout>

    <example.com.jddome.custom.MyScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rlv"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </android.support.v7.widget.RecyclerView>
    </example.com.jddome.custom.MyScrollView>
</RelativeLayout>
scrollview默认只有一个子空间,所以要添加一个布局进行包裹,内容自己加
第三步:Activity.class
?
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
39
40
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.LinearLayout;
 
public class MainActivity extends AppCompatActivity {
 
     private LinearLayout line;
     private ObservableScrollView scrollView;
     private int imageHeight= 300 ; //设置渐变高度,一般为导航图片高度,自己控制
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         //查找控件
         line= (LinearLayout) findViewById(R.id.line);
         scrollView= (ObservableScrollView) findViewById(R.id.scrollView);
         //搜索框在布局最上面
         line.bringToFront();
         //滑动监听
         scrollView.setScrollViewListener( new ObservableScrollView.ScrollViewListener() {
             @Override
             public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
                 if (y <= 0 ) {
                     line.setBackgroundColor(Color.argb(( int ) 0 , 227 , 29 , 26 )); //AGB由相关工具获得,或者美工提供
                 } else if (y > 0 && y <= imageHeight) {
                     float scale = ( float ) y / imageHeight;
                     float alpha = ( 255 * scale);
                     // 只是layout背景透明
                     line.setBackgroundColor(Color.argb(( int ) alpha, 227 , 29 , 26 ));
                 } else {
                     line.setBackgroundColor(Color.argb(( int ) 255 , 227 , 29 , 26 ));
                 }
             }
         });
 
     }
 
 
}

猜你喜欢

转载自blog.csdn.net/qq_42081816/article/details/80700472
今日推荐