首页 > 解决方案 > GraphView 折线图未在可折叠视图中显示数据点

问题描述

我正在尝试在可折叠视图中生成折线图,但是当我运行模拟器时,该图不显示任何数据。我不确定我想要实现的目标是否可行,但我知道我为图表编写的代码确实可以在不包含可折叠视图的布局中工作。关于可能发生的事情有什么建议吗?您可以在单击 headerOne 时看到问题,单击它会展开一个隐藏视图,然后会显示一个图表和 TextView,但该图表不会显示我创建的任何数据。

链接到 repo(工作分支:调试):https ://github.com/irenenserrano/DREUProject/tree/debugging

活动主.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <!-- Base CardView -->
    <androidx.cardview.widget.CardView
        android:id="@+id/base_cardview"
        style="@style/Base.CardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.473"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.021">

        <!-- ConstraintLayout for the entire CardView, including the expandable portion -->
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="@+id/base_cardview"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.511"
            tools:layout_editor_absoluteX="-55dp">

            <!--This is a ConstraintLayout for the fixed portion
                of the CardView. The elements
                that lie within the fixed portion of the CardView
                can be constrained to this layout.-->
            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/fixed_layout"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.0">

                <TextView
                    android:id="@+id/header_one"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:clickable="true"
                    android:text="@string/header_one"
                    android:textSize="40sp" />
            </androidx.constraintlayout.widget.ConstraintLayout>

            <!-- The following is the expandable portion whose
                visibility is initially set to 'gone'.
                The parent LinearLayout contains 3 child LinearLayouts
                that hold a subject name and an icon each.-->
            <LinearLayout
                android:id="@+id/hidden_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:visibility="gone"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/fixed_layout">

                <com.jjoe64.graphview.GraphView
                    android:id="@+id/line_chart"
                    android:layout_width="wrap_content"
                    android:layout_height="400dp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:lineSpacingMultiplier="1.2"
                    android:text="@string/text" />
            </LinearLayout>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>

测试图:

class TesterGraphs: Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // CODE FOR LINE GRAPH
        // create a new data series
        val series = LineGraphSeries<DataPoint>()
        // link activity to layout view
        val graph: GraphView = findViewById(R.id.line_chart)

        // append data to series
        var x: Double = 0.0
        var y: Double
        for (i in 1..25) {
            x = x + 1.0
            y = 2*x
            series.appendData(DataPoint(x, y1), true, 26)
        }

        // add series to graph
        graph.addSeries(series)

        //set max x range
        graph.viewport.setMaxX(25.0)
        graph.viewport.isXAxisBoundsManual = true

主要活动

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val cardView: CardView = findViewById(R.id.base_cardview)
        val hiddenView: LinearLayout = findViewById(R.id.hidden_view)
        val headerOne: TextView = findViewById(R.id.header_one)

        // Add a click listener to header so that view can expand and collapse
        // Original start value of hiddenView.visibility is gone
        headerOne.setOnClickListener {
            // If the CardView is already expanded, set its visibility to gone
            // If it is not expanded, set its visibility to visible
            if (hiddenView.visibility == View.VISIBLE) {
                TransitionManager.beginDelayedTransition(cardView, AutoTransition())
                hiddenView.visibility = View.GONE
            } else {
                TransitionManager.beginDelayedTransition(cardView, AutoTransition())
                hiddenView.visibility = View.VISIBLE
            }
        }

标签: androidandroid-graphview

解决方案


推荐阅读