activity_main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity">    <FrameLayout        android:layout_width="match_parent"        android:layout_height="match_parent">        <com.example.testscrollviewslide.MyScrollView            android:id="@+id/my_scrollview"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:background="#435345">            <LinearLayout                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:orientation="vertical">                <LinearLayout                    android:id="@+id/ll_tabView"                    android:layout_width="match_parent"                    android:layout_height="50dp"                    android:gravity="center"                    android:orientation="vertical">                    <LinearLayout                        android:id="@+id/tv_topView"                        android:layout_width="match_parent"                        android:layout_height="50dp"                        android:gravity="center"                        android:orientation="vertical">                        <RelativeLayout                            android:layout_width="match_parent"                            android:layout_height="match_parent"                            android:background="@android:color/white"                            android:gravity="center">                            <View                                android:layout_width="39dp"                                android:layout_height="5dp"                                android:layout_marginTop="6dp"                                android:layout_centerHorizontal="true"                                android:background="@drawable/line_back"/>                            <View                                android:layout_width="match_parent"                                android:layout_height="1dp"                                android:layout_marginTop="36dp"                                android:background="#E9EDF7"/>                        </RelativeLayout>                    </LinearLayout>                </LinearLayout>                <TextView                    android:id="@+id/tv_contentView"                    android:layout_width="match_parent"                    android:layout_height="1500dp"                    android:background="#ffffff"                    android:gravity="top|center_horizontal"                    android:paddingTop="160dp"                    android:text="优然,喝过最烈的酒,也放过不该放的手。\n人总要经历一些苦痛,才能蜕变得更优秀\n\n\n\n\n优然,喝过最烈的酒,也放过不该放的手。\n人总要经历一些苦痛,才能蜕变得更优秀\n\n\n\n\n优然,喝过最烈的酒,也放过不该放的手。\n人总要经历一些苦痛,才能蜕变得更优秀\n\n\n\n\n优然,喝过最烈的酒,也放过不该放的手。\n人总要经历一些苦痛,才能蜕变得更优秀\n\n\n\n\n优然,喝过最烈的酒,也放过不该放的手。\n人总要经历一些苦痛,才能蜕变得更优秀\n\n\n\n\n优然,喝过最烈的酒,也放过不该放的手。\n人总要经历一些苦痛,才能蜕变得更优秀\n\n\n\n\n优然,喝过最烈的酒,也放过不该放的手。\n人总要经历一些苦痛,才能蜕变得更优秀"                    android:textSize="14sp" />            </LinearLayout>        </com.example.testscrollviewslide.MyScrollView>        <LinearLayout            android:id="@+id/ll_tabTopView"            android:layout_width="match_parent"            android:layout_height="50dp"            android:orientation="vertical" />    </FrameLayout></LinearLayout>

MainActivity.java:

import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.widget.LinearLayout;public class MainActivity extends AppCompatActivity implements MyScrollView.OnScrollListener{    private LinearLayout mTopTabViewLayout;    /**     * 跟随ScrollView的TabviewLayout     */    private LinearLayout mTabViewLayout;    /**     * 要悬浮在顶部的View的子View     */    private LinearLayout mTopView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        MyScrollView mMyScrollView = (MyScrollView) findViewById(R.id.my_scrollview);        mTabViewLayout = (LinearLayout) findViewById(R.id.ll_tabView);        mTopTabViewLayout = (LinearLayout) findViewById(R.id.ll_tabTopView);        mTopView = (LinearLayout) findViewById(R.id.tv_topView);        //滑动监听        mMyScrollView.setOnScrollListener(this);    }    @Override    public void onScroll(int scrollY) {        int mHeight = mTabViewLayout.getTop();        //判断滑动距离scrollY是否大于0,因为大于0的时候就是可以滑动了,此时mTabViewLayout.getTop()才能取到值。        if (scrollY > 0 && scrollY >= mHeight) {            if (mTopView.getParent() != mTopTabViewLayout) {                mTabViewLayout.removeView(mTopView);                mTopTabViewLayout.addView(mTopView);            }        } else {            if (mTopView.getParent() != mTabViewLayout) {                mTopTabViewLayout.removeView(mTopView);                mTabViewLayout.addView(mTopView);            }        }    }}

MyScrollView.java:

import android.content.Context;import android.util.AttributeSet;import androidx.core.widget.NestedScrollView;/** * 自定义ScrollView */public class MyScrollView extends NestedScrollView {    private OnScrollListener mOnScrollListener;    public MyScrollView(Context context) {        super(context);    }    public MyScrollView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    /**     * 监听ScroView的滑动情况     *     * @param l    变化后的X轴位置     * @param t    变化后的Y轴的位置     * @param oldl 原先的X轴的位置     * @param oldt 原先的Y轴的位置     */    @Override    protected void onScrollChanged(int l, int t, int oldl, int oldt) {        super.onScrollChanged(l, t, oldl, oldt);        if (null != mOnScrollListener) {            mOnScrollListener.onScroll(t);        }    }    /**     * 设置滚动接口     *     * @param listener     */    public void setOnScrollListener(OnScrollListener listener) {        this.mOnScrollListener = listener;    }    /**     * 滚动的回调接口     */    public interface OnScrollListener {        /**         * MyScrollView滑动的Y方向距离变化时的回调方法         *         * @param scrollY         */        void onScroll(int scrollY);    }}

line_back.xml:

<?xml version="1.0" encoding="utf-8"?><shape android:shape="rectangle"    xmlns:android="http://schemas.android.com/apk/res/android">    <solid android:color="#ffc9c9c9" />    <corners android:radius="3dp" /></shape>

本文在如下文章基础上修改学习,如需开发(仿微博详情页)也可查阅此文章:
https://blog.csdn.net/JiYaRuo/article/details/86064716

更多相关文章

  1. list滑动删除item
  2. webView scroll滑动事件
  3. android 搞定标题随scrollview滑动变色
  4. Android实现歌词滑动显示
  5. Android(安卓)UI开发第六篇——仿QQ的滑动Tab
  6. android Tab和ViewPager结合的例子
  7. SeeKBarTest
  8. fragment中加载高德地图出现滑动冲突解决。
  9. Android(安卓)左边滑动菜单栏

随机推荐

  1. kivy自定义相机
  2. 任何的分类问题
  3. 经济下行,程序员年底升职加薪指南
  4. 胡忠想|微博微服务架构的Service Mesh实
  5. 不惧疫情,中国SDS市场迎来井喷行情!
  6. 人类语言的表现形式和规则
  7. 如何查询谷歌地球卫星数据源
  8. 使用语音包合成你想说的话-文字转语音
  9. arc_loss训练手写数字分类
  10. mysql5.7安装