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

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

我的代码如下

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

任何指针都将不胜感激

谢谢

final GridView gridView = (GridView) findViewById(R.id.grid_view);
gridView.setNumColumns(numOfColumns);
gridView.getLayoutParams().width = (CELL_WIDTH * numOfColumns);
gridView.getLayoutParams().height = (CELL_WIDTH * numOfRows);

....

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent,View v,int position,long id) {
            Log.d("ABCD","Position Single Click is " + position);
            // Ideally in here I want to put to open a soft keyboard for the user to enter a value
            // InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            // imm.showSoftInput(gridView,InputMethodManager.SHOW_IMPLICIT);
        }
    });

 gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
      @Override
      public boolean onItemLongClick(AdapterView<?> parent,View view,long id) {
          Log.d("ABCD","Position Long Click is " + position);
          return true;
      }
 });

grid_view是

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
           xmlns:tools="http://schemas.android.com/tools"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:descendantFocusability="blocksDescendants"
           android:orientation="vertical">

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal">

    <View
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"/>

    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/my_grid_view"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:clickable="true"/>   <<--- I WANT THIS TO GET THE CLICK
    <View
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"/>
</LinearLayout>

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/listId"
          android:layout_weight="1"
          android:layout_width="match_parent"
          android:layout_height="0dp" />
</LinearLayout>

GridCell在网格视图中是

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:padding="0dp" android:layout_margin="0dp"
           android:focusable="false"
            android:clickable="false"
            android:focusableInTouchMode="false"
   >

   <TextView
        android:id="@+id/grid_item_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="1dp"
        android:paddingRight="0dp"
        android:paddingTop="0dp"
        android:paddingBottom="0dp"
        android:textSize="10px"
        android:focusable="false"
        android:clickable="false"
        android:focusableInTouchMode="false"
        >
</TextView>

<EditText xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/grid_item_label"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@+id/celllabel"
          android:background="@android:color/transparent"
          android:paddingLeft="5dp"
          android:paddingRight="0dp"
          android:paddingTop="0dp"
          android:paddingBottom="0dp"
          android:layout_margin="0dp"
          android:focusable="false"
          android:focusableInTouchMode="false"
          android:clickable="false"
          android:cursorVisible="false">
</EditText>

</RelativeLayout>

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

public View getView(final int position,View convertView,ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridView;

    MyObject obj = myObjects.get(position);

    if (convertView == null) {
        gridView = inflater.inflate(R.layout.grid_cell,null);

        String textColour = "#000000";
        TextView textView = (TextView) gridView.findViewById(R.id.grid_item_label);
        textView.setText(Html.fromHtml(String.format("<font color='%s'>%s</font>",textColour,obj.getValue())));

        TextView superScriptTv = (TextView) gridView.findViewById(R.id.grid_item_number);
        if (obj.getNumber() > 0) {
            superScriptTv.setText(Html.fromHtml(String.format("<font>%s</font>",cell.getNumber())));
        }


    } else {
        gridView = convertView;
    }

    gridView.setBackgroundColor(obj.getBackgroundColour());

    return gridView;
}

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

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

编辑
适配器代码添加.

提前致谢.

解决方法

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

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

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  public void onItemClick(AdapterView<?> parent,long id) {
        Log.d("ABCD","Position Single Click is " + position);
        // Ideally in here I want to put to open a soft keyboard for the user to enter a value
        // InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        // imm.showSoftInput(gridView,InputMethodManager.SHOW_IMPLICIT);
    }
});

gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
  @Override
  public boolean onItemLongClick(AdapterView<?> parent,long id) {
      Log.d("ABCD","Position Long Click is " + position);
      return true;
  }
});

我会做这样的事情:

@Override
public View getView(int position,ViewGroup parent) {

    LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridViewItem;

    if (convertView == null) {
        gridViewItem = new View(mContext);
        gridViewItem = layoutInflater.inflate(R.layout.grid_cell,null);
        TextView textView = (TextView)gridViewItem.findViewById(R.id.grid_item_number);
        textView.setText(mValues[position]);

        EditText editText = (EditText)gridViewItem.findViewById(R.id.grid_item_label);



    } else {
        gridViewItem = (View) convertView;
    }

    gridViewItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.e("GRID","onClick: " );
        }
    });

    gridViewItem.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Log.e("GRID","onLongClick: " );
            return true;
        }
    });

    return gridViewItem;

}

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

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

MainActivity布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="io.github.mmbs.gridviewcheck.MainActivity"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">

<GridView
    android:id="@+id/gridView"
    android:numColumns="auto_fit"
    android:columnWidth="100dp"
    android:stretchMode="columnWidth"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true">

</GridView>

</android.support.constraint.ConstraintLayout>

网格单元布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0dp"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:padding="8dp">

<TextView
    android:id="@+id/grid_item_number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="0dp"
    android:paddingLeft="1dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    android:textSize="20sp"
    android:text="TEXTVIEW">
</TextView>

<EditText
          android:id="@+id/grid_item_label"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_margin="0dp"
          android:background="@android:color/transparent"
          android:cursorVisible="false"
          android:layout_below="@id/grid_item_number"
          android:text="EDITTEXT">
</EditText>

</RelativeLayout>

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

MyGridAdapter:

public class MyGridViewAdapter extends BaseAdapter {

private Context mContext;
private final String[] mValues;

public MyGridViewAdapter(String[] values,Context context) {
    mValues = values;
    mContext = context;
}

@Override
public int getCount() {
    return mValues.length;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position,"onLongClick: " );
            return true;
        }
    });

    return gridViewItem;

}
}

如果你喜欢,我也可以在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.

原文链接:https://www.f2er.com/android/312769.html

猜你在找的Android相关文章