Search code examples
javaandroidmpandroidcharthorizontalscrollview

Switching between HorizontalScrollView and BarChart scroll


I have a MPAndroidChart BarChart which is placed inside a HorizontalScrollView. Now, I have multiple bars in the chart view and also the chart width is fixed as per my requirement. Since, there is a default scroll behaviour of chart, I can see all the bars.

The only problem is the scroll behaviour is not smooth and it lags a lot.

My idea is to disable the HorizontalScrollView once I start scrolling the chart and enable it back when I touch outside the chart. Can someone please tell me how I can do it? I hope my question is clear enough and there is no need to share any XML for it. Thanks in advance.


Solution

  • I faced similar issue while using Mpchart inside horizontal scroll view. I had a workaround with MpChart setOnChartGestureListener.

    barChart.setOnChartGestureListener(new OnChartGestureListener() {
            @Override
            public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
                horizontalScrollView.requestDisallowInterceptTouchEvent(true);
            }
    
            @Override
            public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
                horizontalScrollView.requestDisallowInterceptTouchEvent(false);
            }
    
            //.....//
    
            @Override
            public void onChartTranslate(MotionEvent me, float dX, float dY) {
                Log.i("GESTURE", "onChartTranslate");
                if(barChart.getLowestVisibleX() == barChart.getXAxis().getAxisMinimum() || barChart.getHighestVisibleX() == barChart.getXAxis().getAxisMaximum()) {
                    horizontalScrollView.requestDisallowInterceptTouchEvent(false);
                } else {
                    horizontalScrollView.requestDisallowInterceptTouchEvent(true);
                }
            }
        });
    

    the horizontalScrollView object is where the chart reside.