Android listviewシリーズのitemのクリックイベントおよびitemレイアウトにおけるクリックイベント(四)

18802 ワード

多くの場合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撮影とデータアップロードが含まれています.

<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>

これらの機能を実現するには、元のアダプタが足りないので、listviewを支援する強力なadapterが必要です.次のコードの注釈は、これらの機能を実現することを理解するのに役立ちます.注:以下のコードは不完全なコードで、関連するコードを残して、他のコードはすべて削除して、学ぶ友达は自分で理解することができて書くことができて、あるいは評論を出して聞いてみます

/**
 *                  listview   
 * @author jing_jie
 *
 */
public class IndexListViewAdapter extends BaseAdapter{
    //     ,           
    PhotoUtil photoUtil = new PhotoUtil();
    //       
    List indexs;
    //         
    List 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();
            }
        }
    };

このブログは少し粗野で、見たい友达は辛抱強く...