在Android中不可见的不可见布局

问题描述 投票:0回答:2

我正在尝试实现类似在RecyclerView(id = schedule_recycler_view)中查看项目列表的方法,其父级布局是CardView(id = info_card_view)。该项目是通过API调用获取的。如果没有找到任何项目,或存在连接问题,我想在第一时间查看另一个布局(id = no_list_layout)。

但是我面临的问题,尽管我以编程方式使该布局的可见性(id = no_list_layout)可见,但它不可见。

任何人都可以帮忙吗?

这里是我尝试的代码

<?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"/>

                <TextView
                    android:id="@+id/not_found_text_view"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </com.google.android.material.card.MaterialCardView>

</androidx.constraintlayout.widget.ConstraintLayout>

在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 scheduleData = 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")
                        }
                    }
                }
            }
        })


    }

}

我正在尝试实现类似在RecyclerView(id = schedule_recycler_view)中查看项目列表的方法,其父级布局是CardView(id = info_card_view)。该项目是通过API调用获取的。如果...

android android-layout android-constraintlayout android-cardview
2个回答
1
投票

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.