почему для отображения фрагмента с макетом ограничения в представлении с прокруткой требуется слишком много времени - PullRequest
0 голосов
/ 22 мая 2019

У меня есть 3 фрагмента, у которых есть расположение ограничений в представлении прокрутки.и все они занимают около 1 - 2 секунд, чтобы показать фрагмент на экране, приложение выглядит медленным.

Я уверен, что при создании представления отсутствует фоновая обработка или выборка данных с сервера, задействованного при создании представления.,это просто обновить пользовательский интерфейс.но я не знаю, почему это занимает слишком много времени, чтобы показать фрагмент на экране.

Я не знаю, если это та же проблема здесь: Можно ли поместить ConstraintLayout внутриScrollView? .но я попробовал это решение, но оно не работает во мне.

Я использую компонент контроллера навигации из Android Jetpack.это дает эффект?

или мне нужно использовать другой макет, кроме макета ограничений, если я хочу использовать представление прокрутки?

вот XML, который я использую:

<ScrollView 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="@android:color/white" android:id="@+id/scrollView_created_event_summary">

    <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:context=".Fragments.CreateEvent.CreateEventSummaryFragment"
            android:id="@+id/constraintLayout_create_event_summary">


        <TextView
                android:text="Jenis Acara"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:id="@+id/textView_created_event_summary_event_type_label"
                android:layout_marginTop="16dp"
                app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
                android:layout_marginStart="16dp"/>


        <TextView
                android:text="Nama Pemateri"
                android:layout_width="0dp"
                android:layout_height="wrap_content" android:id="@+id/textView_created_event_summary_speaker_name_label"
                app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
                app:layout_constraintTop_toBottomOf="@+id/editText_create_event_summary_event_name"
                app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="16dp"
                android:layout_marginTop="24dp"/>

        <EditText
                android:layout_width="0dp"
                android:layout_height="45dp"
                android:ems="10"
                android:id="@+id/editText_create_event_summary_speaker_name"
                android:background="@drawable/edit_text_big_box"
                android:gravity="top|left"
                android:hint="Nama Pemateri"
                android:padding="16dp"
                android:textSize="10sp"
                android:cursorVisible="false"
                android:focusable="false"
                android:focusableInTouchMode="false" android:maxLines="1"
                android:layout_marginStart="16dp"
                app:layout_constraintStart_toStartOf="parent" android:layout_marginEnd="16dp"
                app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="16dp"
                app:layout_constraintTop_toBottomOf="@+id/textView_created_event_summary_speaker_name_label"/>

        <TextView
                android:text="Waktu Dimulainya Acara"
                android:layout_width="0dp"
                android:layout_height="wrap_content" android:id="@+id/textView_created_event_summary_starting_date_label"
                app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
                app:layout_constraintTop_toBottomOf="@+id/editText_create_event_summary_speaker_name"
                app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="16dp"
                android:layout_marginTop="24dp"/>

        <EditText
                android:layout_width="0dp"
                android:layout_height="45dp"
                android:ems="10"
                android:id="@+id/editText_create_event_summary_starting_date"
                android:background="@drawable/edit_text_big_box"
                android:gravity="top|left"
                android:hint="Jam dan Waktu Dimulainya Acara"
                android:padding="16dp"
                android:textSize="10sp"
                android:cursorVisible="false"
                android:focusable="false"
                android:focusableInTouchMode="false" android:maxLines="1"
                android:layout_marginStart="16dp"
                app:layout_constraintStart_toStartOf="parent" android:layout_marginEnd="16dp"
                app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="16dp"
                app:layout_constraintTop_toBottomOf="@+id/textView_created_event_summary_starting_date_label"/>

        <TextView
                android:text="Waktu Selesainya Acara"
                android:layout_width="0dp"
                android:layout_height="wrap_content" android:id="@+id/textView_created_event_summary_ending_date_label"
                app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
                app:layout_constraintTop_toBottomOf="@+id/editText_create_event_summary_starting_date"
                app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="16dp"
                android:layout_marginTop="24dp"/>

        <EditText
                android:layout_width="0dp"
                android:layout_height="45dp"
                android:ems="10"
                android:id="@+id/editText_create_event_summary_ending_date"
                android:background="@drawable/edit_text_big_box"
                android:gravity="top|left"
                android:hint="Jam dan Waktu Selesainya Acara"
                android:padding="16dp"
                android:textSize="10sp"
                android:cursorVisible="false"
                android:focusable="false"
                android:focusableInTouchMode="false" android:maxLines="1"
                android:layout_marginStart="16dp"
                app:layout_constraintStart_toStartOf="parent" android:layout_marginEnd="16dp"
                app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="16dp"
                app:layout_constraintTop_toBottomOf="@+id/textView_created_event_summary_ending_date_label"/>

        <TextView
                android:text="Deskripsi Acara"
                android:layout_width="0dp"
                android:layout_height="wrap_content" android:id="@+id/textView_created_event_summary_description_label"
                app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
                app:layout_constraintTop_toBottomOf="@+id/editText_create_event_summary_ending_date"
                app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="16dp"
                android:layout_marginTop="24dp"/>

        <EditText
                android:layout_width="0dp"
                android:layout_height="120dp"
                android:ems="10"
                android:id="@+id/editText_create_event_summary_description"
                android:layout_marginStart="16dp"
                app:layout_constraintStart_toStartOf="parent" android:layout_marginEnd="16dp"
                app:layout_constraintEnd_toEndOf="parent"
                android:background="@drawable/edit_text_big_box"
                android:gravity="top|left"
                android:hint="Gambaran Acara"
                android:padding="16dp"
                android:inputType="textMultiLine"
                android:scrollHorizontally="false"
                android:scrollbars="vertical"
                android:cursorVisible="false"
                android:focusable="false"
                android:focusableInTouchMode="false"
                app:layout_constraintHorizontal_bias="0.0" android:layout_marginTop="16dp"
                app:layout_constraintTop_toBottomOf="@+id/textView_created_event_summary_description_label"
                android:textSize="10sp"/>

        <TextView
                android:text="Kontak"
                android:layout_width="0dp"
                android:layout_height="wrap_content" android:id="@+id/textView_created_event_summary_contact_label"
                app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
                app:layout_constraintTop_toBottomOf="@+id/editText_create_event_summary_description"
                app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="16dp"
                android:layout_marginTop="24dp"/>

        <EditText
                android:layout_width="0dp"
                android:layout_height="45dp"
                android:ems="10"
                android:id="@+id/editText_create_event_summary_contact_name"
                android:background="@drawable/edit_text_big_box"
                android:gravity="top|left"
                android:hint="Nama Panitia"
                android:padding="16dp"
                android:textSize="10sp"
                android:cursorVisible="false"
                android:focusable="false"
                android:focusableInTouchMode="false" android:maxLines="1"
                android:layout_marginEnd="16dp"
                app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="16dp"
                app:layout_constraintTop_toBottomOf="@+id/textView_created_event_summary_contact_label"
                app:layout_constraintStart_toEndOf="@+id/imageView_icon_person" android:layout_marginStart="8dp"/>

        <EditText
                android:layout_width="0dp"
                android:layout_height="45dp"
                android:ems="10"
                android:id="@+id/editText_create_event_summary_contact_number"
                android:background="@drawable/edit_text_big_box"
                android:gravity="top|left"
                android:hint="Nomor Panitia"
                android:padding="16dp"
                android:textSize="10sp"
                android:cursorVisible="false"
                android:focusable="false"
                android:focusableInTouchMode="false" android:maxLines="1"
                android:layout_marginStart="8dp"
                app:layout_constraintStart_toEndOf="@+id/imageView_icon_phone"
                android:layout_marginEnd="16dp"
                app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="16dp"
                app:layout_constraintTop_toBottomOf="@+id/editText_create_event_summary_contact_name"/>

        <EditText
                android:layout_width="0dp"
                android:layout_height="45dp"
                android:ems="10"
                android:id="@+id/editText_create_event_summary_contact_email"
                android:background="@drawable/edit_text_big_box"
                android:gravity="top|left"
                android:hint="Email"
                android:padding="16dp"
                android:textSize="10sp"
                android:cursorVisible="false"
                android:focusable="false"
                android:focusableInTouchMode="false" android:maxLines="1"
                android:layout_marginEnd="16dp"
                app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="16dp"
                app:layout_constraintTop_toBottomOf="@+id/editText_create_event_summary_contact_number"
                android:layout_marginStart="8dp" app:layout_constraintHorizontal_bias="1.0"
                app:layout_constraintStart_toEndOf="@+id/imageView_icon_email"/>

        <EditText
                android:layout_width="0dp"
                android:layout_height="45dp"
                android:ems="10"
                android:id="@+id/editText_create_event_summary_contact_web"
                android:background="@drawable/edit_text_big_box"
                android:gravity="top|left"
                android:hint="Website"
                android:padding="16dp"
                android:textSize="10sp"
                android:cursorVisible="false"
                android:focusable="false"
                android:focusableInTouchMode="false" android:maxLines="1"
                android:layout_marginStart="8dp"
                android:layout_marginEnd="16dp"
                app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="16dp"
                app:layout_constraintTop_toBottomOf="@+id/editText_create_event_summary_contact_email"
                app:layout_constraintStart_toEndOf="@+id/imageView_icon_web"/>


        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/ic_person"
                android:id="@+id/imageView_icon_person"
                app:layout_constraintStart_toStartOf="@+id/textView_created_event_summary_contact_label"
                app:layout_constraintTop_toTopOf="@+id/editText_create_event_summary_contact_name"
                app:layout_constraintBottom_toBottomOf="@+id/editText_create_event_summary_contact_name"/>

        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/ic_phone_android"
                android:id="@+id/imageView_icon_phone"
                app:layout_constraintStart_toStartOf="@+id/textView_created_event_summary_contact_label"
                app:layout_constraintTop_toTopOf="@+id/editText_create_event_summary_contact_number"
                app:layout_constraintBottom_toBottomOf="@+id/editText_create_event_summary_contact_number"/>

        <ImageView
                android:layout_width="25dp"
                android:layout_height="25dp"
                app:srcCompat="@drawable/ic_email"
                android:id="@+id/imageView_icon_email"
                app:layout_constraintStart_toStartOf="@+id/textView_created_event_summary_contact_label"
                app:layout_constraintTop_toTopOf="@+id/editText_create_event_summary_contact_email"
                app:layout_constraintBottom_toBottomOf="@+id/editText_create_event_summary_contact_email"/>

        <ImageView
                android:layout_width="25dp"
                android:layout_height="25dp"
                app:srcCompat="@drawable/ic_web"
                android:id="@+id/imageView_icon_web"
                app:layout_constraintStart_toStartOf="@+id/textView_created_event_summary_contact_label"
                app:layout_constraintTop_toTopOf="@+id/editText_create_event_summary_contact_web"
                app:layout_constraintBottom_toBottomOf="@+id/editText_create_event_summary_contact_web"/>


        <EditText
                android:layout_width="0dp"
                android:layout_height="45dp"
                android:ems="10"
                android:id="@+id/editText_create_event_summary_additional_address"
                android:layout_marginStart="16dp"
                app:layout_constraintStart_toStartOf="parent"
                android:layout_marginEnd="16dp"
                app:layout_constraintEnd_toEndOf="parent"
                android:background="@drawable/edit_text_big_box"
                android:gravity="top|left"
                android:hint="Patokan"
                android:padding="16dp"
                android:cursorVisible="false"
                android:focusable="false"
                android:focusableInTouchMode="false"
                app:layout_constraintHorizontal_bias="0.0" android:layout_marginTop="16dp"
                app:layout_constraintTop_toBottomOf="@+id/editText_create_event_summary_address"
                android:textSize="10sp"/>

        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:tools="http://schemas.android.com/tools"
                  android:layout_width="0dp"
                  android:layout_height="240dp"
                  android:id="@+id/map_create_event_summary"
                  tools:context=".Activities.MainActivity"
                  android:name="com.google.android.gms.maps.SupportMapFragment"
                  android:layout_marginTop="24dp"
                  app:layout_constraintTop_toBottomOf="@+id/editText_create_event_summary_additional_address"
                  android:layout_marginStart="16dp"
                  app:layout_constraintStart_toStartOf="parent"
                  android:layout_marginEnd="16dp"
                  app:layout_constraintEnd_toEndOf="parent"
                  android:layout_marginBottom="24dp"
                  app:layout_constraintBottom_toBottomOf="parent"/>


    </androidx.constraintlayout.widget.ConstraintLayout>

и вот мой код в моем фрагменте:

class CreateEventSummaryFragment : Fragment() {

    lateinit var eventCityEditText: EditText
    lateinit var eventVenueEditText: EditText
    lateinit var eventAddressEditText: EditText
    lateinit var eventAdditionalAddressEditText: EditText

    lateinit var mapFragment : SupportMapFragment
    lateinit var googleMap: GoogleMap

    lateinit var mContext : Context
    lateinit var mActivity : FragmentActivity

    lateinit var userKM : User
    lateinit var createdEvent : Event


    override fun onAttach(context: Context) {
        super.onAttach(context)

        mContext = context
        activity?.let { mActivity = it }

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        fragmentView = inflater.inflate(R.layout.fragment_create_event_summary, container, false)
        setHasOptionsMenu(true)
        setUpViewsDeclaration()
        setUpSafeArg()
        setUpListeners()

        updateUI()

        // make round corner to image view
        eventPosterImageView.clipToOutline = true // to make round corner image view

        return fragmentView
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        super.onCreateOptionsMenu(menu, inflater)

        inflater.inflate(R.menu.send_action_menu,menu)
        super.onCreateOptionsMenu(menu, inflater)


    }



    override fun onOptionsItemSelected(item: MenuItem): Boolean {

        return when (item.itemId) {
            R.id.send_action_menu -> {

                sendEventDataCreationToFirestore()

                true
            }

            else -> super.onOptionsItemSelected(item)
        }
    }

    private fun setUpViewsDeclaration() {
        mainScrollView = fragmentView.findViewById(R.id.scrollView_created_event_summary)
        eventPosterImageView = fragmentView.findViewById(R.id.imageView_create_event_summary_poster)
        eventNameEditText = fragmentView.findViewById(R.id.editText_create_event_summary_event_name)
        eventTypeEditText = fragmentView.findViewById(R.id.editText_create_event_summary_event_type)
        eventSpeakerNameEditText = fragmentView.findViewById(R.id.editText_create_event_summary_speaker_name)
        startingDateEditText = fragmentView.findViewById(R.id.editText_create_event_summary_starting_date)
        endingDateEditText = fragmentView.findViewById(R.id.editText_create_event_summary_ending_date)
        eventDescriptionEditText = fragmentView.findViewById(R.id.editText_create_event_summary_description)
        contactNameEditText = fragmentView.findViewById(R.id.editText_create_event_summary_contact_name)
        contactNumberEditText = fragmentView.findViewById(R.id.editText_create_event_summary_contact_number)
        contactEmailEditText = fragmentView.findViewById(R.id.editText_create_event_summary_contact_email)
        contactWebEditText = fragmentView.findViewById(R.id.editText_create_event_summary_contact_web)
        eventPriceEditText = fragmentView.findViewById(R.id.editText_create_event_summary_price)
        eventCapacityEditText = fragmentView.findViewById(R.id.editText_create_event_summary_capacity)
        eventCityEditText = fragmentView.findViewById(R.id.editText_create_event_summary_city)
        eventVenueEditText = fragmentView.findViewById(R.id.editText_create_event_summary_venue)
        eventAddressEditText = fragmentView.findViewById(R.id.editText_create_event_summary_address)
        eventAdditionalAddressEditText = fragmentView.findViewById(R.id.editText_create_event_summary_additional_address)
    }

    private fun setUpSafeArg() {
        arguments?.let {

            val args = CreateEventSummaryFragmentArgs.fromBundle(it)
            userKM = args.userData
            createdEvent = args.createdEvent


        }
    }


    @SuppressLint("ClickableViewAccessibility")
    private fun setUpListeners() {

        // to make multiline edittext scrollable inside scroll view
        eventDescriptionEditText.setOnTouchListener(object: View.OnTouchListener {
            override fun onTouch(v: View?, event: MotionEvent?): Boolean {
                mainScrollView.requestDisallowInterceptTouchEvent(true)
                return false
            }

        })

    }

    private fun updateUI() {

        createdEvent.imagePoster?.let {
            eventPosterImageView.setImageBitmap(it)
        }

        eventNameEditText.setText(createdEvent.title, TextView.BufferType.EDITABLE)
        eventTypeEditText.setText(createdEvent.eventType.getEventTypeInString(),TextView.BufferType.EDITABLE)
        eventDescriptionEditText.setText(createdEvent.description, TextView.BufferType.EDITABLE)
        contactNameEditText.setText(createdEvent.contactPerson, TextView.BufferType.EDITABLE)
        contactNumberEditText.setText(createdEvent.numberOfContact, TextView.BufferType.EDITABLE)
        contactEmailEditText.setText(createdEvent.email, TextView.BufferType.EDITABLE)
        eventCapacityEditText.setText("${createdEvent.capacity}", TextView.BufferType.EDITABLE)
        eventCityEditText.setText(createdEvent.city, TextView.BufferType.EDITABLE)
        eventVenueEditText.setText(createdEvent.venue, TextView.BufferType.EDITABLE)
        eventAddressEditText.setText(createdEvent.address, TextView.BufferType.EDITABLE)
        eventAdditionalAddressEditText.setText(createdEvent.additionalAddress, TextView.BufferType.EDITABLE)

        val dateStartingDate = DateTimeService.changeDateToString("EEEE, d MMMM yyyy",createdEvent.dateTimeStart)
        val timeStartingDate = DateTimeService.changeDateToString("HH:mm zzz",createdEvent.dateTimeStart)
        startingDateEditText.setText("Hari $dateStartingDate. Jam $timeStartingDate.")



        val dateEndingDate = DateTimeService.changeDateToString("EEEE, d MMMM yyyy",createdEvent.dateTimeFinish)
        val timeEndingDate = DateTimeService.changeDateToString("HH:mm zzz",createdEvent.dateTimeFinish)
        endingDateEditText.setText("Hari $dateEndingDate. Jam $timeEndingDate.")




        if (createdEvent.price == 0) {
            eventPriceEditText.setText("Gratis", TextView.BufferType.EDITABLE)
        } else {
            val localeID = Locale("in", "ID")
            val rupiahFormat = NumberFormat.getCurrencyInstance(localeID)
            val formattedPrice = rupiahFormat.format(createdEvent.price.toDouble())
            eventPriceEditText.setText(formattedPrice, TextView.BufferType.EDITABLE)
        }



        val formatted = String.format("%,d", createdEvent.capacity).replace(',', '.')
        eventCapacityEditText.setText("$formatted Orang", TextView.BufferType.EDITABLE)


        createdEvent.website?.let {
            contactWebEditText.setText(it, TextView.BufferType.EDITABLE)
        } ?: run {
            contactWebEditText.setText("-", TextView.BufferType.EDITABLE)
        }

        createdEvent.speaker?.let {
            eventSpeakerNameEditText.setText(it, TextView.BufferType.EDITABLE)
        } ?: run {
            eventSpeakerNameEditText.setText("-", TextView.BufferType.EDITABLE)
        }




        setUpGoogleMap()


    }

    private fun setUpGoogleMap() {

        mapFragment = childFragmentManager.findFragmentById(R.id.map_create_event_summary) as SupportMapFragment

        mapFragment.getMapAsync {

            googleMap = it

            val eventLocation = LatLng(createdEvent.coordinate.latitude,createdEvent.coordinate.longitude)
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(eventLocation,16f))
            googleMap.addMarker(MarkerOptions().position(eventLocation).title("Lokasi Acara")).showInfoWindow()
        }

    }


}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...