Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find end of scroll value for HorizontalScrollView

i need to implement following scenario,

here i have two horizontalScrollViews the upper scrollView is a main menu and lower scrollView is a submenu.

when i scroll main menu,the menu which comes under center arrow will show its submenu in lower scrollview and from the lower scrollview, the submenu which comes under center arrow shows the screen for that sub-menu.

here's my requirement:enter image description here

i've implemented it using HorizontalScrollViews and ViewFlipper and also it works but it will give correct result only when i scroll it slowly and not when scroll fast.

i've used onTouch() method with a ACTION_UP event,so when i release finger from screen it will gives me getScrollX() position at that point but here i need getScrollX() position when scroll is finished/stop.

here's my code:-

  @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (v.getId()) {

            case R.id.mHorizontalScrollViewMain:
           if (event.getAction() == KeyEvent.ACTION_UP) {
            Log.d("test", " " + hsvUpperTab.getScrollX() + " , "+ mViewFlipper.getChildCount());
            getUpperTabScrolled(hsvUpperTab.getScrollX());
        }
        break;

        case R.id.mHorizontalScrollViewSub:
                if (event.getAction() == KeyEvent.ACTION_UP) {
            Log.d("test1", " " + hsvLowerTab.getScrollX());
            getLowerTabScrolled(hsvLowerTab.getScrollX());

            }

        default:
            break;

        }

        return false;
    }
like image 229
NaserShaikh Avatar asked Sep 03 '12 06:09

NaserShaikh


1 Answers

Here's my answer:

public class ScrollViewCustom extends HorizontalScrollView {
    private Runnable scrollerTask;
    private int intitPosition;
    private int newCheck = 100;
    private static final String TAG = "ScrollView";

    public interface onScrollStopListner{
        void onScrollStoped();
    }

    private onScrollStopListner onScrollstopListner;

    public ScrollViewCustom(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
            scrollerTask = new Runnable() {
                @Override
                public void run() {
                // TODO Auto-generated method stub
                    int newPosition = getScrollX();
                        if ( intitPosition - newPosition == 0 ) {
                            if ( onScrollstopListner != null ) {
                            onScrollstopListner.onScrollStoped();
                            }
                            }else{
                            intitPosition = getScrollX();
                            ScrollViewCustom.this.postDelayed(scrollerTask, newCheck);
                        }
                    }
        };
    }

    public void setOnScrollStopListner( ScrollViewCustom.onScrollStopListner listner ){
        onScrollstopListner = listner;
    }

    public void startScrollerTask(){
        intitPosition = getScrollX();
        ScrollViewCustom.this.postDelayed(scrollerTask, newCheck);
    }

}

Here's in Activity:

scroll.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
                        if ( event.getAction() == MotionEvent.ACTION_UP ) {
                            scroll.startScrollerTask();
                        }
                        return false;
            }
        });



        scroll.setOnScrollStopListner(new onScrollStopListner() {
                @Override
                public void onScrollStoped() {
                    // TODO Auto-generated method stub
                    Log.d(TAG," "+scroll.getScrollX());

                    }

        });
like image 136
NaserShaikh Avatar answered Oct 25 '22 11:10

NaserShaikh