我的ListView 和系统自带的SimpleCursorAdapter里头放了一个ImageVIew,但是无法获取正确获取其点击事件应该咋整?求问

解决方案 »

  1.   

    直接给ImageView设置OnCLickListener,有什么问题啊?
      

  2.   

    在adapter里,就给imageView设置listener啊
      

  3.   

      Adapter 中  绑定  
    @BindView(R.id.image) ImageView image;
    public ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
      holder.image.setOnClickListener(v -> {})就可以了
      

  4.   

    什么叫无法获取点击事件,listview.setOnItemClick...不行的意思吗?发个代码出来
      

  5.   

    代码大概就是这样的:
    MainActivity:
    onCreat(){
    adapter = new SimpleCursorAdapter(this, R.layout.notes_list_cell, null, new String[]{NotesDB.COLUMN_NAME_NOTE_NAME, NotesDB.COLUMN_NAME_NOTE_DATE}, new int[]{R.id.tvName, R.id.tvDate}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
            setListAdapter(adapter);
    }
    方法也就写了一个只有
    protected void onListItemClick(ListView l, View v, int position, long id) {
            super.onListItemClick(l, v, position, id);
            Cursor c = adapter.getCursor();
            c.moveToPosition(position);
            not_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this,"YOU CLICKED",Toast.LENGTH_LONG).show();
                }
            });
            //这里的EXTRA_NOTE_ID是下一个界面里的noteId,用来判断是否需要编辑;
            Intent i = new Intent(MainActivity.this, AtyEditNote.class);
            i.putExtra(AtyEditNote.EXTRA_NOTE_ID, c.getInt(c.getColumnIndex(NotesDB.COLUMN_NAME_ID)));
            i.putExtra(AtyEditNote.EXTRA_NOTE_NAME, c.getString(c.getColumnIndex(NotesDB.COLUMN_NAME_NOTE_NAME)));
            i.putExtra(AtyEditNote.EXTRA_NOTE_CONTENT, c.getString(c.getColumnIndex(NotesDB.COLUMN_NAME_NOTE_CONTENT)));
            startActivityForResult(i, REQUEST_CODE_ADD_NOTE);    }两个布局文件:
    activity.main
    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:fitsSystemWindows="true"
        tools:context="example.com.notes.MainActivity">    <ImageView
            android:id="@+id/bing_pic_img"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:alpha="150"
           />
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:fitsSystemWindows="true"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="#00000000"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                />        <ListView
                android:id="@android:id/list"
                android:layout_below="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layoutAnimation="@anim/listview_anim"
                android:layout_weight="3"        > </ListView>
            <Button
                android:alpha="100"
                android:layout_marginBottom="0dp"
                android:id="@+id/btnAddNote"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="添加日志"/>
        </LinearLayout></FrameLayout>    <!-- android:layout_weight="1"
         if(Build.VERSION.SDK_INT >= 21){
                View decorView = getWindow().getDecorView();
                decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                getWindow().setStatusBarColor(Color.TRANSPARENT);        }        <Button
                android:id="@+id/btnAddNote"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="添加日志"/> android:background="?attr/colorPrimary"
            -->notes_list_cell.xml
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout        xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/idtest"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:id="@+id/tvName"
                android:textStyle="bold"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="NULL TITLE"
                android:textAppearance="?android:attr/textAppearanceLarge"
               />        <TextView
                android:id="@+id/tvDate"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />    </LinearLayout>
        <ImageView
            android:clickable="true"
            android:focusable="false"
            android:id="@+id/not_selected"
            android:adjustViewBounds="true"
            android:fitsSystemWindows="true"
            android:src="@drawable/not_selected"
            android:layout_width="40dp"
            android:layout_height="35dp"
            android:layout_marginLeft="370dp"
            android:layout_centerVertical="true"
            />
        
    </RelativeLayout>
    <!-- android:layout_marginTop="15dp"-->
      

  6.   

    建议你使用recycleView哦,单独的子控件也能使用自身的事件
      

  7.   

    有这句吗ListView.setOnItemClickListener(this);