You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Kotlin中激活自定义进度条对话框(结合点击事件)

Got it, let's get that custom progress dialog working in your RecyclerView click event and during the data loading flow. Here's a step-by-step implementation tailored to your existing code:

Step 1: Add a helper to create your custom progress dialog

First, let's make a reusable way to initialize your custom dialog. We'll integrate it directly into your code for simplicity—just replace the placeholder layout ID with your actual custom progress bar XML.

Step 2: Show the dialog when clicking a RecyclerView item

Modify the setOnClickListener in your RankRecyclerViewAdapterMap to display the dialog right when the user taps an item. We'll dismiss it after a short delay (or you can let the target activity handle dismissal once its loading finishes):

// Inside onBindViewHolder, the item click listener
holder.itemView.setOnClickListener{
    // Initialize your custom progress dialog
    val progressDialog = Dialog(context!!)
    progressDialog.setContentView(R.layout.your_custom_progress_layout) // Replace with your layout ID
    progressDialog.setCancelable(false) // Prevent closing by tapping outside
    progressDialog.show()

    // Launch the detail activity
    val nextIntent = Intent(context, RankRecyclerItemClickActivity::class.java)
    nextIntent.putExtra("MapTitle", singleItem.mapTitle)
    context!!.startActivity(nextIntent)

    // Dismiss the dialog after a short delay (adjust timing as needed)
    Handler(Looper.getMainLooper()).postDelayed({
        if (progressDialog.isShowing) {
            progressDialog.dismiss()
        }
    }, 800)
}
Step 3: Show dialog during data loading in RankRecyclerItemClickActivity

Since this activity has two time-consuming operations (Firestore query and Firebase Storage image load), we'll show the dialog until all content is ready. Here's the updated code:

class RankRecyclerItemClickActivity : AppCompatActivity() {
    lateinit var mapRankingDownloadThread: Thread
    var arrRankingData: ArrayList<RankingData> = arrayListOf()
    var rankingData = RankingData()
    private lateinit var progressDialog: Dialog // Declare dialog variable

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_rank_recycler_item_click)

        // Initialize and show the custom progress dialog immediately
        progressDialog = Dialog(this)
        progressDialog.setContentView(R.layout.your_custom_progress_layout)
        progressDialog.setCancelable(false)
        progressDialog.show()

        val intent = getIntent()
        var mapTitle = intent.extras?.getString("MapTitle").toString()
        var cutted = mapTitle.split("||")
        rankRecyclerMapTitle.text = cutted[0]

        // Image loading logic remains the same
        val imageView = rankRoutePriview
        val storage = FirebaseStorage.getInstance("gs://tracer-9070d.appspot.com/")
        val mapImageRef = storage.reference.child("mapImage").child(mapTitle)
        mapImageRef.downloadUrl.addOnCompleteListener { task ->
            if (task.isSuccessful) {
                Glide.with(this@RankRecyclerItemClickActivity)
                    .load(task.result)
                    .override(1024, 980)
                    .into(imageView)
            }
        }

        mapRankingDownloadThread = Thread(Runnable {
            val db = FirebaseFirestore.getInstance()
            db.collection("rankingMap").document(mapTitle).collection("ranking").orderBy("challengerTime", Query.Direction.ASCENDING)
                .get()
                .addOnSuccessListener { result ->
                    for (document in result) {
                        rankingData = document.toObject(RankingData::class.java)
                        arrRankingData.add(rankingData)
                    }
                    // Update RecyclerView on main thread (required for UI operations)
                    runOnUiThread {
                        rankRecyclerItemClickRecyclerView.layoutManager = LinearLayoutManager(this)
                        rankRecyclerItemClickRecyclerView.adapter = RankRecyclerViewAdapterTopPlayer(arrRankingData)
                        
                        // Dismiss dialog once data is loaded and RecyclerView is set up
                        if (progressDialog.isShowing) {
                            progressDialog.dismiss()
                        }
                    }
                }
                .addOnFailureListener { exception ->
                    // Dismiss dialog even if loading fails to avoid it getting stuck
                    runOnUiThread {
                        if (progressDialog.isShowing) {
                            progressDialog.dismiss()
                        }
                    }
                }
        })
        mapRankingDownloadThread.start()

        rankRecyclerMoreButton.setOnClickListener{
            val nextIntent = Intent(this, RankingMapDetailActivity::class.java)
            nextIntent.putExtra("MapTitle", mapTitle)
            startActivity(nextIntent)
        }
    }

    // Clean up dialog to avoid memory leaks
    override fun onDestroy() {
        super.onDestroy()
        if (::progressDialog.isInitialized && progressDialog.isShowing) {
            progressDialog.dismiss()
        }
    }
}

Key Notes:

  • Replace R.layout.your_custom_progress_layout with the actual ID of your custom progress bar XML layout.
  • We added runOnUiThread when updating the RecyclerView because Firestore callbacks run on a background thread, and all UI operations must happen on the main thread.
  • The dialog is dismissed both on successful data load and failure to prevent it from staying stuck on screen.
  • The onDestroy method ensures the dialog is closed if the activity is destroyed unexpectedly, avoiding memory leaks.

内容的提问来源于stack exchange,提问作者Teddy2212

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 14:49:08