Giao diện minh họa:
Đầu tiên ta tạo layout:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:paddingBottom="@dimen/activity_vertical_margin"tools:context=".MainActivity"><ScrollView android:id="@+id/scrollView"android:layout_width="wrap_content"android:layout_height="wrap_content"><LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Xin Chào" ></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Xin Chào" ></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Xin Chào" ></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Xin Chào" ></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Xin Chào" ></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Xin Chào" ></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Xin Chào" ></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Xin Chào" ></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Xin Chào" ></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Xin Chào" ></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Xin Chào" ></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Xin Chào" ></Button> </LinearLayout> </ScrollView> </LinearLayout>
Sau đó:
Xử lý trong class MainActivity.java
package com.example.minhtung.demoscroll; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.view.ViewTreeObserver; import android.widget.ScrollView; import android.widget.Scroller; import android.widget.Toast; public class MainActivity extends ActionBarActivity implements ViewTreeObserver.OnScrollChangedListener{ ScrollView sc; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sc=(ScrollView)findViewById(R.id.scrollView); //Tim id scrollView bên layout sc.getViewTreeObserver().addOnScrollChangedListener(this); //Lấy ViewTree sau đó add vào sự kiện Scroll change } @Override public void onScrollChanged() { Toast.makeText(MainActivity.this,"bạn đã scroll",Toast.LENGTH_SHORT).show();//xuất ra thông báo } }
Lucky Club Casino Site » Sign Up & Get Bonus up to €1000
Trả lờiXóaGet €1000 welcome bonus, welcome offers, no deposit bonuses, free spins, and more. Sign up today and claim our 100% welcome luckyclub.live bonus!