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#oldtoolbag.com(メール送信時、#を@に変更して報告してください。関連する証拠を提供してください。一旦確認が取れたら、このサイトは即座に侵害される内容を削除します。)

基本チュートリアル
おすすめ