2017-12-06 34 views
0

xml에서 다른 레이아웃으로 레이아웃 목록 programmatically을 부 풀리고 있습니다.레이아웃의 액세스 버튼이 다른 레이아웃 내에서 프로그래밍 방식으로 부풀려 짐

최종 결과는 다음 이미지와 같습니다.

checkboxes의 수는 각 실행에 다른 Image, 내가 어떻게 자신의 상태를 얻을 수 있으며, 이미지 버튼에 리스너를 추가? 이상 적으로는 선택한 레이아웃의 ID를 가진 리스너 하나만 필요합니다.

이것은 XML 코드입니다.

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/chk_item_layout" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"> 

<CheckBox 
    android:id="@+id/checkBox_chkitem" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="16dp" 
    android:layout_marginTop="16dp" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintTop_toTopOf="parent" /> 

<TextView 
    android:id="@+id/textView_chkitem" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="8dp" 
    android:layout_marginStart="8dp" 
    android:layout_marginTop="24dp" 
    android:text="TextView" 
    app:layout_constraintEnd_toStartOf="@+id/imageButton_chkitem" 
    app:layout_constraintStart_toEndOf="@+id/checkBox_chkitem" 
    app:layout_constraintTop_toTopOf="parent" /> 

<ImageButton 
    android:id="@+id/imageButton_chkitem" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="16dp" 
    android:layout_marginTop="16dp" 
    app:layout_constraintEnd_toEndOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    app:srcCompat="@android:drawable/stat_notify_more" /> 
</android.support.constraint.ConstraintLayout> 

그리고 이것은 내가 레이아웃

 val checkitemlist = listOf<ChecklistItem>(ChecklistItem("Categoria", "conteudo"), 
      ChecklistItem("Categoria", "conteudo"), 
      ChecklistItem("Categoria", "conteudo"), 
      ChecklistItem("Categoria", "conteudo")) 
    for (item in checkitemlist) { 
     val inflater = LayoutInflater.from(context) 
     val layout = inflater.inflate(io.ubivis.ier.R.layout.checklistitem_layout, null, false) as ConstraintLayout 
     layout.textView_chkitem.text = item.textSimple 
     checklist_content_layout.addView(layout) 
    } 
+1

'findViewById'에 문제가 있나요 교체를 부풀려 어떻게? – m0skit0

+0

정말 프로그래밍 방식으로이 작업을 수행해야합니까? 리사이클 러뷰 + 어댑터로 할 수있는 것처럼 보입니다. – donfuxx

+0

https://www.androidhive.info/2016/01/android-working-with-recycler-view/ –

답변

0

코드

for (item in checkitemlist) { 
      checklist_content_layout.removeAllViews() 
      val inflater = LayoutInflater.from(context) 
      val layout = inflater.inflate(io.ubivis.ier.R.layout.checklistitem_layout, null, false) as ConstraintLayout 
      layout.textView_chkitem.text = item.textSimple 
      checklist_content_layout.addView(layout) 
     }