Hanry
Hanry

Reputation: 5531

Getting row position of listview on swipe

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.

Upvotes: 5

Views: 9145

Answers (4)

MariuszP
MariuszP

Reputation: 136

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.

Upvotes: 3

MKJParekh
MKJParekh

Reputation: 34301

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.

Upvotes: 1

om252345
om252345

Reputation: 2415

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

Upvotes: 1

brian
brian

Reputation: 6912

private int position = -1;
@Override
protected void onListItemClick(ListView l, View v, int index, long id) {
position = index;
}

like this?

Upvotes: 0

Related Questions