[英]Invisible Layout not visible when make it visible in Android
I am trying to implement something like viewing a list of items in RecyclerView (id=schedule_recycler_view) which parent layout is a CardView (id = info_card_view).我正在尝试实现类似查看 RecyclerView (id=schedule_recycler_view) 中的项目列表,其父布局是 CardView (id = info_card_view)。 The Item is fetched by API call.该项目由 API 调用获取。 If no item found, or connectivity issue, I want to view another layout (id = no_list_layout) which is Visibility-Gone at the first time.如果没有找到项目或连接问题,我想查看另一个布局(id = no_list_layout),它是第一次可见性消失。
But The problem I face, although I make the visibility of that layout (id = no_list_layout) visible programmatically, it did not visible.但是我面临的问题是,虽然我以编程方式使该布局的可见性(id = no_list_layout)可见,但它并不可见。
Can anyone help?任何人都可以帮忙吗?
Here the code what I tried这是我尝试过的代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/top_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/_90sdp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/guideline">
<com.google.android.material.textview.MaterialTextView/>
<com.google.android.material.textview.MaterialTextView/>
<com.google.android.material.textview.MaterialTextView/>
</LinearLayout>
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="horizontal"
app:layout_constraintGuide_begin="@dimen/_90sdp" />
<ImageView
android:id="@+id/add_schedule_image_view"
android:layout_width="@dimen/_50sdp"
android:layout_height="@dimen/_50sdp"
android:background="@drawable/circle_white_with_blue_border"
android:padding="@dimen/_5sdp"
android:src="@drawable/ic_plus"
app:layout_constraintBottom_toBottomOf="@id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.9"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/guideline" />
<com.google.android.material.card.MaterialCardView
android:id="@+id/info_card_view"
style="@style/CardViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/_200sdp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/top_bar_layout">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/schedule_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<LinearLayout
android:id="@+id/no_list_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/_15sdp"
android:orientation="vertical"
android:gravity="center"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/not_found_image_view"
android:layout_width="@dimen/_150sdp"
android:layout_height="@dimen/_150sdp"
tools:src="@drawable/ic_schedule"/>
<TextView
android:id="@+id/not_found_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="No Invoice"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>
in kotlin file在 kotlin 文件中
class TutorScheduleFragment : BaseFragment() {
private val tuitionScheduleAdapter by lazy {
TutorScheduleAdapter {
context?.showToast(it.day)
}
}
private val viewModel: TutorScheduleViewModel by lazy {
getViewModel {
TutorScheduleViewModel(
PreferenceService(context?.getSharedPreference()!!)
)
}
}
override fun getLayoutResId() = R.layout.fragment_tutor_schedule
override fun onResume() {
viewModel.getSchedule(jobTutorId!!)
}
override fun observeLiveData() {
viewModel.tuitionScheduleUiState.observe(this@TutorScheduleFragment, Observer {
it.getContentIfNotHandled()?.let { state ->
when (state) {
is Progress -> {
if (state.isLoading) {
} else {
}
}
is Success -> {
val schedules = state.successInfo.data
if (scheduleData.schedules.size == 0) {
no_list_layout.visibility = View.VISIBLE
} else {
tuitionScheduleAdapter.notifyChanged(scheduleData.schedules)
}
}
is Alert -> context?.showToast(state.alert)
is Failure -> {
if (state.throwable is IOException) {
context?.showToast("Internet Connection Failed")
} else {
context?.showToast("Json Parsing Error")
}
}
}
}
})
}
}
You should use no_list_layout.visibility = View.VISIBLE
你应该使用no_list_layout.visibility = View.VISIBLE
Change your view visibility code更改您的视图可见性代码
try this:尝试这个:
Kotlin: Kotlin:
if (list.size == 0) {
no_list_layout.visibility= View.VISIBLE
} else {
adapter.notifyChanged(list)
}
In Java在 Java
if (list.size == 0) {
no_list_layout.setVisibility(View.VISIBLE)
} else {
adapter.notifyChanged(list)
}
You can try with runOnUiThread
.您可以尝试使用runOnUiThread
。
runOnUiThread
runs the specified action on the UI thread.runOnUiThread
在 UI 线程上运行指定的操作。 If the current thread is the UI thread, then the action is executed immediately.如果当前线程是 UI 线程,则立即执行该操作。 If the current thread is not the UI thread, the action is posted to the event queue of the UI thread.如果当前线程不是 UI 线程,则将动作发布到 UI 线程的事件队列中。
context?.runOnUiThread(Runnable {
// Your Logic
if (list.size==0) {
no_list_layout.visibility= View.VISIBLE
} else {
// Your work
}
})
You have to hide your recyclerview also and make it visible in the other case like this:您还必须隐藏您的 recyclerview 并使其在其他情况下可见,如下所示:
if (scheduleData.schedules.size == 0) {
schedule_recycler_view.visibility = View.GONE
no_list_layout.visibility = View.VISIBLE
} else {
schedule_recycler_view.visibility = View.VISIBLE
no_list_layout.visibility = View.GONE
tuitionScheduleAdapter.notifyChanged(scheduleData.schedules)
}
At last, I found the actual culprit.终于,我找到了真正的罪魁祸首。 It was in the layout file.它在布局文件中。 I recreate the layout我重新创建布局
<?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/info_card_view"
style="@style/CardViewStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/top_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rectangle_light_blue_top_info_holder"
android:minHeight="@dimen/_90sdp"
android:orientation="vertical"
android:paddingTop="@dimen/_10sdp"
android:paddingBottom="@dimen/_10sdp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textview.MaterialTextView
android:id="@+id/upcoming_action_text_view"
style="@style/TextViewStyle.Bold"
android:text="Upcoming Test"
android:textSize="@dimen/_8ssp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/date_text_view"
style="@style/TextViewStyle.Bold"
android:text="16 Aug, Wednesday"
android:textSize="@dimen/_15ssp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/upcoming_action_text_view" />
<ImageView
android:id="@+id/add_action_image_view"
android:layout_width="@dimen/_30sdp"
android:layout_height="@dimen/_30sdp"
android:src="@drawable/ic_plus"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.956"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/upcoming_action_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/_20sdp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/date_text_view" />
<com.tbuonomo.viewpagerdotsindicator.WormDotsIndicator
android:id="@+id/dots_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:dotsColor="@color/colorAccent"
app:dotsSize="@dimen/_8sdp"
app:dotsSpacing="@dimen/_2sdp"
app:dotsStrokeColor="@color/light_blue"
app:dotsStrokeWidth="@dimen/_1sdp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/upcoming_action_view_pager" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/previous_action_recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="@dimen/_10sdp"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/top_bar_layout"
tools:itemCount="5"
tools:listitem="@layout/item_previous_action_of_confirm_job" />
<LinearLayout
android:id="@+id/no_list_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
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/top_bar_layout">
<ImageView
android:id="@+id/not_found_image_view"
android:layout_width="@dimen/_150sdp"
android:layout_height="@dimen/_150sdp"
android:src="@drawable/ic_schedule" />
<TextView
android:id="@+id/not_found_text_view"
style="@style/TextViewStyle.Center.Bold"
android:layout_marginTop="@dimen/_10sdp"
android:textColor="@color/colorTextSecondary"
android:text="No Invoice" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.