当我正在开发一个应用程序时,它有一个导航抽屉,它工作得很好,但我放置了两个图像视图,他们不会触发onClickListener ..请告诉我如何实现onclciklistener工作,如编辑和删除操作... 和
rowlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/relative">
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/imageView1"
android:src="@drawable/ic_menu_delete" />
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_menu_edit" />
</RelativeLayout>
和mainActivity.java
/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
final long id) {
text = parent.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(), "hello" + text, 3000)
.show();
selectItem(position);
positn = position;
mDrawerList.setItemChecked(position, true);
setTitle(text);
mDrawerLayout.closeDrawer(mDrawerList);
// Toast.makeText(getApplicationContext(), "" + text,
// Toast.LENGTH_SHORT).show();
ImageView edit_image = (ImageView) findViewById(R.id.imageView1);
edit_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "clicked", 3000)
.show();
}
});
if (text.equals("add new item")) {
AlertDialog.Builder builder2 = new AlertDialog.Builder(
MainActivity.this);
final EditText input = new EditText(MainActivity.this);
input.setHeight(100);
input.setWidth(340);
input.setGravity(Gravity.LEFT);
input.setImeOptions(EditorInfo.IME_ACTION_DONE);
builder2.setMessage("Press OK or Cancel");
builder2.setView(input);
builder2.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
userinput = input.getText().toString();
// int length2 = userinput.length();
// String string23 = Integer.toString(length2);
// Log.d("length of userinput", string23);
listItems.add(userinput);
adapter = new ArrayAdapter<String>(
getApplicationContext(),
R.layout.rowlayout, R.id.label,
listItems);
mDrawerList.setAdapter(adapter);
mDrawerLayout.openDrawer(mDrawerList);
String counter = "0";
Log.v("id", "" + id);
Log.v("name", "" + userinput);
Log.v("value", "" + counter);
database1.open();
long insertid;
insertid = database1.Insertvalues(id1,
userinput, counter);
// String string2 = Integer.toString((int)
// insertid);
// Log.d("values inserted", string2);
// Toast.makeText(getApplicationContext(), "" +
// insertid, 3000).show();
}
});
builder2.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
}
});
builder2.show();
}
}
}
答案 0 :(得分:2)
您的代码运行正常。
但问题是您在点击 ListItem
后为 ImageView 设置 onClick您必须点击 ListItem ,然后点击 ImageView 。
请将此代码移至 onItemClick
之外ImageView edit_image = (ImageView) findViewById(R.id.imageView1);
edit_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "clicked", 3000)
.show();
}
});
答案 1 :(得分:0)
您必须创建自定义数组适配器并将其设置为抽屉列表视图。在扩展项目布局时,在自定义适配器getView()上,找到图像视图并设置监听器(这将是内部子项目)
@Override
public View getView(int position , View contentView , ViewGroup parent)
{
View row = ((Activity)mContext).getLayoutInflater().inflate(R.layout.rowlayout, null);
ImageView imageView2 = (ImageView) row.findViewById(R.id. imageView2);
imageView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// handle the edit click here
}
});
ImageView imageView1 = (ImageView) row.findViewById(R.id. imageView1);
imageView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// handle the delete click here
}
});
return row;
}