<?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