3

그렇다면 ScrollView가 다소 불편하거나 (의심스러워) 다른 방법이있는 것 같습니다. 여기 내 코드가있다. 폭탄이 터지면 (두 번째로 루프를 통과 함) 주석 처리됩니다.ScrollView에 여러 개의보기를 추가 할 수 있습니까?

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.ondemandandautomatic_dynamicauthorize); 

    ScrollView svh = (ScrollView) findViewById(R.id.scrollViewHost); 

    // Contacts data snippet adapted from 
    // http://saigeethamn.blogspot.com/2011/05/contacts-api-20-and-above-android.html 
    ContentResolver cr = getContentResolver(); 
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, 
      null, null, null); 
    if (cur.getCount() > 0) { 
     while (cur.moveToNext()) { 
      String id = cur.getString(cur 
        .getColumnIndex(ContactsContract.Contacts._ID)); 
      String name = cur 
        .getString(cur 
          .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

      // Create a Linear Layout for each contact? 
      LinearLayout llay = new LinearLayout(this); 
      llay.setOrientation(LinearLayout.HORIZONTAL); 

      LinearLayout.LayoutParams llp = new LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
      llp.weight = 1.0f; 

      CheckBox cbOnDemand = new CheckBox(getApplicationContext()); 
      cbOnDemand.setTag(id); 
      cbOnDemand.setLayoutParams(llp); 
      llay.addView(cbOnDemand); 

      CheckBox cbTime = new CheckBox(getApplicationContext()); 
      cbOnDemand.setTag(id); 
      cbTime.setLayoutParams(llp); 
      llay.addView(cbTime); 

      CheckBox cbSpace = new CheckBox(getApplicationContext()); 
      cbOnDemand.setTag(id); 
      cbSpace.setLayoutParams(llp); 
      llay.addView(cbSpace); 

      TextView tv = new TextView(getApplicationContext()); 
      tv.setTag(id); 
      tv.setText(name); 
      tv.setLayoutParams(llp); 
      llay.addView(tv); 

      svh.addView(llay); // it transports me to Eclipse's Debug perspective when I hit this line the SECOND time around. 
      // One cat on stackOverflow said to do this, another said it 
      // would be unnecessary 
      svh.invalidate(); 
     } 
    } 
} 

답변

9

.

1)은 ScrollView의 유일한 아이가 수직 LinearLayout 수 확인하고 LinearLayout 대신 ScrollView에 당신의 아이들을 모두 추가 할 수 있습니다.

2) 바람직한 대체 방법은 ListView (아마도 ScrollView 안에 LinearLayout을 사용하여 구현 됨)을 사용하는 것입니다.

+0

도움 주셔서 감사합니다. 옵션 2 (이 첫 번째 찌르다) 문제가 런타임에 각 확인란을 프로그래밍 방식으로 식별 할 수있는 방법을 알아낼 수 없다는 것, 옵션 A와 함께 갈 것입니다. 어느 행이 클릭되고 있는지. 확인란마다 연락처 ID에 해당하는 태그 값이 있어야하므로 각 연락처에 대해 어떤 옵션을 선택했는지 알 수 있습니다. 그래서 동적으로 생성되는 위젯 경로를 방문한 후 체크 박스 (및 textView) 위젯을 만드는 루프에 태그를 추가 할 수 있습니다. –

+0

왜 옵션 2가 선호됩니까? – Johan

6

ScrollView에는 하나의보기 만 포함될 수 있습니다. 그러나 해당보기는 LinearLayout과 같은 레이아웃 일 수 있습니다. 일반적으로 내가하는 일은 ScrollView에 이러한보기를 추가하는 것입니다.

예 :

당신이이 문제를 해결하기 위해 할 수있는 두 가지가 있습니다
<ScrollView> 
    <LinearLayout> 
    <ImageView/> 
    <TextView/> 
    </LinearLayout> 
</ScrollView> 
+1

그는 여전히 혼란스러워하기 때문에. LinearLayout은 ScrollView 내부에 직접 배치됩니다. 그것은 당신이 거기에 넣고 싶은 모든 것들을 래퍼하는 데 사용됩니다. –

+1

나는 LinearLayout을 ScrollView 안에 넣고 있다고 생각한다. 3 개의 체크 박스와 textView를 포함하는 LinearLayouts를 만들어서 ScrollView가 아닌 ​​가장 바깥 쪽의 LinearLayout에 추가해야 할 것입니다. –

관련 문제