[英]OnClickListener doesn't works in Navigation Drawer
当我开发一个具有导航抽屉的应用程序时,它可以正常工作,但是我放置了两个图像视图,在这些视图中它们不会触发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();
}
}
}
您的代码工作正常。
但是问题是您单击了ListItem之后将onClick设置为ImageView
您必须单击到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();
}
});
您必须创建一个自定义阵列适配器并将其设置为抽屉列表视图。 在自定义适配器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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.