列表视图中项目的一半未调用OnItemLongClickListener

时间:2013-01-02 14:08:40

标签: android android-listview

我有一个带自定义适配器的列表视图,

public class ClueArrayAdapter extends ArrayAdapter<String> {

---
----

@Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
---
---
return rowView;
}

的RowLayout


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView 
        android:id="@+id/clue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        />

     <com.mydomain.MyView 
        android:id="@+id/myView"    // MyView is custom view and overrides onTouchEvent
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        />

</LinearLayout>

我有一个onItemLongClickListener

listView.setOnItemLongClickListener(new OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> parent, View view,
                int position, long id) {
 ---
view.setSelected(true);
}

当长时间单击TextView但没有响应长按myView时,将调用此侦听器。

我是否需要在rowlayout中设置myView的一些xml属性,以便选择一行来响应对myView的长按?

触摸myView

 @Override
        public boolean onTouchEvent(MotionEvent ev) {

            final int action = ev.getAction();
            switch (action & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN: {
                    final float x = ev.getX();
                    final float y = ev.getY();
                    markedCell = getCellAt(x,y);
                    break;       
                }

            }
            return true;
        }

3 个答案:

答案 0 :(得分:1)

从onTouchEvent返回false。 返回true表示您处理了该事件,并且不应将其传递给下一个处理程序(在本例中为您的长触摸事件)。

答案 1 :(得分:0)

您可以在getView方法中设置点击监听器。

       rowView.setOnClickListener(new View.onClickListener)
          {
            public void onclick()
             { //your code
             }
          });

答案 2 :(得分:0)

试试这个:

public class ClueArrayAdapter extends ArrayAdapter<String> {
---
----

@Override
  public View getView(int position, View convertView, ViewGroup parent) {
  LayoutInflater inflater = (LayoutInflater) context
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
  rowView.setOnItemLongClickListener(new OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> parent, View view,
            int position, long id) {
         ---
  rowView.setSelected(true);
}

 ---
 ---
return rowView;

}