最新消息:文章中包含代码时,请遵守代码高亮规范!

【原创】滑动到顶部,固定顶部(一)

Android 王 伟 879浏览 0评论

开发中,遇到一个首页,需要选项滑动到顶部,并固定到顶部。
首先XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="@color/colorPrimary" />

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

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

                <View
                    android:layout_width="match_parent"
                    android:layout_height="140dp"
                    android:background="@android:color/holo_blue_dark" />

                <LinearLayout
                    android:id="@+id/ll_filter"
                    android:layout_width="match_parent"
                    android:layout_height="45dp"
                    android:background="@android:color/white"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="center"
                        android:text="固定View" />

                </LinearLayout>

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1000dp"
                    android:background="@android:color/holo_green_light" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/ll_top"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:background="@android:color/white"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="固定View" />

            </LinearLayout>

        </FrameLayout>
    </com.example.topdemo1.MyScrollView>

</LinearLayout>

其中需要一个自定义的类MyScollView,代码如下:

public class MyScrollView extends ScrollView {
    private OnScrollListener onScrollListener;

    public MyScrollView(Context context) {
        this(context, null);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setOnScrollListener(OnScrollListener onScrollListener) {
        this.onScrollListener = onScrollListener;
    }

    @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 {
        public void onScroll(int scrollY);
    }
}

然后activity的代码如下:

public class MainActivity extends AppCompatActivity implements MyScrollView.OnScrollListener {

    private LinearLayout mLlfilter;
    private LinearLayout mLltop;
    private MyScrollView myScrollView;
    private LinearLayout mLlParent;

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

    private void initView() {
        mLlfilter = (LinearLayout) findViewById(R.id.ll_filter);
        mLltop = (LinearLayout) findViewById(R.id.ll_top);
        myScrollView = (MyScrollView) findViewById(R.id.scrollView);
        mLlParent = (LinearLayout) findViewById(R.id.ll_parent);
        myScrollView.setOnScrollListener(this);
        mLlParent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                onScroll(myScrollView.getScrollY());
            }
        });
    }

    @Override
    public void onScroll(int scrollY) {
        int mTop = Math.max(scrollY, mLlfilter.getTop());
        mLltop.layout(0, mTop, mLltop.getWidth(), mTop + mLltop.getHeight());
    }
}

转载时请注明出处及相应链接,本文永久地址:https://blog.yayuanzi.com/23245.html


pay_weixin
pay_weixin
微信打赏
pay_weixin
支付宝打赏
感谢您对作者Bob的打赏,我们会更加努力!    如果您想成为作者,请点我

您必须 登录 才能发表评论!