Диалог не отображается, и экран только тускнеет на экране - PullRequest
0 голосов
/ 05 сентября 2018

В настоящее время я создаю пользовательский диалог для отображения часов с 00:00 до 23:59, я использую kotlin в качестве языка разработки. Моя проблема в том, что когда я открываю диалоговое окно, экран гаснет и ничего не отображается, я думаю, что я сделал все правильные шаги. Ошибки не отображаются, но перед тем, как я поместил '?' в recyclerViewCalendar? [Calendar Dialog Class], он выдал мне нулевую ошибку в переменной. Это мой код

Класс диалога календаря

class CalendarDialog : DialogFragment() {

    /**
     * Define variables
     */
    private val mDaysList : MutableList<Days> = ArrayList()
    private val dayMonthYear = "2018-06-14" //TODO fetch the date of today

    /**
     * Initialize the adapter
     */
    private val adapter = CalendarAdapter(mDaysList)

    /**
     * Initialize the layout manager
     */
    private fun getLinearLayoutManager(): LinearLayoutManager {
        return LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false)
    }

    private fun initView() {
        setDataListItems()
        recyclerViewCalendar?.adapter = adapter
        recyclerViewCalendar?.layoutManager = getLinearLayoutManager()
        recyclerViewCalendar?.setHasFixedSize(true)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val rootView = inflater.inflate(R.layout.calendar_dialog, container)
        initView()
        return rootView
    }


    /**
     * TODO dependendo do horário da clinica bloqueio e desbloqueio horários
     */
    private fun setDataListItems() {
        mDaysList.add(Days("06:00", dayMonthYear))
        mDaysList.add(Days("06:30", dayMonthYear))
        mDaysList.add(Days("07:00", dayMonthYear))
        mDaysList.add(Days("07:30", dayMonthYear))
        mDaysList.add(Days("08:00", dayMonthYear))
        mDaysList.add(Days("08:30", dayMonthYear))
        mDaysList.add(Days("09:00", dayMonthYear))
        mDaysList.add(Days("09:30", dayMonthYear))
        mDaysList.add(Days("10:00", dayMonthYear))
        mDaysList.add(Days("10:30", dayMonthYear))
        mDaysList.add(Days("11:00", dayMonthYear))
        mDaysList.add(Days("11:30", dayMonthYear))
        mDaysList.add(Days("12:00", dayMonthYear))
        mDaysList.add(Days("12:30", dayMonthYear))
        mDaysList.add(Days("13:00", dayMonthYear))
        mDaysList.add(Days("13:30", dayMonthYear))
        mDaysList.add(Days("14:00", dayMonthYear))
        mDaysList.add(Days("14:30", dayMonthYear))
        mDaysList.add(Days("15:00", dayMonthYear))
        mDaysList.add(Days("15:30", dayMonthYear))
        mDaysList.add(Days("16:00", dayMonthYear))
        mDaysList.add(Days("16:30", dayMonthYear))
        mDaysList.add(Days("17:00", dayMonthYear))
        mDaysList.add(Days("17:30", dayMonthYear))
        mDaysList.add(Days("18:00", dayMonthYear))
        mDaysList.add(Days("18:30", dayMonthYear))
        mDaysList.add(Days("19:00", dayMonthYear))
        mDaysList.add(Days("19:30", dayMonthYear))
        mDaysList.add(Days("20:00", dayMonthYear))
        mDaysList.add(Days("20:30", dayMonthYear))
    }

}

Item.xml

<android.support.constraint.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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/client_id_input_layout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent">

        <TextView
            android:id="@+id/hourOfDay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="monospace"
            android:gravity="center"
            android:text="00:00"
            android:textColor="@color/picker_default_selected_text_color"
            android:textSize="36sp"
            android:textStyle="bold" />

    </android.support.design.widget.TextInputLayout>


    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5"/>

    <TextView
        android:id="@+id/day_month_year"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="22 Agosto 2018"
        android:textStyle="bold|italic"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline" />

</android.support.constraint.ConstraintLayout>

Модель класса

data class Days(var hourOfDay:String ,var dayMonthYear:String)

Макет диалогового окна календаря xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#f4f2f2"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerViewCalendar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</android.support.constraint.ConstraintLayout>

Фрагмент класса (где я вызываю диалог)

class BlockSchedulesFragment : BaseFragment<BlockSchedulesViewModel>() {


    /**
     * Call this fragment in the parent activity
     */
    companion object {
        fun  newInstance() = BlockSchedulesFragment()
    }


    /**
     * Base fragment methods
     */

    override fun layoutToInflate() = R.layout.fragment_block_shedules_screen

    override fun definedViewModel() = ViewModelProviders.of(this, Injection.provideViewModelBlockerFactory(context))
            .get(BlockSchedulesViewModel::class.java)

    override fun doOnCreated(savedInstanceState: Bundle?) {

       //I do the call to the dialog here
        val fm = fragmentManager
        val tv = CalendarDialog()

        open_calendar_btn.setOnClickListener {
            tv.show(fm,"TV_tag")
        }

        confirm_block.setOnClickListener {
            Toast.makeText(activity,"Bloqueio efectuado",Toast.LENGTH_SHORT).show()
        }

    }
}

1 Ответ

0 голосов
/ 05 сентября 2018

В макете вашего календаря xml вы использовали ConstraintLayout, но не использовали правильно ограничения в элементе RecyclerView.

Ниже приведена правильная реализация

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f4f2f2"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerViewCalendar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

</android.support.constraint.ConstraintLayout>

Кроме того, в классе Календарь * введите false в поле attachToParent.

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val rootView = inflater.inflate(R.layout.calendar_dialog, container, false)
    initView()
    return rootView
}

Это должно работать. Если это не так, скажите мне.

...