android 布局中某一个View 在scroll滑动的时候吸附在顶部

来源:互联网 发布:js上传身份证图片插件 编辑:程序博客网 时间:2024/05/17 22:15
第一个要重写scroll,要提供滑动监听的接口出来:
                                                                                  
package com.dksj.suctiontopdemo;import android.content.Context;import android.util.AttributeSet;import android.widget.ScrollView;/** * 重构 ScrollView */public class AbsScrollView extends ScrollView {    private OnScrollListener onScrollListener;    public AbsScrollView(Context context) {        super(context);    }    public AbsScrollView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public AbsScrollView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    protected int computeHorizontalScrollRange() {        return super.computeHorizontalScrollRange();    }    @Override    protected void onScrollChanged(int l, int t, int oldl, int oldt) {        super.onScrollChanged(l, t, oldl, oldt);        if (onScrollListener != null) {            onScrollListener.onScroll(t);        }    }    public interface OnScrollListener {        void onScroll(int y);    }    public void setOnScrollListener(OnScrollListener onScrollListener) {        this.onScrollListener = onScrollListener;    }}


第二部,在布局中使用重构的scroll:

                                                           

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent">    <com.dksj.suctiontopdemo.AbsScrollView        android:id="@+id/mScrollView"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:scrollbars="none">        <FrameLayout            android:layout_width="match_parent"            android:layout_height="wrap_content">            <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:padding="10dp"                    android:text="Hello World!" />                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:padding="10dp"                    android:text="Hello World!" />                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:padding="10dp"                    android:text="Hello World!" />                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:padding="10dp"                    android:text="Hello World!" />                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:padding="10dp"                    android:text="Hello World!" />                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:padding="10dp"                    android:text="Hello World!" />                <TextView                    android:id="@+id/mTextView"                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:background="#ff00"                    android:padding="10dp"                    android:text="Hello World!"                    android:textColor="#fff" />                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:padding="10dp"                    android:text="Hello World!" />                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:padding="10dp"                    android:text="Hello World!" />                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:padding="10dp"                    android:text="Hello World!" />                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:padding="10dp"                    android:text="Hello World!" />                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:padding="10dp"                    android:text="Hello World!" />                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:padding="10dp"                    android:text="Hello World!" />                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:padding="10dp"                    android:text="Hello World!" />                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:padding="10dp"                    android:text="Hello World!" />                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:padding="10dp"                    android:text="Hello World!" />                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:padding="10dp"                    android:text="Hello World!" />                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:padding="10dp"                    android:text="Hello World!" />                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:padding="10dp"                    android:text="Hello World!" />                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:padding="10dp"                    android:text="Hello World!" />                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:padding="10dp"                    android:text="Hello World!" />                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:padding="10dp"                    android:text="Hello World!" />                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:padding="10dp"                    android:text="Hello World!" />                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:padding="10dp"                    android:text="Hello World!" />                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:padding="10dp"                    android:text="Hello World!" />            </LinearLayout>            <TextView                android:id="@+id/mTextViewTop"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:background="#ff00"                android:padding="10dp"                android:text="Hello World!"                android:textColor="#fff" />        </FrameLayout>    </com.dksj.suctiontopdemo.AbsScrollView></RelativeLayout>


第三部在activity中使用:

                                        

package com.dksj.suctiontopdemo;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.TextUtils;import android.view.ViewTreeObserver;import android.widget.RelativeLayout;import android.widget.TextView;public class MainActivity extends AppCompatActivity implements AbsScrollView.OnScrollListener {    private AbsScrollView absScrollView;    private RelativeLayout activity_main;    private TextView mTextView;    private TextView mTextViewTop;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        activity_main = (RelativeLayout) findViewById(R.id.activity_main);        absScrollView = (AbsScrollView) findViewById(R.id.mScrollView);        absScrollView.setOnScrollListener(this);        mTextView = (TextView) findViewById(R.id.mTextView);        mTextViewTop = (TextView) findViewById(R.id.mTextViewTop);        activity_main.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {            @Override            public void onGlobalLayout() {                onScroll(absScrollView.getScrollY());            }        });    }    @Override    public void onScroll(int y) {        int top = Math.max(y, mTextView.getTop());        mTextViewTop.layout(0, top, mTextViewTop.getWidth(), top + mTextViewTop.getHeight());    }}


0 0
原创粉丝点击