English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Android 버튼 클릭하여 위로 돌아가는 코드 구현

버튼을 클릭하여 상단으로 돌아가는 코드를 바로 보여드리겠습니다

레이아웃 파일

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#ffffff"
  android:orientation="vertical">
  <ScrollView
    android:id="@"+id/sv_home"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true">
    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical">
      <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:background="@mipmap"/eason"/>
      <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:background="@mipmap"/eason"/>
      <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:background="@mipmap"/eason"/>
                    <android.support.design.widget.FloatingActionButton
        android:id="@"+id/fab_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|bottom"
        android:layout_marginRight="10dp"
        android:src="@mipmap/top"
        app:backgroundTint="#ecefef"
        app:elevation="10dp"
        app:pressedTranslationZ="12dp"
        app:rippleColor="@color/colorPrimary" />
    </LinearLayout>
  </ScrollView>
</LinearLayout>

버튼 클릭 이벤트

topBtn.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {
      sc.post(new Runnable() {
        @Override
        public void run() {
         sc.post(new Runnable() {
           public void run() {
            // 위로 돌아가기
            sc.fullScroll(ScrollView.FOCUS_UP);
           }
         });
        }
      });
     }
   });

끝까지 이동 버튼 추가

 bottomBtn.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {
      sc.post(new Runnable() {
        @Override
        public void run() {
         sc.post(new Runnable() {
           public void run() {
            // 끝까지 스크롤
            sc.fullScroll(ScrollView.FOCUS_DOWN);
           }
         });
        }
      });
     }
   });

이것이 이 문서의 전체 내용입니다. 많은 도움이 되었기를 바랍니다. 또한, 나아가教程을 많이 지지해 주시기 바랍니다.

성명서: 이 문서의 내용은 인터넷에서 가져왔으며, 저작권자는 모두에게 있습니다. 내용은 인터넷 사용자가 자발적으로 기여하고 업로드한 것이며, 이 웹사이트는 소유권을 가지지 않으며, 인공 편집을하지 않았으며, 관련 법적 책임도 부담하지 않습니다. 저작권 문제가 있는 내용을 발견하면 notice#w로 이메일을 보내 주시기 바랍니다.3codebox.com(보내는 이메일에서 #을 @으로 변경하십시오. 신고하고 관련 증거를 제공하시면, 사실이 확인되면 이 사이트는 즉시 의심스러운 저작권 내용을 삭제합니다。)

좋아하는 것