【发布时间】:2018-12-31 16:28:58
【问题描述】:
我得到了一个包含多个项目的recyclerView (ViewHolders)。在其中一个(ViewHolderItemTratamentos)中,我得到了以下元素:
当点击第一个“添加button”时,通过inflator 布局,相同的元素(editText 和button)会在之前的元素下方创建。就像这样:
我的adapter 中的代码(它得到了设置点击的逻辑)创建新行:
holder.add_field_button.setOnClickListener {
holder.parent_linear_layout.apply {
val inflater = LayoutInflater.from(context)
val rowView = inflater.inflate(R.layout.used_products_field, this, false)
holder.parent_linear_layout.addView(rowView, holder.parent_linear_layout.childCount!! - 0)
holder.add_field_button.text = "-"
}
}
所以,问题是我无法从layout.used_products_field 的button 中获取id 来生成另一个新行。我应该夸大这个布局吗(它确实有意义吗?)?我应该给每个button(静态的和生成的)同样的id吗?
R.layout.used_products_field 的内容:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/number_edit_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:inputType="phone"/>
<Button
android:id="@+id/add_field_button"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
style="@style/botaoCard"
android:textSize="24dp"
android:text="+"
android:padding="5dp"/>
【问题讨论】:
-
我想如果你的按钮总是在那里
visibility="GONE"那么你就不会遇到这些问题 -
@epicPandaForce 也许你不明白这一点。我的问题是我无法获取第二个按钮的 id,它位于与第一个不同的布局内。
-
@PedroRelvas 你有另一个问题没有接受答案,即使你在这个新问题中明确使用它,我的意思是
val inflater = LayoutInflater.from(context)部分,请参阅stackoverflow.com/questions/53981684/… -
像往常一样在
R.layout.used_products_field中指定id,稍后点击监听器调用holder.parent_linear_layout.findViewById(R.id.button) -
@OmarMainegra 你好!对不起,你是对的!我不是故意的!感谢您提醒我注意。
标签: android kotlin android-recyclerview adapter android-viewholder