Loading Dialog

박재원·2024년 2월 2일
0

TIL

목록 보기
42/50
post-thumbnail
post-custom-banner

무언가 작업을 할 때 로딩이 되는 화면을 구현해 보도록 하겠다.

CircleProgressDialog

package com.example.searchapi.fragment

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import com.example.searchapi.databinding.FragmentDialogBinding

class CircleProgressDialog : DialogFragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        val binding = FragmentDialogBinding.inflate(inflater, container, false)
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

        return binding.root
    }
}

fragment_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".fragment.CircleProgressDialog">

    <ProgressBar
        android:id="@+id/circle_progress"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:indeterminateDrawable="@drawable/circle_progress"
        android:indeterminateDuration="1000"
        android:padding="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</FrameLayout>

Drawable circle_progress.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="360" >
    <shape
        android:innerRadiusRatio="3"
        android:shape="ring"
        android:thicknessRatio="8"
        android:useLevel="false">

        <size
            android:width="10dp"
            android:height="60dp"/>
        <gradient
            android:angle="0"
            android:endColor="#FF9800"
            android:startColor="@color/white"
            android:type="sweep"
            android:useLevel="false"
            />
    </shape>
</rotate>

code

binding.rvSearch.addOnScrollListener(object : RecyclerView.OnScrollListener() {
	override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
		super.onScrolled(recyclerView, dx, dy)
		val query = binding.etSearch.text.toString()
		val lastVisibleItemPosition =
                    (recyclerView.layoutManager as GridLayoutManagerWrapper).findLastCompletelyVisibleItemPosition()
		val itemTotalCount = recyclerView.adapter!!.itemCount - 1
		if (lastVisibleItemPosition == itemTotalCount) {
			showLoading() //로딩
			page += 1 //스크롤이 최하단이면 다음페이지 출력
			CoroutineScope(Dispatchers.Main).launch {
				val response = Retrofit.api.searchImage(query, "accuracy", page, 80)
                imageAdapter.imageList.addAll(response.documents)
				imageAdapter.notifyDataSetChanged()
			}
		}
	}
})
private fun showLoading() {
	CoroutineScope(Dispatchers.Main).launch {
		loadingDialog.show(parentFragmentManager, loadingDialog.tag)
		withContext(Dispatchers.Default) { delay(1500) }
		loadingDialog.dismiss()
	}
}
post-custom-banner

0개의 댓글