不知從某某時間開始,這樣的效果開始在UI設計中流行起來了。讓我們先來看看效果:
大家在支付寶、美團等非常多App中都有使用。要實現這個效果,我們能夠來分析下思路:
我們肯定要用2個一樣的布局來顯示我們的粘至布局。一個是正常的、還有一個是到頂部不動的。正常的那個,隨著scroll一起滾,該滾到哪滾到哪。僅僅是他滾到最上面的時候,
我們須要用粘至的布局,放到頂部。當然。他還在后面繼續滾,ok。如今我們來看看詳細怎樣實現:
先看布局,just a demo。用幾張圖片略微做做樣子。
粘至布局:
<?
xml version="1.0" encoding="UTF-8"?
> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="50dp" android:orientation="horizontal" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:textColor="#FFFFFF" android:background="#232323" android:text="我不會動" android:textSize="30dp" /> </LinearLayout>
主布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/parent_layout"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><com.xys.scrolltrick.TrickScrollandroid:id="@+id/scrollView"android:layout_width="fill_parent"android:layout_height="fill_parent" ><FrameLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical" ><ImageViewandroid:id="@+id/iamge"android:layout_width="match_parent"android:layout_height="200dp"android:background="@drawable/ic_launcher"android:scaleType="centerCrop" /><includeandroid:id="@+id/stick"layout="@layout/stick_layout" /><ImageViewandroid:layout_width="match_parent"android:layout_height="200dp"android:background="@drawable/ic_launcher"android:scaleType="centerCrop" /><ImageViewandroid:layout_width="match_parent"android:layout_height="200dp"android:background="@drawable/ic_launcher"android:scaleType="centerCrop" /><ImageViewandroid:layout_width="match_parent"android:layout_height="200dp"android:background="@drawable/ic_launcher"android:scaleType="centerCrop" /></LinearLayout><includeandroid:id="@+id/normal"layout="@layout/stick_layout" /></FrameLayout></com.xys.scrolltrick.TrickScroll></LinearLayout>
加入多個imageview是為了讓他能滾起來
因為ScrollView中并沒有提供scroll listener,因此我們僅僅能重寫下,來創建一個接口:
package com.xys.scrolltrick;import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;public class TrickScroll extends ScrollView {public interface onScrollListener {public void onScroll(int scrollY);}private onScrollListener onScrollListener;public TrickScroll(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}public TrickScroll(Context context, AttributeSet attrs) {super(context, attrs);}public TrickScroll(Context context) {super(context);}public void setOnScrollListener(onScrollListener onsScrollListener) {this.onScrollListener = onsScrollListener;}@Overrideprotected void onScrollChanged(int l, int t, int oldl, int oldt) {super.onScrollChanged(l, t, oldl, oldt);if (onScrollListener != null) {onScrollListener.onScroll(t);}}
}
我們給他的滑動。提供一個監聽接口。
主程序:
package com.xys.scrolltrick;import android.app.Activity;
import android.os.Bundle;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.LinearLayout;import com.xys.scrolltrick.TrickScroll.onScrollListener;public class MainActivity extends Activity implements onScrollListener {private TrickScroll mScroll;// 正常狀態下的布局private LinearLayout mLayout1;// 頂部粘至的布局private LinearLayout mLayout2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mScroll = (TrickScroll) findViewById(R.id.scrollView);mLayout1 = (LinearLayout) findViewById(R.id.stick);mLayout2 = (LinearLayout) findViewById(R.id.normal);mScroll.setOnScrollListener(this);// 根布局狀態下。監聽布局改變findViewById(R.id.parent_layout).getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() {onScroll(mScroll.getScrollY());}});}@Overridepublic void onScroll(int scrollY) {// 獲取正常布局的位置來又一次設置粘至布局的位置int layoutTop = Math.max(scrollY, mLayout1.getTop());mLayout2.layout(0, layoutTop, mLayout2.getWidth(),layoutTop + mLayout2.getHeight());}
}
當中的核心就在onScroll(int scrollY)這種方法中。我們用max函數來推斷當前最大值
這里須要注意2個方法:
1、getTop():該方法返回該view到容器的top像素
2、getScrollY():該方法返回的是。你的scrollview已經滾了多少
-------------------一旦這個世界有了scroll整個世界就不一樣了-------------------
知道了這2個方法后。我們就能夠推斷了,當滾的距離小于getTop()的時候。保持與正常的一樣,大于的時候,就須要用getScrollY()了,這個比較難理解,事實上你能夠把布局想象成一個紙帶,而手機屏幕是一個挖了孔的框,我們在后面從下往上拉紙帶,這樣就模擬了scroll滑動,這樣理解的話,會比較清楚點
以上。