ScrollView 實現子視圖滑動到頂部時固定不動

我們現在看看效果

(製作GIF圖片太麻煩 大笑


這就是實現之後的效果,中間的那塊視圖當ScrollView滑動到頂部的時候就會一直留在頂部,直到滑動下來的時候纔會繼續跟着滑動。

這裏就需要自定義ScrollView

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ScrollView;

/**
 * Created by liaoyi on 2016/10/10 0010.
 */

public class MScrollView extends ScrollView {

    View v1;
    View v2;

    public MScrollView(Context context) {
        super(context);
        init();
    }

    public MScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        init();
    }

    private void init() {
        v2 = findViewById(R.id.Weekend2);
    }

    public void setV1(View v1) {
        this.v1 = v1;
    }

    @Override
    public void computeScroll() {
        if (getScrollY() >= v2.getTop()) {
            v1.setVisibility(View.VISIBLE);
        } else {
            v1.setVisibility(View.GONE);
        }
        super.computeScroll();
    }
}

其實從這裏就可以看得出實現的原理,就是去重寫ScrollView 的 computeScroll()方法 監聽滑動,然後去判斷你想要的佈局是否已經到了頂部,如果到了,其實我最開始就寫了兩個一模一樣的佈局一個放在屏幕的最上方只不過一直是隱藏的,這個時候就需要把它顯示出來就可以了,是不是特別簡單生氣


接下來就看看我的xml佈局吧 (其實只要理解了上面我所說的 下面的就不需要看了)

<?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"
    android:background="#ffffff">

    <com.example.vsat.test.MScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:gravity="center"
                android:text="星期一" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:gravity="center"
                android:text="星期二" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:gravity="center"
                android:text="星期三" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:gravity="center"
                android:text="星期四" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:gravity="center"
                android:text="星期五" />

            <TextView
                android:id="@+id/Weekend2"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:background="#90C451"
                android:gravity="center"
                android:text="喂!你醒醒!再堅持一下,馬上就週末了" />

            <com.example.vsat.test.MListView
                android:id="@+id/listView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>

    </com.example.vsat.test.MScrollView>

    <TextView
        android:id="@+id/Weekend1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#90C451"
        android:gravity="center"
        android:text="喂!你醒醒!再堅持一下,馬上就週末了"
        android:visibility="gone" />
</RelativeLayout>

然後就是Activity

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;

import java.util.ArrayList;

public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MScrollView scrollView = (MScrollView) findViewById(R.id.scrollView);
        MListView listView = (MListView) findViewById(R.id.listView);
        View v = findViewById(R.id.Weekend1);
        scrollView.setV1(v);
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add("第" + i + "號機器人");
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this,
                android.R.layout.simple_expandable_list_item_1,
                list);
        listView.setAdapter(adapter);
        listView.setFocusable(false);
    }

}
是不是有點太簡單了,不過這樣最實用不是嗎!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章