1. 程式人生 > >android listview系列之item的點選事件及item佈局中的點選事件(四)

android listview系列之item的點選事件及item佈局中的點選事件(四)

很多時候listview只顯示簡略資訊,我們需要點選子項去跳轉或在顯示詳細資訊的位置,將相關的詳細資訊顯示出來,listview提供了onItemClickListener()方法,在方法中我們可以執行我們需要的內容。

listview.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //我們需要的內容,跳轉頁面或顯示詳細資訊
} });

listview還提供了OnItemSelectedListener()方法,可以配合介面顯示的其它元件,關聯顯示。

//listView子選單選擇事件
helpcenterlistview.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long
id) { } @Override public void onNothingSelected(AdapterView<?> parent) { } });

下面關鍵點來了,關於item中包含點選事件,如有拍照按鈕,資料提交按鈕等,我們就需要在自定義的adapter中新增點選事件的回掉介面,來實現一些操作。
下面通過程式碼來具體說說。
我遇到一個專案要求在listview的item包含拍照按鈕,資料提交按鈕,資料輸入等,這時我就發愁了,怎麼去實現具體的功能,如拍照,拍照返回顯示的圖片不要序列等,遇到了好多問題,這兒就以這個為例給大家講一下。
下面是item的佈局檔案程式碼,裡面包含Textview顯示資訊,ImageButton拍照和資料上傳

<?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="wrap_content"
    android:orientation="vertical" 
    android:padding="8dp">
    <View
        android:layout_width="fill_parent"
        android:layout_height="0.5px"
        android:background="#B8B8B8"
        android:visibility="visible"
        />
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="1">
        <LinearLayout 
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">
            <LinearLayout 
                android:id="@+id/ll_index_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="8dp"
                android:orientation="horizontal">
                <TextView 
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="指標名稱"
                    android:textColor="#ffffff"
                    android:textSize="20sp"/>
                <TextView
                    android:id="@+id/tv_index"
                    android:layout_marginLeft="16dp"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="監測指標"
                    android:textColor="#ffffff"
                    android:textSize="20sp" />
            </LinearLayout>
            <LinearLayout 
                android:id="@+id/ll_value"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="16dp"
                android:orientation="horizontal">
                <Button 
                    android:id="@+id/btn_photo"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:layout_gravity="center"
                    android:background="@drawable/btn_paizhao"/>
                <ImageButton
                    android:id="@+id/ib_upload"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="18dp"
                    android:layout_gravity="center"
                    android:background="@drawable/tijiao"/>
            </LinearLayout>
        </LinearLayout>
        <ImageView 
            android:id="@+id/iv_data_photo"
            android:layout_height="300dp"
            android:layout_width="300dp"
            android:layout_gravity="center_vertical"/>
    </LinearLayout>
    <View
        android:layout_width="fill_parent"
        android:layout_height="0.5px"
        android:background="#B8B8B8"
        android:visibility="visible" 
        />
</LinearLayout>

要實現這些功能原先的介面卡就有些不夠用了,就需要一個強大的adapter來幫助listview來完成。
下面在程式碼的註釋會幫助你去理解實現這些功能。
注:下面程式碼是不完整程式碼,留下了相關程式碼,別的程式碼都刪了,要學習的朋友可以自己去理解的寫,或者發評論問問


/**
 * 自定義的任務下的監測點的監測指標的listview介面卡
 * @author jing_jie
 *
 */
public class IndexListViewAdapter extends BaseAdapter{
    //拍照工具類,用來處理一些照片的方法
    PhotoUtil photoUtil = new PhotoUtil();
    //檢測指標的集合
    List<MeasurementUtil> indexs;
    //檢測指標狀態的集合
    List<MeasurementState> ms;
    //傳入按鈕點選事件
    private MyClickListener mListener;
    private MyClickListener pListener;
    //傳入當前的DBmanager
    DBManager dbmanager;
    LayoutInflater inflater;
    //需要傳入點選事件
    public IndexListViewAdapter(Context ctx,MyClickListener mListener,MyClickListener pListener,MyClickListener xListener){
        inflater = LayoutInflater.from(ctx);
        this.mListener = mListener;
        this.pListener = pListener;
        this.xListener = xListener;
    }
    @Override
    public int getCount() {
        return indexs.size();
    }
    @Override
    public Object getItem(int position) {
        return indexs.get(position);
    }
    @Override
    public long getItemId(int position) {
        return indexs.get(position).getId();
    }
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        int valuetype = indexs.get(position).getValueType();

        view = inflater.inflate(R.layout.activity_table_photo, null);
        TextView tv_index = (TextView)view.findViewById(R.id.tv_index);
        TextView tv_description = (TextView)view.findViewById(R.id.tv_description);
        Button btn_photo = (Button)view.findViewById(R.id.btn_photo);
        ImageButton ib_upload = (ImageButton)view.findViewById(R.id.ib_upload); 

        ImageView iv_photo = (ImageView)view.findViewById(R.id.iv_data_photo);
        EditText et_note = (EditText)view.findViewById(R.id.et_index_note);

        //給點選事件的view新增tag和點選事件
        btn_photo.setTag(position);
        btn_photo.setOnClickListener(pListener);
        ib_upload.setTag(position);
        ib_upload.setOnClickListener(mListener);
        return view;
    }

    /**
      * 用於回撥的抽象類
      */
     public static abstract class MyClickListener implements OnClickListener {
         /**
          * 基類的onClick方法
          */
         @Override
         public void onClick(View v) {
             myOnClick((Integer) v.getTag(), v);
         }
         public abstract void myOnClick(int position, View v);
     }
}

adapter的使用

mIndexAdapter = new IndexListViewAdapter(DataActivity.this,mListener,pListener);
lv_data.setAdapter(mIndexAdapter);

注意在使用的時候需要響應點選事件的實現類,及傳入構造方法的mListener,pListener。
這而就實現一下拍照按鈕

//拍照按鈕的點選事件
    private MyClickListener pListener = new MyClickListener() {
        @Override
        public void myOnClick(int position, View v) {
            //獲得元件
            //在GridView和ListView中,getChildAt ( int position ) 方法中position指的是當前可見區域的第幾個元素。
            //如果你要獲得GridView或ListView的第n個View,那麼position就是n減去第一個可見View的位置
            view = lv_data.getChildAt(position - lv_data.getFirstVisiblePosition());
            //獲得item中的對應的Imageview,用來拍照返回的時候顯示照片略縮圖
            iv_photo = (ImageView)view.findViewById(R.id.iv_data_photo);
            try {
                String state = Environment.getExternalStorageState();
                if (state.equals(Environment.MEDIA_MOUNTED)) {//判斷sd卡是否插入
                    //設定路徑
                    mPhotoPath = Environment.getExternalStorageDirectory().getPath()
                            + "//patrol//" + "//" + photoUtil.getPhotoFileName();
                    mPhotoFile = new File(mPhotoPath);
                    if (!mPhotoFile.exists()) {
                        mPhotoFile.createNewFile();
                    }
                    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                    // 載入路徑
                    Uri uri = Uri.fromFile(mPhotoFile);
                    // 指定儲存路徑,這樣就可以儲存原圖了
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                    startActivityForResult(intent, CAMERA_RESULT);
                }else {
                    appContext.showInfo("請插入SD卡");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

本篇部落格有點粗獷,希望看的朋友們耐心點。。。