我的ListView 和系統自帶的SimpleCursorAdapter里頭放了一個ImageVIew,但是無法獲取正確獲取其點擊事件應該咋整?求問

uj5u.com熱心網友回復:
直接給ImageView設定OnCLickListener,有什么問題啊?uj5u.com熱心網友回復:
在adapter里,就給imageView設定listener啊uj5u.com熱心網友回復:
Adapter 中 系結@BindView(R.id.image) ImageView image;
public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
holder.image.setOnClickListener(v -> {})就可以了
uj5u.com熱心網友回復:
什么叫無法獲取點擊事件,listview.setOnItemClick...不行的意思嗎?發個代碼出來uj5u.com熱心網友回復:
代碼大概就是這樣的: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="https://bbs.csdn.net/topics/@drawable/not_selected"
android:layout_width="40dp"
android:layout_height="35dp"
android:layout_marginLeft="370dp"
android:layout_centerVertical="true"
/>
</RelativeLayout>
<!-- android:layout_marginTop="15dp"-->
uj5u.com熱心網友回復:
建議你使用recycleView哦,單獨的子控制元件也能使用自身的事件uj5u.com熱心網友回復:
有這句嗎ListView.setOnItemClickListener(this);轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/102786.html
標籤:Android
