androidlistview 시리즈의 item 클릭 이벤트 및 item 레이아웃의 클릭 이벤트 (4)

18948 단어 Android--기본
listview는 간략한 정보만 표시하는 경우가 많습니다. 하위 항목을 클릭하여 이동하거나 상세한 정보를 표시하는 위치에 자세한 정보를 표시해야 합니다. listview는 onItem ClickListener () 방법을 제공합니다. 방법에서 우리가 필요로 하는 내용을 실행할 수 있습니다.
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();
            }
        }
    };

이 블로그는 좀 거칠어요. 보시는 분들은 참으셨으면 좋겠어요.

좋은 웹페이지 즐겨찾기