开发者

AbsListView Consumes touch events

I have a ViewFlipper with several GridViews in it. I want to be able to interact with the GridViews and swipe to switch from one grid to the other. The problem that I an running AbsListView is consuming the touch events by returning true no matter what it has done. I found this post that suggests that I try onInterceptTouchEvent() but that isn't working. Since I'm trying to use a GestureDetector it seems to not be able to track the touches correctly.

Using the method in that post I am using onInter开发者_JAVA技巧ceptTouchEvent() to print out the touches that are intercepted, but I am not getting MOTION_UP events consistently. I see a MOTION_DOWN, several MOTION_MOVE's and no MOTION_UP.


Alright, if anyone is looking for an answer to this here is the solution I came up with! The basic set up is that I have a FrameLayout that contains a ViewFlipper and I add GridViews to the ViewFlipper based on how many items I have to show. The I detect a swipe on the FrameLayout and switch between the GridViews in the ViewFlipper. I still need to be able to click items in the GridView so I can't just consume the events in the FrameLayout but the GridView consumes any events that it gets so I had to intercept the events before they got to the GridView and give them to my GestureDetector.

public class PagedGrid extends FrameLayout
{
    private Context _context;
    private ViewFlipper _flipper;
    private GestureDetector _gestureDetector;

    public PresentationsGrid(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);

        LayoutInflater.from(context).inflate(R.layout.presentation_grid, this, true);

        _flipper = (ViewFlipper)findViewById(R.id.flipper);
        _gestureDetector = new GestureDetector(new GestureListener());

        setFocusable(true);
        setFocusableInTouchMode(true);
        setClickable(true);

        _context = context;

        // ... Add Pages ...
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event)
    {
        return _gestureDetector.onTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        _gestureDetector.onTouchEvent(event);
        return false;
    }
}

Then I subclassed the GridView to keep track of its state and pass events back sensibly.

public class UnNomableGridView extends GridView
{
    private boolean _wasDown;

    public UnNomableGridView(Context context)
    {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        if(!_wasDown && event.getActionMasked() == MotionEvent.ACTION_DOWN)
        {
            super.onTouchEvent(event);
            _wasDown = true;
            return true;
        }
        else if(_wasDown && event.getActionMasked() == MotionEvent.ACTION_UP)
        {
            super.onTouchEvent(event);
            _wasDown = false;
            return true;
        }
        else if(_wasDown && event.getActionMasked() == MotionEvent.ACTION_CANCEL)
        {
            super.onTouchEvent(event);
            _wasDown = false;
            return true;
        }
        else
        {
            return false;
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜