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

Android:Kotlin中调用showSpinner(true)无法重新显示设为GONE的LinearLayout问题

Troubleshooting Your HorizontalScrollView Display Issue

Hey there, let's break down why your HorizontalScrollView isn't showing content when you call showSpinner(true)—even though the code seems to execute. Based on your provided layout and Kotlin snippet, here are the key issues and fixes to resolve this:

1. You’re not restoring visibility when showing the view again

In the else block (when hiding the spinner), you set linearLayout!!.visibility = View.GONE, but you never reset it back to View.VISIBLE when show is true. Even if you have content inflated, a GONE view won’t render on screen.

Fix:
Add a visibility reset at the start of your if (show) block:

if (show) {
    linearLayout?.visibility = View.VISIBLE // Add this line
    val layoutInflater = LayoutInflater.from(this)
    // Rest of your inflation logic...
}

2. Content inflation only runs once

Your current code wraps the entire show logic inside if (linearLayout == null). Once linearLayout is initialized on the first showSpinner(true) call, all subsequent calls to showSpinner(true) will skip the inflation code entirely. That means if you cleared content or it didn’t load properly the first time, nothing will appear when you try to show it again.

Fix:
Separate initialization from content display to ensure visibility and content are handled every time:

fun showSpinner(show: Boolean) {
    // Initialize linearLayout if it's not already set
    if (linearLayout == null) {
        linearLayout = findViewById(R.id.linear1)
    }

    if (show) {
        linearLayout?.visibility = View.VISIBLE
        // Only inflate content if the layout is empty (optional, adjust as needed)
        if (linearLayout?.childCount == 0) {
            val layoutInflater = LayoutInflater.from(this)
            // Your code to inflate and add child views to linearLayout here
            // Example: val childView = layoutInflater.inflate(R.layout.your_item_layout, null)
            // linearLayout?.addView(childView)
        }
    } else {
        linearLayout?.visibility = View.GONE
        // Optional: Clear child views if you want to reload fresh content next time
        // linearLayout?.removeAllViews()
    }
}

3. Missing step to add inflated views to the LinearLayout

Double-check that your inflation code actually adds the inflated child views to linearLayout. If you inflate a view but forget to call linearLayout?.addView(inflatedView), the LinearLayout will be visible but empty—so you won’t see anything on screen.

4. Verify the parent layout’s visibility

Make sure the parent RelativeLayout (with id myScrollView) isn’t set to GONE or INVISIBLE elsewhere in your code. If the parent is hidden, all child views (including your HorizontalScrollView) will also be hidden.

Bonus Best Practices

  • Replace non-null assertions (!!) with safe calls (?) to avoid unexpected null pointer crashes.
  • Consider using View Binding instead of findViewById for safer, more maintainable code.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 10:27:30