Horizontal ListView in Android?

后端 未结 19 977
深忆病人
深忆病人 2020-11-22 03:54

Is it possible to make the ListView horizontally? I have done this using a gallery view, but the selected item comes to the center of the screen automatically.

19条回答
  •  心在旅途
    2020-11-22 04:42

    @Paul answer links to a great solution, but the code doesn't allow to use onClickListeners on items children (the callback functions are never called). I've been struggling for a while to find a solution and I've decided to post here what you need to modify in that code (in case somebody need it).

    Instead of overriding dispatchTouchEvent override onTouchEvent. Use the same code of dispatchTouchEvent and delete the method (you can read the difference between the two here http://developer.android.com/guide/topics/ui/ui-events.html#EventHandlers )

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean handled = mGesture.onTouchEvent(event);
        return handled;
    }
    

    Then, add the following code which will decide to steal the event from the item children and give it to our onTouchEvent, or let it be handled by them.

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch( ev.getActionMasked() ){
            case MotionEvent.ACTION_DOWN:
                 mInitialX = ev.getX();
                 mInitialY = ev.getY();             
                 return false;
            case MotionEvent.ACTION_MOVE:
                 float deltaX = Math.abs(ev.getX() - mInitialX);
                 float deltaY = Math.abs(ev.getY() - mInitialY);
                 return ( deltaX > 5 || deltaY > 5 );
            default:
                 return super.onInterceptTouchEvent(ev);
        }
    }
    

    Finally, don't forget to declare the variables in your class:

    private float mInitialX;
    private float mInitialY;
    

提交回复
热议问题