Как переключить видимость различных TextView в одном и том же упражнении - PullRequest
0 голосов
/ 16 февраля 2020

У меня есть 3 карты, которые отображаются на экране моего приложения. Когда пользователь нажимает на карту, карта должна развернуться, чтобы показать текст. Я хочу добиться этого, переключая видимость текстового представления на карте, на которую нажали. Самая первая карта на странице работает должным образом, но ни одна из других карт не работает должным образом. Как мне добиться желаемого поведения?

вот мой kotlin класс:

private lateinit var thruHist: TextView
private lateinit var esHist: TextView
private lateinit var bayHist: TextView

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // this tells the app which layout to associate with this class
    setContentView(R.layout.activity_workhistory)

    // now connect each view to a variable for manipulation
    thruHist = findViewById(R.id.thruHistory)
    esHist = findViewById(R.id.esHistory)
    bayHist = findViewById(R.id.bayHistory)
}

fun openBay(view: View) {
    if (bayHist.visibility === View.VISIBLE) {
        bayHist.visibility = View.GONE
    } else bayHist.visibility = View.VISIBLE
}

fun openEs(view: View) {
    if (esHist.visibility === View.VISIBLE) {
        esHist.visibility = View.GONE
    } else esHist.visibility = View.VISIBLE
}

fun openThru(view: View) {
    if (thruHist.visibility === View.VISIBLE) {
        thruHist.visibility = View.GONE
    } else thruHist.visibility = View.VISIBLE
}

Вот мой макет контента XML файл:

    <androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:layout_margin="15dp"
    android:onClick="openBay"
    app:cardElevation="2dp"
    app:cardCornerRadius="8dp"
    >
          <TextView
            android:id="@+id/bayHistory"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:text="Worked here blabbity blah blah" />
 </androidx.cardview.widget.CardView>

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:layout_margin="15dp"
    android:onClick="openEs"
    app:cardElevation="2dp"
    app:cardCornerRadius="8dp"
    >
          <TextView
            android:id="@+id/esHistory"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:text="Worked here blabbity blah blah" />
 </androidx.cardview.widget.CardView>

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:layout_margin="15dp"
    android:onClick="openThru"
    app:cardElevation="2dp"
    app:cardCornerRadius="8dp"
    >
        <TextView
            android:id="@+id/thruHistory"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:text="Worked here blabbity blah blah" />
 </androidx.cardview.widget.CardView>

Я сжал код для удобства чтения, а также выделил проблему, которую я пытаюсь решить.

Я новичок в android, поэтому обратная связь будет принята с благодарностью. Заранее спасибо.

1 Ответ

1 голос
/ 16 февраля 2020

Я использовал LinearLayout с вертикальной ориентацией и работает, как и ожидалось, при нажатии отображаются тексты, я не вносил никаких изменений в вашу деятельность, зависимость gradle для карты составляет implementation 'androidx.cardview:cardview:1.0.0'

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
              xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
              android:layout_height="match_parent" android:orientation="vertical">
    <androidx.cardview.widget.CardView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
            android:layout_height="120dp"
            android:layout_margin="15dp"
            android:onClick="openBay"
            app:cardElevation="2dp"
            app:cardCornerRadius="8dp">
        <TextView
                android:id="@+id/bayHistory"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone"
                android:text="Worked here blabbity blah blah"/>
    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:layout_margin="15dp"
            android:onClick="openEs"
            app:cardElevation="2dp"
            app:cardCornerRadius="8dp">
        <TextView
                android:id="@+id/esHistory"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone"
                android:text="Worked here blabbity blah blah"/>
    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:layout_margin="15dp"
            android:onClick="openThru"
            app:cardElevation="2dp"
            app:cardCornerRadius="8dp">
        <TextView
                android:id="@+id/thruHistory"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone"
                android:text="Worked here blabbity blah blah"/>
    </androidx.cardview.widget.CardView>
</LinearLayout>
...