Scrollview滑动到某一位置,控件的隐藏与显示
程序员文章站
2022-06-15 09:48:05
...
一 、首先我们要自定义一个Scrollview
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;
/**
* Created by Mrluper on 2018/3/12 0021.
*/
public class ObservableScrollView extends ScrollView {
public interface ScrollViewListener{
void onScrollChanged(ObservableScrollView scrollView, int x,int y,
int oldx, int oldy);
}
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);
}
}
}
二、在xml中引用
<com.zwhou.palmhospital.ui.erban.utils.ObservableScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:layout_below="@id/title">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/hosin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp">
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="13dp"
android:gravity="center_vertical"
android:layout_marginBottom="21dp"
android:layout_marginTop="17dp">
<TextView
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="@drawable/linebacg"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="体检机构"
android:textSize="15sp"
android:textColor="@color/medicalappointtextnormal"
android:layout_marginLeft="8dp"/>
</RelativeLayout>
</LinearLayout>
</com.zwhou.palmhospital.ui.erban.utils.ObservableScrollView>
三、在java代码中运用,其中hosin,getY()函数的获取,必须在控件加载完成之后,不能写在oncreate()里面,这点一定需要注意。因为demo图片涉及到公司项目就不上传了。
scrollview.setScrollViewListener(new ObservableScrollView.ScrollViewListener() {
@Override
public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
Log.e("TAG", "oldx === " + oldx);
Log.e("TAG", "x === " + x);
Log.e("TAG", "oldy === " + oldy);
Log.e("TAG", "y === " + y);
int high = (int) hosin.getY();
if (y >= high){
if (isGone){
bottom.setVisibility(View.VISIBLE);
TranslateAnimation visibleAnim = new TranslateAnimation(0, 0, 150, 0);
visibleAnim.setDuration(200);
visibleAnim.setFillAfter(true);
bottom.setAnimation(visibleAnim);
isGone = false;
}
}else if (y <= high){
if (!isGone){
bottom.setVisibility(View.GONE);
TranslateAnimation goneAnim = new TranslateAnimation(0, 0, 0, 150);
goneAnim.setDuration(200);
goneAnim.setFillAfter(true);
bottom.setAnimation(goneAnim);
isGone = true;
}
}
}
});
}