2

i've a listview in which the users can "swipe" to delete item from the listview. But i want to have the possibility to see "details" of this item. I want to call a new activity when the user "click" on an item.

I use this to allow the swipe to delete :

View.OnTouchListener mTouchListener = new View.OnTouchListener()

And so i'm using it with the adapter :

    mAdapter = new TestAdapter(getActivity(),
            R.layout.layout_test, list, mTouchListener);
    listV.setAdapter(mAdapter);

Before to use that, i used onItemClick to get the position of the item choosed.

But how can i get the POSITION without the function onItemClick ?

deveLost
  • 1,151
  • 2
  • 20
  • 47

2 Answers2

2

To know the position in an ArrayAdapter you can use the adapter.getPosition(view) http://developer.android.com/reference/android/widget/ArrayAdapter.html#getPosition(T).

You can use a TouchListener with a onListItemClick in your adapter to handle the swipe action and the click action (to open a detail for example).

Also using a TouchListener you can use something similar to Roman Nurik code to retrieve the position by the view clicked.

https://github.com/romannurik/Android-SwipeToDismiss/blob/master/src/com/example/android/swipedismiss/SwipeDismissListViewTouchListener.java

// Find the child view that was touched (perform a hit test)
Rect rect = new Rect();
int childCount = mListView.getChildCount();
int[] listViewCoords = new int[2];
mListView.getLocationOnScreen(listViewCoords);
int x = (int) motionEvent.getRawX() - listViewCoords[0];
int y = (int) motionEvent.getRawY() - listViewCoords[1];
View child;
for (int i = 0; i < childCount; i++) {
    child = mListView.getChildAt(i);
    child.getHitRect(rect);
    if (rect.contains(x, y)) {
         mDownView = child;
         break;
    }
}

if (mDownView != null) {
    mDownX = motionEvent.getRawX();
    mDownY = motionEvent.getRawY();
    mDownPosition = mListView.getPositionForView(mDownView);
 }
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
1

1) You should create your custom Adapter for ListView.
2) In getView method of this Adapter you should create OnTouchListener for each item (Layout or rootView).

Community
  • 1
  • 1
Volodymyr Kulyk
  • 6,455
  • 3
  • 36
  • 63
  • Mhh, i've a custom adapter, thx for the advice, i didn't create a OnTouchListener for each item, but i set row.setId(position); in my custom adapter, and i get it with view.getId() in my only function OnTouchListener. thx ! – deveLost Jun 16 '14 at 20:19