Implement scroll chart view in viewpager
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
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?
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) } } }
Not working with CandleStick probably with custom marker
@ArcherEmiya05 it doesn't not matter what chart type you use. Maybe your custom chart marker prevents chart from receiving a touch
@ArcherEmiya05 it doesn't not matter what chart type you use. Maybe your custom chart marker prevents chart from receiving a touch
Yes it does not matter which chart is being use since all of them extends ChartTouchListener, not sure why but in my case after implementing this and touching the chart it freezes and crash.
Hope it will be helpful. ViewPager2 LineChart for nested left and right scrolling `
onTouchListener = object : BarLineChartTouchListener(this, viewPortHandler.matrixTouch, 3f) {
private var initialX = 0f
private var initialY = 0f
val touchSlop = ViewConfiguration.get(context).scaledTouchSlop
private fun canScroll(delta: Float): Boolean {
val lineData = chart.data
val chart = chart
if (lineData == null) {
return false
}
val direction = -delta.sign.toInt()
return if (direction < 0) {
chart.lowestVisibleX > (lineData.xMin + 0.2)
} else {
chart.highestVisibleX < (lineData.xMax - 0.2)
}
}
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
var result = super.onTouch(v, event)
if (canScroll(-1f) || canScroll(1f)) {
parent.requestDisallowInterceptTouchEvent(true)
}
when (event?.action?.and(MotionEvent.ACTION_MASK)) {
MotionEvent.ACTION_DOWN -> {
initialX = event.x
initialY = event.y
}
MotionEvent.ACTION_MOVE -> {
val dx = event.x - initialX
val dy = event.y - initialY
val scaledDx = dx.absoluteValue * .5f
val scaledDy = dy.absoluteValue * 1f
if (scaledDx > touchSlop || scaledDy > touchSlop) {
if (scaledDy > scaledDx) {
parent.requestDisallowInterceptTouchEvent(false)
result = false
} else {
if (canScroll(dx)) {
parent.requestDisallowInterceptTouchEvent(true)
} else {
parent.requestDisallowInterceptTouchEvent(false)
result = false
}
}
}
}
else -> {}
}
return result
}
}
`