簡體   English   中英

為什么我的 setOnClickListener 只適用於每個循環通過的最后一個元素?

[英]Why does my setOnClickListener only apply to last element each loop pass?

對不起,如果代碼有點復雜,我一直在發送垃圾郵件試圖解決這個問題。

我在一個布局中創建孩子並跟蹤他們被添加的數量,然后比較它們,以便我們知道哪些集合屬於哪些練習。 單擊箭頭后,設置的信息應該會折疊,但只有每個練習的最后一個視圖才會折疊。

完全讀取

點擊隱藏

所有按鈕點擊隱藏

    fun listeners() {
for (i in childCount3) {
    val imageButton: TextView = binding.parentLinearLayout.getChildAt(i).weightTxt
    val fragment = EditSetFragment()
    imageButton.setOnClickListener {
        supportFragmentManager.beginTransaction().replace(R.id.parent_linear_layout, fragment).commit()
    }
}
// childList = 10 | childList2 = [0,3,6,8] | childList3 = [1,2,4,5,7,9]
var testCount = 0
var testCount2 = 0
for (i in 0 until childCount2.size) {
    var matches = ArrayList<Int>()
    val imageButton: ImageButton = binding.parentLinearLayout.getChildAt(childCount2[i]).expandCard
    Log.d("I  ", i.toString())
    for (k in 0 until childCount3.size) {
        Log.d("k  ", k.toString())
        Log.d("CHECKER  ", binding.parentLinearLayout.getChildAt(childCount2[i]).instanceTxtEx.text.toString() + binding.parentLinearLayout.getChildAt(childCount3[k]).instanceTxt.text).toString()
        if(binding.parentLinearLayout.getChildAt(childCount2[i]).instanceTxtEx.text == binding.parentLinearLayout.getChildAt(childCount3[k]).instanceTxt.text) {
            matches.add(childCount3[k])
            Log.d("MATCH  ", i.toString())
            imageButton.setOnClickListener{
                binding.parentLinearLayout.getChildAt(childCount3[k]).visibility = GONE
            }

        }   
    }
}
}

有點難以理解你在做什么,但我認為你正在childCount2中的每個項目,抓住它的展開按鈕,然后嘗試為該項目在childCount3中的每個孩子添加一個點擊偵聽器?

問題是您只能在View上設置一個單擊偵聽器,因此在您的循環中,您不會添加多個偵聽器,每個偵聽器都隱藏一個按鈕,而是每次都替換當前偵聽器。 所以它最終只隱藏了你設置監聽器的最后一個項目。

如果您以編程方式將這些項目添加到布局中,您不能只存儲每個頂級項目的子項列表嗎? 這樣,您只需為每個按鈕設置一次偵聽器,並在其中引用您的子項列表。 像這樣的東西:

val parentsAndChildren = mutableMapOf<View, MutableList<View>>()

// Add your top-level (childCount2) views here, mapped to an empty list
// As you add children (childCount3), grab their parent's list and add the child to it

fun setUpCollapseButtons() {
    // iterate over all the parents and set a click listener on their buttons
    parentsAndChildren.keys.forEach { parent ->
        parent.expandCard.setOnClickListener {
            // grab the current list of children for this parent, and do the thing
            parentsAndChildren[parent].forEach { it.visibility = GONE }
            // could also do { it.visibility = if (it.visibility == VISIBLE) GONE else VISIBLE } for a toggle
        }
    }
}

getChildAt只返回一個View時,我不是 100% 確定你是如何引用expandCard類的東西,但是當你設置點擊監聽器時,你可以在每個parent級上使用findViewById 希望這能給您帶來想法 - 只跟蹤視圖本身要容易得多,而不必去挖掘它們(並進行文本比較以匹配它們)


如果您仍然想這樣做,以下是如何為每個設置一個單擊偵聽器的方法:

// collect all the parent Views
val parents = childCount2.map { i -> binding.parentLinearLayout.getChildAt(i) }
parents.forEach { parent ->
    // collect all its children
    val children = childCount3.map { j -> binding.parentLinearLayout.getChildAt(j) }
        .filter { child -> parent.instanceTxtEx.text == child.instanceTxtEx.text  }

    // add a single click listener to the parent, make it affect all the children
    parent.expandCard.setOnClickListener {
        children.forEach { it.visibility = GONE }
    }
}

有更好的方法來收集父母和孩子,但希望這是有道理的——你需要先收集他們,然后才能設置影響他們所有人的點擊監聽器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM