1. 程式人生 > >Android Studio ListView的item點選事件彈出AlertDialog,和item的滑動

Android Studio ListView的item點選事件彈出AlertDialog,和item的滑動

首先看看效果圖:

點選彈出AlertDialog的確認框!

(一)第一步,建立一個xml檔案顯示item的佈局

student_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/ig1"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_marginTop="10dp"
            android:src="@mipmap/ic_launcher_round" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="7dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv1"
                android:layout_width="match_parent"
                android:layout_height="20dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="9dp"
                android:text="同學甲" />

            <TextView
                android:id="@+id/tv2"
                android:layout_width="match_parent"
                android:layout_height="20dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="9dp"
                android:text="詳情" />

        </LinearLayout>
    </LinearLayout>

</LinearLayout>執行結果

(二):在你想要顯示的佈局檔案裡新增ListView

這裡我選擇的是新建一個xml檔案

(三):就是ACtivity了

package com.example.hellohh.myapplication_listview;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity implements AdapterView.OnItemClickListener,AbsListView.OnScrollListener {
    private ListView listView;
    private SimpleAdapter simpleAdapter;
    private List<Map<String,Object>>dataList;

    String stu1 = new String( "同學1,學號1601030033,性別女,愛好唱歌" );
    String stu2 = new String( "同學2,學號1601030033,性別女,愛好唱歌" );
    String stu3 = new String( "同學3,學號1601030033,性別女,愛好唱歌" );
    String stu4 = new String( "同學4,學號1601030033,性別女,愛好唱歌" );
    String stu5 = new String( "同學甲,學號1601030033,性別女,愛好唱歌" );

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );

        dataList = new ArrayList<Map<String, Object>>(  );
        listView = (ListView) findViewById( R.id.lv1 );

//SimpleAdapter( MainActivity.this,getDate(),R.layout.student_item,new String[]{"ig1","tv1","tv2"},new int[]{R.id.ig1,R.id.tv1,R.id.tv2} );
//對應的引數詳解:
        /**
         * context:上下文
         * data:資料來源(List<? extend Map<String,?>>data)一個Map所組成的List集合
         *      每一個Map都回去對應ListView列表中的一行
         *      每一個Map(鍵-值對)中的鍵必須包含所有在from中指定的鍵
         * resource:列表項的佈局檔案ID
         * from:Map中的鍵名
         * to:繫結資料檢視中的ID,與form成對應關係
         */
        simpleAdapter = new SimpleAdapter( MainActivity.this,getDate(),R.layout.student_item,new String[]{"ig1","tv1","tv2"},new int[]{R.id.ig1,R.id.tv1,R.id.tv2} );
        listView.setAdapter( simpleAdapter ); //綁介面卡
        listView.setOnItemClickListener( this );
        listView.setOnScrollListener( this );
    }
//獲得資料:
    private List<Map<String,Object>> getDate(){
        String s = new String( "A" );
        for(int i=1;i<=15;i++){
            Map<String,Object> map = new HashMap<String, Object>(  );
            map.put( "ig1",R.mipmap.A );
            map.put( "tv1","同學"+i );
            map.put( "tv2","檢視同學"+i+"的詳情" );
            dataList.add(map);
        }
        return dataList;
    }

//設定手指滑動
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState) {
            case SCROLL_STATE_FLING:
                Log.i( "Main", "由於使用者在手指離開前滑動,而手指離開後的慣性滑動" );
                Map<String, Object> map = new HashMap<String, Object>();
                map.put( "ig1", R.mipmap.A );
                map.put( "tv1", "同學" );
                map.put( "tv2", "檢視同學的詳情" );
                dataList.add( map );
                //自動提醒重新整理資料
                simpleAdapter.notifyDataSetChanged();
                break;
            case SCROLL_STATE_IDLE:
                Log.i( "Main", "試圖停止滑動" );
                break;
            case SCROLL_STATE_TOUCH_SCROLL:
                Log.i( "Main", "手指沒有離開螢幕,試圖正在滑動" );
                break;
        }
    }
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    }
//設定點選ITem事件
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        AlertDialog.Builder builder = new AlertDialog.Builder( MainActivity.this);

        if(position==0){
            builder.setTitle( "同學1" );//設定標題
            builder.setIcon( R.drawable.ic_launcher_background );
            builder.setMessage( stu1);
        }
        else if(position==1){
            builder.setTitle( "同學2" );//設定標題
            builder.setIcon( R.drawable.ic_launcher_background );
            builder.setMessage( stu2);
        }
        builder.setPositiveButton( "確定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText( MainActivity.this,"點選了確定按鈕", Toast.LENGTH_SHORT).show();
            }
        } );
        builder.setNegativeButton( "取消",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText( MainActivity.this,"點選了取消按鈕", Toast.LENGTH_SHORT).show();
            }
        } );
        AlertDialog dialog = builder.create();
        dialog.show();
        //String text = listView.getItemAtPosition( position )+"";
        //Toast.makeText( this,"posistion="+position+" text="+text,Toast.LENGTH_SHORT ).show();
    }
}

這裡我只寫了兩個列表的點選事件,根據需求自己新增