[英]GridView OnclickListener doesnt work if there is an EditText
I am using a gridview and the view in each square is a relative layout. 我正在使用gridview,每个正方形中的视图都是相对布局。 The onitemclicklistener will not work if there is an edittext or button in the relativelayout.
如果相对布局中有一个编辑文本或按钮,则onitemclicklistener无效。
It is not a problem with the adapter because I tested the onclicklistener without any edittext or buttons and it worked. 适配器没有问题,因为我在没有任何编辑文本或按钮的情况下测试了onclicklistener,并且可以正常工作。
How do I make it work with an edit text and button? 如何使它与编辑文本和按钮一起使用?
Itemlayout 项目布局
<ImageView
android:id="@+id/ivcustom1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/selectexcercise" />
<TextView
android:id="@+id/tvcustomsets1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/ivcustom1"
android:textColor="#3498db"
android:text="Sets:" />
<TextView
android:id="@+id/tvcustomreps1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tvcustomsets1"
android:textColor="#3498db"
android:text="Reps:" />
<EditText
android:id="@+id/etcustom2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvcustomreps1"
android:layout_alignBottom="@+id/tvcustomreps1"
android:layout_alignRight="@+id/ivcustom1"
android:background="@android:color/transparent"
android:hint="Enter Reps"
android:textColor="#3498db"
android:inputType="number"
android:maxLength="6"
android:textSize="12sp" >
</EditText>
<EditText
android:id="@+id/etcustom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvcustomsets1"
android:layout_alignBottom="@+id/tvcustomsets1"
android:layout_alignLeft="@+id/etcustom2"
android:background="@android:color/transparent"
android:hint="Enter Sets"
android:textColor="#3498db"
android:inputType="number"
android:maxLength="6"
android:textSize="12sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignBottom="@+id/ivcustom1"
android:layout_alignRight="@+id/etcustom1"
android:background="#3498db" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="23dp"
android:layout_alignLeft="@+id/tvcustomreps1"
android:layout_alignRight="@+id/etcustom1"
android:layout_below="@+id/tvcustomreps1"
android:background="@drawable/stretchesclicked"
android:text="Note"
android:textColor="#FFFFFF"
android:textSize="14sp" />
</RelativeLayout>
Just in case here is the onitemclicklistener 以防万一,这里是onitemclicklistener
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
System.out.println("The position is" + position);
}
});
and the adapter 和适配器
public class CustomWorkoutAdapter extends BaseAdapter {
private Context mContext;
public CustomWorkoutAdapter(Context c) {
mContext = c;
}
public int getCount() {
return rList.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(final int position, View convertView,
ViewGroup parent) {
RelativeLayout RL = rList.get(position);
return RL;
}
}
In your Itemlayout,add this: 在您的项目布局中,添加以下内容:
android:descendantFocusability="blocksDescendants"
try it. 试试吧。
ListViews are not made for that. ListView不是为此而创建的。 If you want an item to be editable, you handle the ListView.OnItemClickListener() event and have a Dialog pop-up that allows them to edit it.
如果要使项目可编辑,请处理ListView.OnItemClickListener()事件,并弹出一个Dialog对话框,允许他们对其进行编辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.