views:

1417

answers:

1

I am using table layout to display data, but i want it to behave like list items (ability to select, when select change background, when click, having hover effect, click able) for that purpose i am using following listeners

  • OnClickListener(to perform action)
  • OnFocusChangeListener(To change background color)
  • OnTouchListener(to focus specific row)

Now problem is when user touch any item it get focus first and then have to touch again to fire onclick event, to fix this i made a change and ontouch i fire action on specific to row.

@Override
public boolean onTouch(View v, MotionEvent event) {
 if (event.getAction() != MotionEvent.ACTION_UP){
  v.requestFocus();
  int viewId= v.getId();
  handleEvent(viewId);//Switch cases to perform row specific actions.
 }
 return false;
}

now if user touch the row event get fired that works perfect, But one more problem rises here when even user want to scroll down the data via dragging finger onto the screen ontouch event get fired.... and action automatically performed although user think it will scroll down the screen.

+1  A: 

Hi!
I don't know if this solution is applicable in your case, but maybe you can do like this: Let's assume that user clicked your item (so probably you will get sequence of three events to your OnTouch() method: ACTION_ DOWN, ACTION_ MOVE (not necessarily) and ACTION_UP. Now you can react accordingly.

  1. If it's ACTION_DOWN, you can save x and y coordinates.
  2. If it's ACTION_ MOVE, take its x and y and calculate the distance from corresponding ACTION_DOWN. If it's longer than some assumed value, then make the scroll and set the flag indicating that items were scrolled.
  3. If it's ACTION_UP check your flag. If items were not scrolled, fire your action and clear the flag.

Probably calculations is not what you should do in ACTION_MOVE event, because it should be fast, but give it a try.
Regards!

Ramps