Tự học lập trình online

Lập trình online

Chương trình Demo cách sử dung Scroll trong android

Scroll được hiểu là thanh cuộn. Khi dữ liệu dài quá màn hình, thì người dùng sẽ sử dụng thanh cuộn để scroll lên hoặc xuống.
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    }
}
Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

1 nhận xét:

  1. Lucky Club Casino Site » Sign Up & Get Bonus up to €1000
    Get €1000 welcome bonus, welcome offers, no deposit bonuses, free spins, and more. Sign up today and claim our 100% welcome luckyclub.live bonus!

    Trả lờiXóa