Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách triển khai long click listener cho ListView trong Android

Giới thiệu

Hướng dẫn này trình bày cách xử lý sự kiện nhấn giữ (long click) trên các mục trong ListView Android.

Các bước thực hiện

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project và điền thông tin cần thiết.

Bước 2: Thiết kế giao diện (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

Bước 3: Viết logic xử lý (src/MainActivity.java)

package app.com.sample;

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 androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    String[] mobileArray = {"Android", "iPhone", "Windows Mobile", "Blackberry", "WebOS", "Ubuntu", "Windows 7", "Mac OS X"};
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        ListView listView = findViewById(R.id.listView);
        ArrayAdapter<> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mobileArray);
        listView.setAdapter(adapter);
        
        // Đăng ký long click listener
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                String selectedItem = (String) parent.getItemAtPosition(position);
                Toast.makeText(getApplicationContext(), "Nhấn giữ: " + selectedItem, Toast.LENGTH_SHORT).show();
                return true; // Trả về true để báo đã xử lý sự kiện
            }
        });
    }
}

Bước 4: Cấu hình Manifest (androidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">
    <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=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Chạy ứng dụng

Kết nối thiết bị Android, nhấn Run trong Android Studio. Nhấn giữ vào bất kỳ mục nào trong danh sách để xem Toast hiển thị.

Cách triển khai long click listener cho ListView trong Android