Mpandroidchart: Implement scroll chart view in viewpager

Created on 24 Oct 2017  路  3Comments  路  Source: PhilJay/MPAndroidChart

If chart exist in viewpager, when scroll in chart will scroll viewpager, very hard scroll chart. I found solution at https://github.com/PhilJay/MPAndroidChart/issues/1885 by Modify BarLineChartTouchListener.java. Add line mChart.disableScroll() after action MOVE and remove line mChart.disableScroll()
_if (mTouchMode == DRAG) {
//mChart.disableScroll()
....
}
else if (mTouchMode == X_ZOOM || mTouchMode == Y_ZOOM || mTouchMode == PINCH_ZOOM) {
//mChart.disableScroll();
....
}_
Awsome, i can scroll chart is smooth, not confused with scroll of viewpager

Most helpful comment

For those how are looking for a working chart inside ViewPager. Here is how I done it in Kotlin. It will work for classes that use BarLineChartTouchListener.

chart.onTouchListener = object : BarLineChartTouchListener(chart, chart.viewPortHandler.matrixTouch, 3F) {
            override fun onTouch(v: View, event: MotionEvent): Boolean {
                return when (event.action) {
                    MotionEvent.ACTION_DOWN -> {
                        v.parent.requestDisallowInterceptTouchEvent(true)
                        super.onTouch(v, event)
                    }
                    MotionEvent.ACTION_UP -> {
                        v.parent.requestDisallowInterceptTouchEvent(false)
                        super.onTouch(v, event)
                    }
                    MotionEvent.ACTION_MOVE -> {
                        v.parent.requestDisallowInterceptTouchEvent(true)
                        super.onTouch(v, event)
                    }
                    else -> super.onTouch(v, event)
                }
            }
        }

All 3 comments

How can you modify a protected class?

For those how are looking for a working chart inside ViewPager. Here is how I done it in Kotlin. It will work for classes that use BarLineChartTouchListener.

chart.onTouchListener = object : BarLineChartTouchListener(chart, chart.viewPortHandler.matrixTouch, 3F) {
            override fun onTouch(v: View, event: MotionEvent): Boolean {
                return when (event.action) {
                    MotionEvent.ACTION_DOWN -> {
                        v.parent.requestDisallowInterceptTouchEvent(true)
                        super.onTouch(v, event)
                    }
                    MotionEvent.ACTION_UP -> {
                        v.parent.requestDisallowInterceptTouchEvent(false)
                        super.onTouch(v, event)
                    }
                    MotionEvent.ACTION_MOVE -> {
                        v.parent.requestDisallowInterceptTouchEvent(true)
                        super.onTouch(v, event)
                    }
                    else -> super.onTouch(v, event)
                }
            }
        }

@thanhcly920 How did you modify that file?

Was this page helpful?
0 / 5 - 0 ratings