Scrollview滑动到某一位置,控件的隐藏与显示

一 、首先我们要自定义一个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;
                    }
                }
            }
        });
    }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值