开发者

Android fires both onclick and onlongclick on a short click

People thank you all for your thoughts. I feel quite ashamed right now, the problem was nog with the listeners, they send a message to a handler that uses a switch. One simple break statement was missing there causing the program to behave the way it did. Thanks again for your time.

i'm making an adapter class that stores quick trips possibilities for traveling via a train. This is implemented in a list. The list contains a delete button. If that one is pressed it is removed from the list. If something else is touched it should open another window, but if long pressed it should open a dialog to edit the current settings.

The problem is that on a short click both the short and long click listeners fire. If I press long only the long click is fired.

Does anyone have any suggestions/tips/answers? the code is:

public View getView(int position, View convertView, ViewGroup vie开发者_StackOverflowwGroup) {
QuickTrips entry = quickTripList.get(position);

if(convertView == null) {
  LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  convertView = inflater.inflate(R.layout.quick_trip_list, null);
}

convertView.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
    if(handler != null) {
      QuickTrips entry = (QuickTrips) v.getTag();
      Message msg = Message.obtain();
      msg.what = LAUNCH_QUICK_TRIP;
      msg.obj = entry;
      handler.sendMessage(msg);
    }
  }
});

convertView.setOnLongClickListener(new View.OnLongClickListener() {
  @Override
  public boolean onLongClick(View v) {
    QuickTrips entry = (QuickTrips) v.getTag();
    Message msg = Message.obtain();
    msg.what = EDIT_QUICK_TRIP;
    msg.obj = entry;
    handler.sendMessage(msg);
    return true;
  }
});

convertView.setTag(entry);

TextView text = (TextView) convertView.findViewById(R.id.text);
text.setText(entry.getFromTo());

Button remove = (Button) convertView.findViewById(R.id.remove);
remove.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
    QuickTrips entry = (QuickTrips) v.getTag();
    quickTripList.remove(entry);
    notifyDataSetChanged();
  }
});
remove.setTag(entry);

return convertView;

}

Well i tried removing the View.on... by importing it but without succes. Now i tried the onTouchListener... the problem is it only gets called on an event. To start using timers is more work then it should be, shouldn't it? I just don't understand why long click is fired on a short click

the code from the ontouchevent:

    convertView.setOnTouchListener(new OnTouchListener() {
  @Override
  public boolean onTouch(View v, MotionEvent event) {
    long downTime = event.getDownTime();
    long pressedTime = event.getEventTime();
    if(DEBUG) Log.i("qt", downTime + " en " + pressedTime);

    if(event.getAction() == MotionEvent.ACTION_DOWN) {
      if(DEBUG) Log.i("qt", "key down");
    }
    else if(event.getAction() == MotionEvent.ACTION_UP) {
      if(DEBUG) Log.i("qt", "key up");
      return true;
    } 
    else {
      if(DEBUG) Log.i("qt", "else"); //never called
    }

    return true; //if false then action up isn't called
  }
});


try:

convertView.setOnLongClickListener(new OnLongClickListener() { 
        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            return true;
        }
    });

extracted from here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜