Как управлять динамически создаваемыми представлениями в Kotlin - PullRequest
0 голосов
/ 15 апреля 2020

Мне удалось запрограммировать себя в очень симпатичную маленькую коробочку, и мне бы хотелось немного помочь с выходом.

Как работает программное обеспечение, оно динамически создает маленькие linearLayouts, которые ласково называют «Bubbles». Каждый Bubble содержит 3 textView и имеет прикрепленный onClickListener. Это теперь пресловутая вставка из первого параграфа, объясненная. Для того, чтобы оправдать себя из окна, мне нужен способ определить, на каком пузыре была нажата кнопка, и уметь манипулировать этим пузырем.

TL; DR: как управлять динамически создаваемыми представлениями в Kotlin.

//Opens a dialog box, which is used to enter in 
        //The Name of the Bubble, an assignee and the date (from a calenedView)
        fab.setOnClickListener {
            val dialogTitle = getString(R.string.newDocument) 
            val themeContext = ContextThemeWrapper(this, R.style.Bubbles)
            val linearShell = LinearLayout(themeContext, null, 0)
            linearShell.orientation = LinearLayout.VERTICAL

            //Sets up the textViews to populate the Bubble
            val textThemeContext = ContextThemeWrapper(this, R.style.BubbleText)
            val headingThemeContext = ContextThemeWrapper(this, R.style.BubbleHeading)
            val name = TextView(headingThemeContext)
            val currentPlace = TextView(textThemeContext)
            val date = TextView(textThemeContext)

            name.textSize = 20F

            //Sets up the DialogView layout
            val builder = AlertDialog.Builder(this)
            builder.setTitle(dialogTitle)
            val inflater = layoutInflater
            val dialogLayout = inflater.inflate(R.layout.dialogpage, null)
            val nameText = dialogLayout.findViewById<EditText>(R.id.dialogNameBox)
            val assigneeText = dialogLayout.findViewById<EditText>(R.id.dialogAssigneeBox)
            val calender = dialogLayout.findViewById<CalendarView>(R.id.dialogCalender)
            var dateValue = ""
            calender?.setOnDateChangeListener { _, year, month, dayOfMonth ->
                dateValue = "" + dayOfMonth + "/" + (month + 1) + "/" + year
            }

            builder.setView(dialogLayout)



            builder.setPositiveButton("Done"){_,_ ->
                //Once the values have been selected, save the values into variables
                name.text = nameText.text.toString()
                currentPlace.text = assigneeText.text.toString()
                date.text = dateValue

                //assign an onClickListener to the Bubble
                linearShell.setOnClickListener(){
                    BubbleSelectedFunction()
                }

                //Create the Bubble
                MainLayout.addView(linearShell)
                linearShell.addView(name)
                linearShell.addView(currentPlace)
                linearShell.addView(date)
            }

            builder.setNegativeButton("Cancel"){_,_ ->}
            builder.create().show()
        }
    }

    private fun BubbleSelectedFunction() {
        //This is where all the Bubble clicks will end up
        Toast.makeText(this@MainActivity, "All roads", Toast.LENGTH_SHORT).show()
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...