<?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">
<com.lanmai.ObservableScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 中间就是填充的view就不写了-->
<!--指定要置顶的view-->
<TextView
android:id="@+id/specific_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_dark"
android:gravity="center"
android:text="text"
android:textSize="40sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@android:color/darker_gray"
android:gravity="center"
android:text="text"
android:textSize="40sp"/>
</LinearLayout>
</RelativeLayout>
</com.lanmai.ObservableScrollView>
<!--指定要置顶的相同的view visibility设置为gone -->
<TextView
android:id="@+id/specific_text_view_gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_dark"
android:gravity="center"
android:text="text"
android:textSize="40sp"
android:visibility="gone"/>
</RelativeLayout>
public class ObservableScrollView extends ScrollView {
private ScrollViewListener scrollViewListener = null;
public ObservableScrollView(Context context) {
super(context);
}
public ObservableScrollView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public ObservableScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setScrollViewListener(ScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
}
@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
super.onScrollChanged(x, y, oldx, oldy);
if (scrollViewListener != null) {
scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
}
}
public interface ScrollViewListener {
void onScrollChanged(ScrollView scrollView, int x, int y, int oldx, int oldy);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll_view);
mTextView = ((TextView) findViewById(R.id.specific_text_view));
mScrollView = ((ObservableScrollView) findViewById(R.id.scrollview));
mVisibleTextView = ((TextView) findViewById(R.id.specific_text_view_gone));
mTextView.setOnClickListener(this);
mScrollView.setScrollViewListener(this);
}
@Override
public void onScrollChanged(ScrollView scrollView, int x, int y, int oldx, int oldy) {
int[] location = new int[2];
mTextView.getLocationOnScreen(location);
int xPosition = location[0];
int yPosition = location[1];
Log.d("ScrollViewActivity", "yPosition:" + yPosition);
int statusBarHeight = getStatusBarHeight();
Log.d("ScrollViewActivity", "statusBarHeight:" + statusBarHeight);
if (yPosition <= statusBarHeight) {
mVisibleTextView.setVisibility(View.VISIBLE);
} else {
mVisibleTextView.setVisibility(View.GONE);
}
}
int[] location = new int[2];
mTextView.getLocationOnScreen(location);
int xPosition = location[0];
int yPosition = location[1];
/* mTextView就是要悬浮的view,getLocationOnScreen(location)这个方法就是拿到view在屏幕中的位置 ,传入一个数组,最后得到的yPosition就是view在屏幕中的高度,这里面调用了native层的实现方式,所以数组能直接附上值*/
// 值得注意的是,拿到的这个高度还包括状态栏的高度。只要减掉就可以了,状态栏的高度获取获取附上代码:
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
int statusBarHeight = getStatusBarHeight();
Log.d("ScrollViewActivity", "statusBarHeight:" + statusBarHeight);
通过获取到的状态栏高度,如果小于状态栏的高度就表示已经滑出屏幕了,将要置顶的view设置为visibvle否则设置为gone
if (yPosition <= statusBarHeight) {
mVisibleTextView.setVisibility(View.VISIBLE);
} else {
mVisibleTextView.setVisibility(View.GONE);
}
@Override
public void onClick(View v) {
int[] location = new int[2];
v.getLocationOnScreen(location);
int x = location[0];
int y = location[1];
mScrollView.scrollBy(0, location[1] - getStatusBarHeight());
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有