Android Scrollview 滑動到指定位置

百度上有很多滴 一查就查到了
我只是開發(fā)的時候遇見了 就寫下來 省著忘記了
不過既然都寫了 就記錄的全一點 如果看到的人有自己的想法可以私信我

白話文來講 你點擊某一個控件 想要滑動到下面的某塊區(qū)域

使用 Scrollview 當(dāng)然不是唯一的答案,但是絕對是最簡單的答案

首先 分為兩種情況

-屬于 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需要的距離
    }

滑動到對應(yīng)位置

  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()的第二個參數(shù)代表要滾動到的位置,top為滾動到指定view的頂部,還可以滾動到bottom
scrollTo()使用同上 只不過這個是瞬間完成 而上邊的緩慢的完成

效果圖

-不是Scrollview的子view,會復(fù)雜一點

//注意使用 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在當(dāng)前窗口內(nèi)的絕對坐標(biāo)
view.getLocationInWindow();
// 獲取在整個屏幕內(nèi)的絕對坐標(biāo),注意這個值是要從屏幕頂端算起,也就是包括了通知欄的高度。
view.getLocationOnScreen();

還有一個就是滑動到頂部 和滑動到底部了
滾到到底部:
scrollView.fullScroll(ScrollView.FOCUS_DOWN);

滾動到頂部:
scrollView.fullScroll(ScrollView.FOCUS_UP);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容