onTouchEvent in a BaseAdapter Android

2019-08-28 17:01发布

问题:

In my program I must insert this onTouchEvent method in a BaseAdapter component:

    public class FlipAdapter extends BaseAdapter {

        ...           
    public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {

    case MotionEvent.ACTION_DOWN: {
        startY = event.getY();
        break;
    }

    case MotionEvent.ACTION_UP: {
        float endY = event.getY();

        if (endY > startY) {
            layoutComments.startAnimation(animDown);
            layoutComments.setVisibility(View.GONE);
        }

    }

    }
    return true;
}

This function works in an Activity (it's right) but in this component it's not works. I think that the problem is that the layoutComments is inserted in a ViewHolder, but I can't pass the view to the onTouchEvent. Any ideas?

回答1:

Follow Like this:

 public class YourClassName extends Activity
{

   @Override
     protected void onCreate(Bundle savedInstanceState) 
       {
        .......
        } 

    class FlipAdapter extends BaseAdapter
    {
            //Now you can access everything from the activity.
    } 
 } 

Hope this will help you.