5

I know this type of question is asked before here. I have tried both gesture detector :

 wholelistview.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        });

which detects only swipe(cannot get row position here).

similarly in getview:

public View getView(int position, View convertView, ViewGroup parent) {
//other stuff

convertview.setOnTouchListener(same as above);

} 

but cannot detect swipe.

Any solution for this ??? I want only the row position on which swipe is made.

Community
  • 1
  • 1
Hanry
  • 5,481
  • 2
  • 40
  • 53
  • 1
    http://stackoverflow.com/questions/4373485/android-swipe-on-list anyways..Happy Birthday To You! – MKJParekh Dec 06 '11 at 06:58
  • @Frankenstein I have seen this already.But how can i get row position from this. – Hanry Dec 06 '11 at 09:03
  • I don't know exactlly but using this http://software-workshop.eu/content/swiping-listview-elements you can do swipe right? there you are getting View v as argument can't you do getTag there to know the view's tag and set it during your list createion getView() method do setTag() there..this way using set and getTag() you might can achieve this..try – MKJParekh Dec 06 '11 at 10:01

4 Answers4

3

hanry what kind of errors do you get? I've implemented it and it works perectly. But instead of storing the position of the element I'm storing the view holder that among the others also have the model property. Remember that you have to check if the convertview isn't null. Take a look at the code:

public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;

    Model m = filter.subItems.get(position);
    if(m != null)
    {
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.rowlayout, null);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.position = position; - here you can put your position.
            view.setOnTouchListener(this.listener);
            //assign whatever you like to the viewHolder - in most cases the model and inlated controls and then assign 
        } else {
            view = convertView;
        }
        view.setTag(viewHolder);
    }

and then

public boolean onTouch(View v, MotionEvent event) {
    ViewHolder viewHolder = ((ViewHolder) v.getTag());
}

hop that helps. ps: If you don't know anything about view holders and tags I suugest to see there. And my approach has been described in the link that Frankenstein gave.

MariuszP
  • 136
  • 4
  • on which I should implement ontouchlistener full listview or convertview???? as i cannot detect touch/swipe – Hanry Dec 07 '11 at 10:07
  • I'm attaching to the view in the Adapter. view.setOnTouchListener(this.listener); I'm doing it only if the converview is null, after inflating new view. – MariuszP Dec 07 '11 at 16:04
  • +1 i m getting row position on touch but **(gestureDetector.onTouchEvent(event))** is not called. – Hanry Dec 09 '11 at 11:25
  • I've had some issues with the gesturedetector as well, that's why I stopped using it. – MariuszP Dec 12 '11 at 09:09
1

To get row position for your view, get position of view passed by onTouch for your listview by this function

wholelistview.setOnTouchListener(new View.OnTouchListener() {         
 public boolean onTouch(View v, MotionEvent event) {                 
 if (gestureDetector.onTouchEvent(event)) {
    int position =  indexOfChild(View v);

     return true;                 
     }                 
     return false;             
                  }         }); 

It returns index of child. http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.5_r1/android/view/ViewGroup.java#ViewGroup.indexOfChild%28android.view.View%29

om252345
  • 2,395
  • 4
  • 29
  • 31
  • can you explain it more as i can't use indexofchild it says:Multiple markers at this line - View cannot be resolved to a variable - Syntax error on token "v", delete this – Hanry Dec 05 '11 at 10:12
  • I have not tried it, and right now can not try this... I will try and tell you once @ home – om252345 Dec 05 '11 at 10:33
1

you can do getTag and setTag to know the tag of the view and using that you can decide which row has been swiped

public View getView(int position, View convertView, ViewGroup parent) { 
//other stuff 
     convertview.setTag(position);
}  

and for swipe from the link

 public boolean onTouch(View v, MotionEvent event) {
     int pos = (Integer)v.getTag();
     Log.i(TAG,pos+" is swiped");
 }

I didn't Implemented this..so you will have to test for error..but According to me this can be a good way to achieve what you want to do.

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
0
private int position = -1;
@Override
protected void onListItemClick(ListView l, View v, int index, long id) {
position = index;
}

like this?

brian
  • 6,802
  • 29
  • 83
  • 124