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

如何为Android的10个Activity实现ViewPager式剩余页面填充指示器?

Solution for Activity Completion Indicator (Similar to ViewPager Indicator)

Got it, since you're dealing with multiple Activities instead of a ViewPager with Fragments, the key here is to track completion state across all Activities and render a custom indicator in each one. Here's a step-by-step solution that mimics the ViewPager indicator style:

1. Create a Global Completion Tracker

First, you need a way to share completion state between all 10 Activities. A singleton class works great for real-time updates, and we can add persistence if you need to retain state after app restarts.

object ActivityCompletionTracker {
    private val completedActivities = BooleanArray(10) { false }

    // Mark an activity as completed (pass its index: 0-9)
    fun markActivityCompleted(position: Int) {
        if (position in 0..9) {
            completedActivities[position] = true
        }
    }

    // Check if an activity is completed
    fun isActivityCompleted(position: Int): Boolean {
        return position in 0..9 && completedActivities[position]
    }

    // Get total completed activities
    fun getCompletedCount(): Int {
        return completedActivities.count { it }
    }

    // Optional: Persist state to SharedPreferences
    fun saveState(context: Context) {
        val prefs = context.getSharedPreferences("ActivityCompletion", Context.MODE_PRIVATE)
        val editor = prefs.edit()
        completedActivities.forEachIndexed { index, isCompleted ->
            editor.putBoolean("activity_$index", isCompleted)
        }
        editor.apply()
    }

    // Optional: Load saved state on app start
    fun loadState(context: Context) {
        val prefs = context.getSharedPreferences("ActivityCompletion", Context.MODE_PRIVATE)
        for (index in 0..9) {
            completedActivities[index] = prefs.getBoolean("activity_$index", false)
        }
    }
}

2. Add Indicator Layout to Each Activity

In every Activity's layout XML, add a container for the indicator dots (and optional text for remaining count):

<!-- Add this to your Activity layout, e.g., at the top or bottom -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="16dp">

    <TextView
        android:id="@+id/tv_remaining"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="还剩10个页面需要填写"
        android:layout_marginBottom="8dp"/>

    <LinearLayout
        android:id="@+id/ll_indicator_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"/>
</LinearLayout>

3. Create Indicator Dot Drawables

Make 3 drawable resources to represent different states:

indicator_dot_current.xml (Current Activity's dot)

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <solid android:color="@color/primary"/> <!-- Use your app's primary color -->
</shape>

indicator_dot_completed.xml (Completed Activity's dot)

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <solid android:color="@color/grey_700"/>
</shape>

indicator_dot_incomplete.xml (Unfinished Activity's dot)

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <stroke android:color="@color/grey_300" android:width="2dp"/>
    <solid android:color="@android:color/transparent"/>
</shape>

4. Implement Indicator Rendering in Each Activity

Add this method to every Activity to build and update the indicator. Call it in onResume() to ensure it refreshes when the Activity comes back into view:

private fun setupIndicator(currentActivityIndex: Int) {
    val indicatorContainer = findViewById<LinearLayout>(R.id.ll_indicator_container)
    val remainingText = findViewById<TextView>(R.id.tv_remaining)

    // Update remaining count text
    val remaining = 10 - ActivityCompletionTracker.getCompletedCount()
    remainingText.text = "还剩$remaining 个页面需要填写"

    // Clear existing dots before rebuilding
    indicatorContainer.removeAllViews()

    val dotSize = resources.getDimensionPixelSize(R.dimen.indicator_dot_size)
    val dotMargin = resources.getDimensionPixelSize(R.dimen.indicator_dot_margin)

    // Create a dot for each Activity
    for (index in 0..9) {
        val dot = View(this)
        val params = LinearLayout.LayoutParams(dotSize, dotSize)
        params.setMargins(dotMargin, 0, dotMargin, 0)
        dot.layoutParams = params

        // Set dot background based on state
        dot.setBackgroundResource(
            when {
                index == currentActivityIndex -> R.drawable.indicator_dot_current
                ActivityCompletionTracker.isActivityCompleted(index) -> R.drawable.indicator_dot_completed
                else -> R.drawable.indicator_dot_incomplete
            }
        )

        indicatorContainer.addView(dot)
    }
}

// Call this in onResume
override fun onResume() {
    super.onResume()
    setupIndicator(0) // Replace 0 with this Activity's index (0-9)
}

Don't forget to add the dimensions to res/values/dimens.xml:

<dimen name="indicator_dot_size">16dp</dimen>
<dimen name="indicator_dot_margin">8dp</dimen>

5. Update Completion State When User Finishes an Activity

When the user completes the current Activity's form, mark it as done and save the state (if using persistence):

// Example: On submit button click
btn_submit.setOnClickListener {
    // Mark current Activity as completed (use its index)
    ActivityCompletionTracker.markActivityCompleted(0)
    // Optional: Save state to persist across app restarts
    ActivityCompletionTracker.saveState(this)
    
    // Navigate to next Activity
    startActivity(Intent(this, NextActivity::class.java))
}

6. Initialize Persistence (Optional)

If you want to retain completion state after the app closes, add this to your custom Application class:

class MyApp : Application() {
    override fun onCreate() {
        super.onCreate()
        ActivityCompletionTracker.loadState(this)
    }
}

And register the Application class in AndroidManifest.xml:

<application
    android:name=".MyApp"
    ...>
    ...
</application>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:18:44