美文网首页
Android Scrollview 滑动到指定位置

Android Scrollview 滑动到指定位置

作者: 珹_ | 来源:发表于2020-12-20 10:30 被阅读0次

    百度上有很多滴 一查就查到了
    我只是开发的时候遇见了 就写下来 省着忘记了
    不过既然都写了 就记录的全一点 如果看到的人有自己的想法可以私信我

    白话文来讲 你点击某一个控件 想要滑动到下面的某块区域

    使用 Scrollview 当然不是唯一的答案,但是绝对是最简单的答案

    首先 分为两种情况

    -属于 Scrollview 的子条目 (这个时候 最好用 LinearLayout 嵌套一下布局)

    布局
       <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
                <TextView
    
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/dp_100"
                    android:gravity="center"
                    android:background="@color/black"/>
                <TextView
    
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/dp_100"
                    android:gravity="center"
                    android:background="@color/black"/>
              <TextView
                  android:id="@+id/text1"
                  android:layout_width="match_parent"
                  android:layout_height="@dimen/dp_100"
                  android:gravity="center"
                  android:text="textview1"/>
    
                <TextView
    
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/dp_100"
                    android:gravity="center"
                    android:background="@color/black"/>
                <TextView
    
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/dp_100"
                    android:gravity="center"
                    android:background="@color/black"/>
                <TextView
                    android:id="@+id/text2"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/dp_100"
                    android:gravity="center"
                    android:text="textview2"/>
                <TextView
    
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/dp_100"
                    android:gravity="center"
                    android:background="@color/black"/>
                <TextView
    
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/dp_100"
                    android:gravity="center"
                    android:background="@color/black"/>
    
                <TextView
                    android:id="@+id/text3"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/dp_100"
                    android:gravity="center"
                    android:text="textview3"/>
                <TextView
    
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/dp_100"
                    android:gravity="center"
                    android:background="@color/black"/>
                <TextView
    
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/dp_100"
                    android:gravity="center"
                    android:background="@color/black"/>
    
                <TextView
                    android:id="@+id/text4"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/dp_100"
                    android:gravity="center"
                    android:text="textview4"/>
            </LinearLayout>
    
      </ScrollView>
    

    这里为了能更好的看到了效果 所以中间穿插的很多 TextView

    代码

    计算滑动距离

     @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            super.onWindowFocusChanged(hasFocus);
            top1 = text1.getTop();  //滑动到textview1需要的距离
            top2 = text2.getTop();  //滑动到textview2需要的距离
            top3 = text3.getTop();  //滑动到textview3需要的距离
            top4 = text4.getTop();  //滑动到textview4需要的距离
        }
    

    滑动到对应位置

      l1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    scrollView.smoothScrollTo(0, top1);
                }
            });
            l2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    scrollView.smoothScrollTo(0, top2);
                }
            });
            l3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    scrollView.smoothScrollTo(0, top3);
                }
            });
            l4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    scrollView.smoothScrollTo(0, top4);
                }
            });
    

    smoothScrollTo()的第二个参数代表要滚动到的位置,top为滚动到指定view的顶部,还可以滚动到bottom
    scrollTo()使用同上 只不过这个是瞬间完成 而上边的缓慢的完成

    效果图

    -不是Scrollview的子view,会复杂一点

    //注意使用 getLocationInWindow 还是 getLocationOnScreen 需要看情况而定
    int[] ln= new int[2];
    view.getLocationInWindow(ln); `
    int position = location[1] -  title.getHeight();
    if (position< 0) {
        position= 0;
    }
    scrollview.smoothScrollTo(0,position);
    
    
    // 获取v在当前窗口内的绝对坐标
    view.getLocationInWindow();
    // 获取在整个屏幕内的绝对坐标,注意这个值是要从屏幕顶端算起,也就是包括了通知栏的高度。
    view.getLocationOnScreen();
    
    
    还有一个就是滑动到顶部 和滑动到底部了
    滚到到底部:
    scrollView.fullScroll(ScrollView.FOCUS_DOWN);
    
    滚动到顶部:
    scrollView.fullScroll(ScrollView.FOCUS_UP);
    

    相关文章

      网友评论

          本文标题:Android Scrollview 滑动到指定位置

          本文链接:https://www.haomeiwen.com/subject/uanhnktx.html