Majialiang131 2019-03-25 15:44 采纳率: 0%
浏览 1138
已结题

如何实现ScrollView上滑顶部布局被覆盖,下滑顶部布局显示,顶部布局有两个点击事件

ScrollView上滑顶部布局被覆盖,下滑顶部布局显示,顶部布局有两个点击事件

  • 写回答

1条回答 默认 最新

  • MaizyLau 2019-03-25 16:53
    关注

     1. 顶部1,顶部2布局需要完全一样,初始化时顶部1不显示,为gone。

    2.  当顶部2向上滑动到屏幕顶端时,设置顶部1可见,由于顶部1不包含在ScrollVIew里面,所以会一直存在,不会被滑动;

     而向下滑时,当顶部2到达屏幕顶端,隐藏顶部1,显示顶部2即可。

    所以实现关键步骤是:

    判断顶部2是否滑动到了顶部,也就是标题栏之下。

    先看布局,很简单:

    布局和原理里面一致:顶部1,ScrollView 就没了。

    只是有一个自定义的ScrollView,这个是重点,在代码里面讲,布局也没什么。

    记得顶部1不在ScrollView里面,二顶部2在ScrollView里面哦~

    <tao.demo.testscrollview.view.MyScrollView
    android:id="@+id/myscroview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
    
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:scaleType="centerCrop"
            android:src="@mipmap/ic_launcher" />
    
        <RelativeLayout
            android:id="@+id/rl2"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#66ff0000">
            <TextView
                android:textSize="20sp"
                android:textColor="#ffffff"
                android:gravity="center"
                android:text="滑动到顶部悬浮"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </RelativeLayout>
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/mrecycle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
    </LinearLayout>
    

    <RelativeLayout
    android:id="@+id/rl1"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="#aa00ff00"
    android:visibility="gone"

    <TextView
        android:textSize="20sp"
        android:textColor="#ffffff"
        android:gravity="center"
        android:text="滑动到顶部悬浮"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    

    接下来就看自定义的ScrollView:

        这里面关键的就是onScrollChanged这个方法,实现了ScrollView滑动距离的监听。

    public class MyScrollView extends ScrollView {

    private static StopCall stopCall;
    
    //ScrollView向上滑动到顶部的距离
    private int upH;
    
    public MyScrollView(Context context) {
        super(context);
    }
    
    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    
        //赋值:200很重要,这个值是顶部2上面的高度
        upH = dpTopx(200);//单位是dp
    }
    
    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    
    
    public static void setCallback(StopCall c) {
        stopCall = c;
    }
    
    /**
     * 关键部分在这里,测量当前ScrollView滑动的距离
     * <p>
     * 其中t就是,单位是px,不是dp
     * <p>
     * stopCall是一个接口,是为了在Activity中实现设置顶部1/2可不可见
     */
    
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (t > upH) {//如果向上滑动距离>本例中图片高度
            stopCall.stopSlide(true);//设置顶部1可见,顶部2不可见
        } else {//否则
            stopCall.stopSlide(false);//设置顶部1不可见,顶部2可见
    
        }
    
    }
    
    /**
     * F: 将dp转成为px
     */
    
    private int dpTopx(int dpValue) {
        final float scale = this.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
    
    
    public interface StopCall {
    
        void stopSlide(boolean isStop);
    }
    

    }
     

     在activity中

    public class MainActivity extends AppCompatActivity implements MyScrollView.StopCall{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
      myscroview.setCallback(this);//设置myscrollview滑动的监听
    }

    @Override
    public void stopSlide(boolean isStop) {

    //根据滑动距离显示或隐藏顶部1,顶部2
    if (isStop) {
    rl2.setVisibility(View.INVISIBLE);
    rl1.setVisibility(View.VISIBLE);
    } else {
    rl1.setVisibility(View.INVISIBLE);
    rl2.setVisibility(View.VISIBLE);
    }

    }

    }


    作者:江月明
    来源:CSDN
    原文:https://blog.csdn.net/lst_shuo/article/details/78280918
    版权声明:本文为博主原创文章,转载请附上博文链接!

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月29日

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大