【问题标题】:CardView not clickableCardView 不可点击
【发布时间】:2019-05-06 09:54:56
【问题描述】:

在 MainActivity 中,我有一个饼图,其中库来自 GitHub - PhilJay/MPAndroidChart: A powerful Android chart view。我将饼图添加到cardView。单击cardView 时,假设会显示toast。但是onClickListener 根本不起作用。

MainActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        showChart()

        cardView.setOnClickListener {
            Toast.makeText(application,"clicked",Toast.LENGTH_LONG).show()
        }
    }

    private fun showChart() {

        chart.setUsePercentValues(true)
        chart.description.isEnabled = false
        chart.center
        chart.extraRightOffset = 20f

        chart.isDrawHoleEnabled = true
        chart.setHoleColor(Color.WHITE)

        chart.setTransparentCircleColor(Color.WHITE)
        chart.setTransparentCircleAlpha(10)

        chart.holeRadius = 58f
        chart.transparentCircleRadius = 46f

        chart.setDrawCenterText(true)

        chart.rotationAngle = 0f
        chart.isRotationEnabled = true
        chart.animateY(1400, Easing.EaseInOutQuad)

        val l = chart.legend
        l.verticalAlignment = Legend.LegendVerticalAlignment.CENTER
        l.horizontalAlignment = Legend.LegendHorizontalAlignment.LEFT
        l.orientation = Legend.LegendOrientation.VERTICAL
        l.setDrawInside(false)
        l.xOffset = 50f

        chart.setEntryLabelColor(Color.WHITE)
        chart.setEntryLabelTextSize(12f)
        setData(3,5)
    }

    private fun setData(numOpen: Int, numInProgress: Int) {
        val entries = ArrayList<PieEntry>()
        val colors = ArrayList<Int>()

        if (numOpen > 0) {
            entries.add(PieEntry(numOpen.toFloat(), "Open"))
            colors.add(ColorTemplate.rgb("#F44336"))

        }
        if (numInProgress > 0) {
            entries.add(PieEntry(numInProgress.toFloat(), "Progress"))
            colors.add(ColorTemplate.rgb("#2196F3"))
        }

        val dataSet = PieDataSet(entries, "Status")

        dataSet.sliceSpace = 3f
        dataSet.iconsOffset = MPPointF(0f, 40f)
        dataSet.selectionShift = 5f

        dataSet.colors = colors


        val data = PieData(dataSet)
        data.setValueFormatter(PercentFormatter(chart))
        data.setValueTextSize(11f)
        data.setValueTextColor(Color.WHITE)
        chart.data = data

        // undo all highlights
        chart.highlightValues(null)
        chart.invalidate()
    }
}

ma​​in_activity

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

        <android.support.v7.widget.CardView
                android:id="@+id/cardView"
                android:clickable="true"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="15dp"
                android:layout_marginRight="15dp">

            <com.github.mikephil.charting.charts.PieChart
                    android:background="@color/colorPrimary"
                    android:id="@+id/chart"
                    android:layout_width="match_parent"
                    android:layout_height="250dp"
                    android:layout_gravity="center_horizontal|center_vertical"/>

        </android.support.v7.widget.CardView>
    </LinearLayout>
</ScrollView>

图片

【问题讨论】:

  • 能不能评论一下showChart(),然后看看是否可以点击?如果空卡片视图是可点击的,那么您的图表点击可能会停止卡片视图点击。另一种方法是让LinearLayout Clickable,我想你希望整个视图都可以点击吧?
  • @TheAnkush 问题已解决。谢谢

标签: android kotlin pie-chart cardview


【解决方案1】:

由于PieChart 完全吸收click 与图表元素交互,因此不会调用父级中定义的点击监听器。即使您将OnClickListener 设置为PieChart,它也不起作用。这个问题的解决方法是将onChartGestureListener设置为PieChart,并从onChartSingleTapped调用你的方法。

示例

override fun onCreate(savedInstanceState: Bundle?) {
    ...
    cardView.setOnClickListener {
        doThis();
    }

    chart.onChartGestureListener = object : OnChartGestureListener {
        override fun onChartSingleTapped(me: MotionEvent?) {
            doThis()
        }
    }
}

fun doThis(){
    // your code goes here
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-05
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多