当可见性最初设置为 GONE 时,日历视图不显示
CalendarView doesn't show when visabilty is initally set to GONE
更新:按照我在回答中所述修复后,我添加了整个代码
大家好,我遇到了一个奇怪的错误。我有一个最初设置为 GONE 的日历视图,当用户单击图标时,我尝试将其更改为 VISIBLE 和 GONE。
它没有显示。
但是,当我最初将它设置为 VISIBLE 时,它按预期工作。
我试过调试,它进入了 when 语句的两个分支,但它并没有改变可见性
知道我在这里做错了什么吗?
calendar_icon.setOnClickListener {
when (calendarView.visibility) {
View.GONE -> {
calendarView.visibility = View.VISIBLE
}
else -> { calendarView.visibility = View.GONE
}
}
}
XML代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/per_day_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/brighter_white">
<androidx.cardview.widget.CardView
android:id="@+id/cardView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:cardBackgroundColor="@color/brighter_white"
app:cardCornerRadius="12dp"
app:cardElevation="12dp"
app:contentPadding="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/per_day_search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:layout_weight="4"
android:background="@android:color/transparent"
android:hint="@string/search"
android:textColorHint="@color/silver"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/imageView11"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.6" />
<ImageView
android:id="@+id/imageView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:background="@drawable/rounded_corner_azure_bg"
android:padding="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_search_line" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:background="@drawable/rounded_corner_lavendar_bg"
android:padding="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/calendarView"
app:layout_goneMarginTop="16dp">
<TextView
android:id="@+id/per_day_current_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_weight="4"
android:fontFamily="@font/montserrat_medium"
android:text="TextView"
android:textColor="@color/charcoal"
android:textSize="15sp" />
<ImageView
android:id="@+id/calendar_icon"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/ic_calendar" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout">
<TextView
android:id="@+id/report_item_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:background="@color/periwinkle"
android:fontFamily="@font/montserrat_medium"
android:gravity="center"
android:paddingStart="8dp"
android:paddingLeft="8dp"
android:paddingTop="16dp"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:paddingBottom="16dp"
android:text="@string/user_name"
android:textColor="@color/charcoal"
android:textStyle="bold" />
<TextView
android:id="@+id/report_item_attend"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:background="@color/platinum"
android:fontFamily="@font/montserrat_medium"
android:gravity="center"
android:maxLines="2"
android:paddingStart="8dp"
android:paddingLeft="8dp"
android:paddingTop="16dp"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:paddingBottom="16dp"
android:text="@string/attend_time"
android:textColor="@color/charcoal"
android:textStyle="bold" />
<TextView
android:id="@+id/report_item_leave"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:background="@color/misty_rose"
android:fontFamily="@font/montserrat_medium"
android:gravity="center"
android:maxLines="2"
android:paddingStart="8dp"
android:paddingLeft="8dp"
android:paddingTop="16dp"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:paddingBottom="16dp"
android:text="@string/leave_time"
android:textColor="@color/charcoal"
android:textStyle="bold" />
<TextView
android:id="@+id/report_item_lost_points"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:background="@color/magnolia"
android:ellipsize="end"
android:fontFamily="@font/montserrat_medium"
android:gravity="center"
android:maxLines="2"
android:paddingStart="8dp"
android:paddingLeft="8dp"
android:paddingTop="16dp"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:paddingBottom="16dp"
android:text="@string/lostPoints"
android:textColor="@color/charcoal"
android:textStyle="bold" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/report_rv"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout5" />
<CalendarView
android:id="@+id/calendarView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cardView2"
tools:visibility="visible" />
</androidx.constraintlayout.widget.ConstraintLayout>
片段代码
package iti.intake40.mawgood_admin.reports.per_day
import android.app.DatePickerDialog
import android.os.Build
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.snackbar.Snackbar
import iti.intake40.mawgood_admin.R
import iti.intake40.mawgood_admin.core.DateUtilities
import iti.intake40.mawgood_admin.models.Report
import kotlinx.android.synthetic.main.fragment_reports.*
class ReportsFragment : Fragment(), ReportPerDayContract.IView {
private val TAG = ReportsFragment::class.java.simpleName
private lateinit var presenter: ReportPerDayPresenter
private lateinit var adapter: ReportPerDayAdapter
private val reportList = ArrayList<Report>()
private var rootView: View? = null
private var mDatePickerDialog: DatePickerDialog? = null
private var per_day_current_date: TextView? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment_reports, container, false)
per_day_current_date = root.findViewById(R.id.per_day_current_date)
presenter = ReportPerDayPresenter(context!!, this)
return root
}
@RequiresApi(Build.VERSION_CODES.O)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
rootView = view.findViewById(R.id.per_day_root)
presenter.getOrganizationCreationDate()
calendarView.maxDate = System.currentTimeMillis()
calendarView.visibility = View.GONE
calendar_icon.setOnClickListener {
when (calendarView.visibility) {
View.GONE -> {
calendarView.visibility = View.VISIBLE
}
View.VISIBLE -> {
calendarView.visibility = View.GONE
}
}
}
calendarView.setOnDateChangeListener { view, year, month, dayOfMonth ->
calendarView.visibility = View.GONE
val date = "$dayOfMonth-${month + 1}-$year"
Log.d(TAG, date)
val dayInUnix = DateUtilities.convertDateToTimestamp(date)
presenter.listenForChanges(dayInUnix.toString())
}
}
@RequiresApi(Build.VERSION_CODES.O)
override fun onStart() {
super.onStart()
activity!!.title = "Reports"
search()
per_day_current_date?.text = DateUtilities.getTodayInDate()
val today = DateUtilities.getTodayInUnix().toString()
presenter.listenForChanges(today)
}
private fun search() {
per_day_search.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
adapter.filter.filter(s)
}
})
}
override fun onStop() {
super.onStop()
presenter.detachListeners()
}
override fun displayNoData() {
// per_day_empty_iv.visibility = View.VISIBLE
// per_day_empty_tv.visibility = View.VISIBLE
report_rv.visibility = View.INVISIBLE
}
override fun hideNoData() {
// per_day_empty_iv.visibility = View.INVISIBLE
// per_day_empty_tv.visibility = View.INVISIBLE
report_rv.visibility = View.VISIBLE
}
override fun updateRecyclerView(reports: List<Report>) {
reportList.clear()
reportList.addAll(reports)
adapter = ReportPerDayAdapter(reportList)
report_rv.setHasFixedSize(true)
report_rv.layoutManager = LinearLayoutManager(context)
report_rv.adapter = adapter
adapter.notifyDataSetChanged()
}
override fun updateDayView(date: String) {
per_day_current_date?.text = date
}
override fun showConnectionError() {
Snackbar.make(rootView!!, R.string.no_connection_show_cache, Snackbar.LENGTH_LONG)
.show()
}
override fun setCalendarStartDate(creationDateUnix: Long) {
calendarView.minDate = creationDateUnix
Log.d(TAG, creationDateUnix.toString())
}
}
这似乎是一个错误。我能够通过从 XML 中删除可见性属性来解决这个问题。
并在 onViewCreated
中以编程方式设置可见性,如下所示:calendarView.visibility = View.GONE
更新:按照我在回答中所述修复后,我添加了整个代码
大家好,我遇到了一个奇怪的错误。我有一个最初设置为 GONE 的日历视图,当用户单击图标时,我尝试将其更改为 VISIBLE 和 GONE。 它没有显示。
但是,当我最初将它设置为 VISIBLE 时,它按预期工作。
我试过调试,它进入了 when 语句的两个分支,但它并没有改变可见性 知道我在这里做错了什么吗?
calendar_icon.setOnClickListener {
when (calendarView.visibility) {
View.GONE -> {
calendarView.visibility = View.VISIBLE
}
else -> { calendarView.visibility = View.GONE
}
}
}
XML代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/per_day_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/brighter_white">
<androidx.cardview.widget.CardView
android:id="@+id/cardView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:cardBackgroundColor="@color/brighter_white"
app:cardCornerRadius="12dp"
app:cardElevation="12dp"
app:contentPadding="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/per_day_search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:layout_weight="4"
android:background="@android:color/transparent"
android:hint="@string/search"
android:textColorHint="@color/silver"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/imageView11"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.6" />
<ImageView
android:id="@+id/imageView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:background="@drawable/rounded_corner_azure_bg"
android:padding="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_search_line" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:background="@drawable/rounded_corner_lavendar_bg"
android:padding="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/calendarView"
app:layout_goneMarginTop="16dp">
<TextView
android:id="@+id/per_day_current_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_weight="4"
android:fontFamily="@font/montserrat_medium"
android:text="TextView"
android:textColor="@color/charcoal"
android:textSize="15sp" />
<ImageView
android:id="@+id/calendar_icon"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/ic_calendar" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout">
<TextView
android:id="@+id/report_item_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:background="@color/periwinkle"
android:fontFamily="@font/montserrat_medium"
android:gravity="center"
android:paddingStart="8dp"
android:paddingLeft="8dp"
android:paddingTop="16dp"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:paddingBottom="16dp"
android:text="@string/user_name"
android:textColor="@color/charcoal"
android:textStyle="bold" />
<TextView
android:id="@+id/report_item_attend"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:background="@color/platinum"
android:fontFamily="@font/montserrat_medium"
android:gravity="center"
android:maxLines="2"
android:paddingStart="8dp"
android:paddingLeft="8dp"
android:paddingTop="16dp"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:paddingBottom="16dp"
android:text="@string/attend_time"
android:textColor="@color/charcoal"
android:textStyle="bold" />
<TextView
android:id="@+id/report_item_leave"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:background="@color/misty_rose"
android:fontFamily="@font/montserrat_medium"
android:gravity="center"
android:maxLines="2"
android:paddingStart="8dp"
android:paddingLeft="8dp"
android:paddingTop="16dp"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:paddingBottom="16dp"
android:text="@string/leave_time"
android:textColor="@color/charcoal"
android:textStyle="bold" />
<TextView
android:id="@+id/report_item_lost_points"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:background="@color/magnolia"
android:ellipsize="end"
android:fontFamily="@font/montserrat_medium"
android:gravity="center"
android:maxLines="2"
android:paddingStart="8dp"
android:paddingLeft="8dp"
android:paddingTop="16dp"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:paddingBottom="16dp"
android:text="@string/lostPoints"
android:textColor="@color/charcoal"
android:textStyle="bold" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/report_rv"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout5" />
<CalendarView
android:id="@+id/calendarView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cardView2"
tools:visibility="visible" />
</androidx.constraintlayout.widget.ConstraintLayout>
片段代码
package iti.intake40.mawgood_admin.reports.per_day
import android.app.DatePickerDialog
import android.os.Build
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.snackbar.Snackbar
import iti.intake40.mawgood_admin.R
import iti.intake40.mawgood_admin.core.DateUtilities
import iti.intake40.mawgood_admin.models.Report
import kotlinx.android.synthetic.main.fragment_reports.*
class ReportsFragment : Fragment(), ReportPerDayContract.IView {
private val TAG = ReportsFragment::class.java.simpleName
private lateinit var presenter: ReportPerDayPresenter
private lateinit var adapter: ReportPerDayAdapter
private val reportList = ArrayList<Report>()
private var rootView: View? = null
private var mDatePickerDialog: DatePickerDialog? = null
private var per_day_current_date: TextView? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment_reports, container, false)
per_day_current_date = root.findViewById(R.id.per_day_current_date)
presenter = ReportPerDayPresenter(context!!, this)
return root
}
@RequiresApi(Build.VERSION_CODES.O)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
rootView = view.findViewById(R.id.per_day_root)
presenter.getOrganizationCreationDate()
calendarView.maxDate = System.currentTimeMillis()
calendarView.visibility = View.GONE
calendar_icon.setOnClickListener {
when (calendarView.visibility) {
View.GONE -> {
calendarView.visibility = View.VISIBLE
}
View.VISIBLE -> {
calendarView.visibility = View.GONE
}
}
}
calendarView.setOnDateChangeListener { view, year, month, dayOfMonth ->
calendarView.visibility = View.GONE
val date = "$dayOfMonth-${month + 1}-$year"
Log.d(TAG, date)
val dayInUnix = DateUtilities.convertDateToTimestamp(date)
presenter.listenForChanges(dayInUnix.toString())
}
}
@RequiresApi(Build.VERSION_CODES.O)
override fun onStart() {
super.onStart()
activity!!.title = "Reports"
search()
per_day_current_date?.text = DateUtilities.getTodayInDate()
val today = DateUtilities.getTodayInUnix().toString()
presenter.listenForChanges(today)
}
private fun search() {
per_day_search.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
adapter.filter.filter(s)
}
})
}
override fun onStop() {
super.onStop()
presenter.detachListeners()
}
override fun displayNoData() {
// per_day_empty_iv.visibility = View.VISIBLE
// per_day_empty_tv.visibility = View.VISIBLE
report_rv.visibility = View.INVISIBLE
}
override fun hideNoData() {
// per_day_empty_iv.visibility = View.INVISIBLE
// per_day_empty_tv.visibility = View.INVISIBLE
report_rv.visibility = View.VISIBLE
}
override fun updateRecyclerView(reports: List<Report>) {
reportList.clear()
reportList.addAll(reports)
adapter = ReportPerDayAdapter(reportList)
report_rv.setHasFixedSize(true)
report_rv.layoutManager = LinearLayoutManager(context)
report_rv.adapter = adapter
adapter.notifyDataSetChanged()
}
override fun updateDayView(date: String) {
per_day_current_date?.text = date
}
override fun showConnectionError() {
Snackbar.make(rootView!!, R.string.no_connection_show_cache, Snackbar.LENGTH_LONG)
.show()
}
override fun setCalendarStartDate(creationDateUnix: Long) {
calendarView.minDate = creationDateUnix
Log.d(TAG, creationDateUnix.toString())
}
}
这似乎是一个错误。我能够通过从 XML 中删除可见性属性来解决这个问题。
并在 onViewCreated
中以编程方式设置可见性,如下所示:calendarView.visibility = View.GONE