1

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

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;
}

行布局


<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;
        }
4

3 回答 3

1

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

于 2013-02-01T17:48:10.140 回答
0

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

       rowView.setOnClickListener(new View.onClickListener)
          {
            public void onclick()
             { //your code
             }
          });
于 2013-01-02T14:48:18.927 回答
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;

}

于 2013-01-02T14:49:56.640 回答