android – OnItemClickListener不工作,但OnItemLongClickListener正在GridView上工作

前端之家收集整理的这篇文章主要介绍了android – OnItemClickListener不工作,但OnItemLongClickListener正在GridView上工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在gridview上遇到一个clickListener问题. LongClickListener无问题.但是我似乎没有从点击侦听器得到任何响应.

我的代码如下

我很困惑为什么长的点击工作,但不是正常的点击,

任何指针都将不胜感激

谢谢

  1. final GridView gridView = (GridView) findViewById(R.id.grid_view);
  2. gridView.setNumColumns(numOfColumns);
  3. gridView.getLayoutParams().width = (CELL_WIDTH * numOfColumns);
  4. gridView.getLayoutParams().height = (CELL_WIDTH * numOfRows);
  5.  
  6. ....
  7.  
  8. gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  9. public void onItemClick(AdapterView<?> parent,View v,int position,long id) {
  10. Log.d("ABCD","Position Single Click is " + position);
  11. // Ideally in here I want to put to open a soft keyboard for the user to enter a value
  12. // InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
  13. // imm.showSoftInput(gridView,InputMethodManager.SHOW_IMPLICIT);
  14. }
  15. });
  16.  
  17. gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
  18. @Override
  19. public boolean onItemLongClick(AdapterView<?> parent,View view,long id) {
  20. Log.d("ABCD","Position Long Click is " + position);
  21. return true;
  22. }
  23. });

grid_view是

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:descendantFocusability="blocksDescendants"
  6. android:orientation="vertical">
  7.  
  8. <LinearLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="0dp"
  11. android:layout_weight="1"
  12. android:gravity="center"
  13. android:orientation="horizontal">
  14.  
  15. <View
  16. android:layout_width="0dp"
  17. android:layout_height="wrap_content"
  18. android:layout_weight="2"/>
  19.  
  20. <GridView xmlns:android="http://schemas.android.com/apk/res/android"
  21. android:id="@+id/my_grid_view"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:clickable="true"/> <<--- I WANT THIS TO GET THE CLICK
  25. <View
  26. android:layout_width="0dp"
  27. android:layout_height="wrap_content"
  28. android:layout_weight="2"/>
  29. </LinearLayout>
  30.  
  31. <ListView xmlns:android="http://schemas.android.com/apk/res/android"
  32. android:id="@+id/listId"
  33. android:layout_weight="1"
  34. android:layout_width="match_parent"
  35. android:layout_height="0dp" />
  36. </LinearLayout>

GridCell在网格视图中是

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:padding="0dp" android:layout_margin="0dp"
  3. android:focusable="false"
  4. android:clickable="false"
  5. android:focusableInTouchMode="false"
  6. >
  7.  
  8. <TextView
  9. android:id="@+id/grid_item_number"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:paddingLeft="1dp"
  13. android:paddingRight="0dp"
  14. android:paddingTop="0dp"
  15. android:paddingBottom="0dp"
  16. android:textSize="10px"
  17. android:focusable="false"
  18. android:clickable="false"
  19. android:focusableInTouchMode="false"
  20. >
  21. </TextView>
  22.  
  23. <EditText xmlns:android="http://schemas.android.com/apk/res/android"
  24. android:id="@+id/grid_item_label"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:text="@+id/celllabel"
  28. android:background="@android:color/transparent"
  29. android:paddingLeft="5dp"
  30. android:paddingRight="0dp"
  31. android:paddingTop="0dp"
  32. android:paddingBottom="0dp"
  33. android:layout_margin="0dp"
  34. android:focusable="false"
  35. android:focusableInTouchMode="false"
  36. android:clickable="false"
  37. android:cursorVisible="false">
  38. </EditText>
  39.  
  40. </RelativeLayout>

适配器类有一个getView,如下所示

  1. public View getView(final int position,View convertView,ViewGroup parent) {
  2. LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  3.  
  4. View gridView;
  5.  
  6. MyObject obj = myObjects.get(position);
  7.  
  8. if (convertView == null) {
  9. gridView = inflater.inflate(R.layout.grid_cell,null);
  10.  
  11. String textColour = "#000000";
  12. TextView textView = (TextView) gridView.findViewById(R.id.grid_item_label);
  13. textView.setText(Html.fromHtml(String.format("<font color='%s'>%s</font>",textColour,obj.getValue())));
  14.  
  15. TextView superScriptTv = (TextView) gridView.findViewById(R.id.grid_item_number);
  16. if (obj.getNumber() > 0) {
  17. superScriptTv.setText(Html.fromHtml(String.format("<font>%s</font>",cell.getNumber())));
  18. }
  19.  
  20.  
  21. } else {
  22. gridView = convertView;
  23. }
  24.  
  25. gridView.setBackgroundColor(obj.getBackgroundColour());
  26.  
  27. return gridView;
  28. }

编辑
现在真的把我的头撞在墙上:)
我更新代码示例,所以有更多的数据.我注意到在我的适配器,如果我不设置文本在textview与ID = R.id.grid_item_number然后它的工作.一旦我设置文本,那么我失去了点击监听器.

链接的问题/答案没有帮助我看到.任何人都可以帮助我的愚蠢吗?

编辑
适配器代码添加.

提前致谢.

解决方法

问题在于row_cell中的EditText.当您点击该项目时,它会聚焦,并防止整个项目再次点击.你注意到,只有长时间点击的作品.
Here you have a similar problem.

为了解决这个问题,我将把OnItemClickListeners从Activity / Fragment移动到GridViewAdapter中,而不是:

  1. gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  2. public void onItemClick(AdapterView<?> parent,long id) {
  3. Log.d("ABCD","Position Single Click is " + position);
  4. // Ideally in here I want to put to open a soft keyboard for the user to enter a value
  5. // InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
  6. // imm.showSoftInput(gridView,InputMethodManager.SHOW_IMPLICIT);
  7. }
  8. });
  9.  
  10. gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
  11. @Override
  12. public boolean onItemLongClick(AdapterView<?> parent,long id) {
  13. Log.d("ABCD","Position Long Click is " + position);
  14. return true;
  15. }
  16. });

我会做这样的事情:

  1. @Override
  2. public View getView(int position,ViewGroup parent) {
  3.  
  4. LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  5.  
  6. View gridViewItem;
  7.  
  8. if (convertView == null) {
  9. gridViewItem = new View(mContext);
  10. gridViewItem = layoutInflater.inflate(R.layout.grid_cell,null);
  11. TextView textView = (TextView)gridViewItem.findViewById(R.id.grid_item_number);
  12. textView.setText(mValues[position]);
  13.  
  14. EditText editText = (EditText)gridViewItem.findViewById(R.id.grid_item_label);
  15.  
  16.  
  17.  
  18. } else {
  19. gridViewItem = (View) convertView;
  20. }
  21.  
  22. gridViewItem.setOnClickListener(new View.OnClickListener() {
  23. @Override
  24. public void onClick(View v) {
  25. Log.e("GRID","onClick: " );
  26. }
  27. });
  28.  
  29. gridViewItem.setOnLongClickListener(new View.OnLongClickListener() {
  30. @Override
  31. public boolean onLongClick(View v) {
  32. Log.e("GRID","onLongClick: " );
  33. return true;
  34. }
  35. });
  36.  
  37. return gridViewItem;
  38.  
  39. }

这将阻止你现在正在努力的这种奇怪的行为.

为了这个例子,请在下面找到我的代码

MainActivity布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:id="@+id/activity_main"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. tools:context="io.github.mmbs.gridviewcheck.MainActivity"
  10. tools:layout_editor_absoluteX="0dp"
  11. tools:layout_editor_absoluteY="0dp">
  12.  
  13. <GridView
  14. android:id="@+id/gridView"
  15. android:numColumns="auto_fit"
  16. android:columnWidth="100dp"
  17. android:stretchMode="columnWidth"
  18. android:layout_width="match_parent"
  19. android:layout_height="match_parent"
  20. android:clickable="true">
  21.  
  22. </GridView>
  23.  
  24. </android.support.constraint.ConstraintLayout>

网格单元布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:layout_margin="0dp"
  6. android:clickable="false"
  7. android:focusable="false"
  8. android:focusableInTouchMode="false"
  9. android:padding="8dp">
  10.  
  11. <TextView
  12. android:id="@+id/grid_item_number"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:paddingBottom="0dp"
  16. android:paddingLeft="1dp"
  17. android:paddingRight="0dp"
  18. android:paddingTop="0dp"
  19. android:textSize="20sp"
  20. android:text="TEXTVIEW">
  21. </TextView>
  22.  
  23. <EditText
  24. android:id="@+id/grid_item_label"
  25. android:layout_width="match_parent"
  26. android:layout_height="wrap_content"
  27. android:layout_margin="0dp"
  28. android:background="@android:color/transparent"
  29. android:cursorVisible="false"
  30. android:layout_below="@id/grid_item_number"
  31. android:text="EDITTEXT">
  32. </EditText>
  33.  
  34. </RelativeLayout>

请考虑到,我删除了所有布局属性负责对焦性.

MyGridAdapter:

  1. public class MyGridViewAdapter extends BaseAdapter {
  2.  
  3. private Context mContext;
  4. private final String[] mValues;
  5.  
  6. public MyGridViewAdapter(String[] values,Context context) {
  7. mValues = values;
  8. mContext = context;
  9. }
  10.  
  11. @Override
  12. public int getCount() {
  13. return mValues.length;
  14. }
  15.  
  16. @Override
  17. public Object getItem(int position) {
  18. return null;
  19. }
  20.  
  21. @Override
  22. public long getItemId(int position) {
  23. return position;
  24. }
  25.  
  26. @Override
  27. public View getView(int position,"onLongClick: " );
  28. return true;
  29. }
  30. });
  31.  
  32. return gridViewItem;
  33.  
  34. }
  35. }

如果你喜欢,我也可以在github上分享这个代码,所以你将有一个完全可操作的例子.

第二个选择是将您的实现保留为原样,并在EditText可对焦并启用时执行hacks操作.这里有相关主题

> Android: Force EditText to remove focus?
> ListView items focus behaviour
> Focusable EditText inside ListView
> Android: Force EditText to remove focus?

此外,请考虑that answer:

Do not use clickable objects in the grid. In that case Android cannot handle the click event of GridView.

Instead,use something to show a similar user interface view. Then handle that object’s click actions.

Don’t: put Button in the GridView to perform some click actions.

Do: put an ImageView instead of ImageButton and handle ImageView’s click events.

编辑:请查找link to the project on my Github.

猜你在找的Android相关文章