Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 622
Hanry Avatar asked Dec 05 '11 09:12

Hanry


People also ask

How to get data from ListView by clicking item on ListView?

Try this: my_listview. setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } });

How to edit ListView item in android?

OnItemClickListener MshowforItem = new AdapterView. OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ((TextView)view). setText("Hello"); } };


1 Answers

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.

like image 69
MariuszP Avatar answered Sep 23 '22 16:09

MariuszP