Tìm hiểu ListView trong Android

Khi nói về UI Layout không thể không nói đến ListView trong Android. Vậy ListView là gì? Có mấy cách khai báo ListView? Sử dụng ListView như thế nào?

Tìm hiểu về ListView trong Android

Bây giờ tôi cùng các bạn sẽ cùng nhau tìm hiểu ListView trong Android.

1. ListView là gì?

ListView trong Android là một view group, hiển thị các item theo một danh sách có thể cuộn được theo chiều thẳng đứng. Tiếp theo đây là phần khai báo ListView trong Android.

2. Cú pháp khái báo ListView trong Android

Trong Android ListView được khai báo như sau.

<ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
</ListView>

3. Các thuộc tính của ListView trong Android

Một số thuộc tính quan trọng của ListView tôi đã liệt kê ở dưới.

Thuộc tính Mô tả
android:id ID là duy nhất để nhận diện Layout
android:divider Nó có thể vẽ hoặc tô màu giữa các item trong danh sách.
android:dividerHeight Nó xác định chiều cao của dải phân cách. Điều này có thể là px, dp, sp, in hoặc mm.
android:entries Chỉ định tham chiếu đến một tài nguyên mảng sẽ điền vào ListView.
android:footerDividersEnabled Khi được đặt thành false, ListView sẽ không vẽ dải phân cách trước mỗi chế độ xem chân trang. Giá trị mặc định là true.
android:headerDividersEnabled Khi được đặt thành false, ListView sẽ không vẽ dải phân cách sau mỗi chế độ xem tiêu đề. Giá trị mặc định là true.

4. Ví dụ sử dụng ListView trong Android

Để hiểu rõ hơn về ListView trong Android mới các bạn cùng tôi đến với ví dụ sau. Và đây là source code ví dụ sử dụng ListView.

Sau đây là 5 bước chuẩn để thực hiện quá trình tạo Project sử dụng ListView trong Android.

Bước 1: Khởi tạo project với tên ListViewExample.

Chọn File >> New >> New Project. Xuất hiện form và đặt tên project ListViewExample sau đó click Finish.

Cấu trúc project ListViewExample như sau.

Tìm hiểu về ListView trong Android

Bước 2: Vào res >> layout >> list_view_activity.xml và thêm vào đoạn code dưới đây. Trong ví dụ sử dụng ListView tôi sẽ lấy danh sách ngôn ngữ lập trình đổ vào trong ListView.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.teamvietdev.listviewexample.ListViewActivity">

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ListView>

</android.support.constraint.ConstraintLayout>

Bước 3: Mở file ListViewActivity.java và thêm đoạn code sau đây vào.

package com.teamvietdev.listviewexample;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;


import java.util.ArrayList;
import java.util.List;

public class ListViewActivity extends AppCompatActivity {

    private ListView listView;
    private List<String> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_view_activity);

        listView = (ListView) findViewById(R.id.listview);
        list = new ArrayList<>();
        list.add("Lập Trình Android");
        list.add("Lập Trình Java");
        list.add("Lập Trình JavaFX");
        list.add("Lập Trình Web");
        list.add("Lập Trình Ruby");
        list.add("Lập Trình C++");
        list.add("Lập Trình PHP");
        list.add("Lập Trình WordPress");
        ArrayAdapter adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, list);
        listView.setAdapter(adapter);
    }

}

Bước 4: Mở file AndroidManifest.xml và dán đoạn code sau đây vào.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.teamvietdev.listviewexample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".ListViewActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Bước 5: Sau cùng vào res >> values >> strings.xml và dán đoạn code sau đây vào.

<resources>
    <string name="app_name">Frame Layout Example</string>
    <string name="tv_teamvietdev">TEAM VIET DEV</string>
</resources>

Kết quả chạy ví dụ sử dụng ListView trong Android như sau.

Tìm hiểu về ListView trong Android

Lời kết: Như vậy là các bạn đã tìm hiểu về ListView trong Android. Tuy nhiên đây chỉ là phần cơ bản về ListView, bởi vậy các item trong ListView khá đơn điệu và không bắt mắt. Vì lý do đó các bạn cần phải biết custom ListView trong Android. Ngoài ra các bạn có thể theo dõi thêm các chuyên mục khác tại Team Việt Dev.

Xem thêm các layout khác tại đây.

Sử dụng Linear Layout trong Android

Sử dụng Relative Layout trong Android

Sử dụng Table Layout trong Android

Đang cập nhật…

(Tác giả: Team Việt Dev)

Bình luận