2014-11-17 4 views
1

У меня есть следующий макет в моей деятельностиДобавление контента в ScrollView динамически

<HorizontalScrollView 
     android:id="@+id/horizontalScrollView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/topHeading" 
     android:layout_centerHorizontal="true" 
     android:layout_gravity="center_horizontal|center_vertical"> 

     <LinearLayout 
      android:id="@+id/scrollViewlinearlayout" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:gravity="center" 
      android:orientation="horizontal"> 
      <!-- 
      Content will be dynamically added to this portion. 
      --> 
     </LinearLayout> 

    </HorizontalScrollView> 

В моем коде деятельности я надуть вид и добавить к выше макете

LinearLayout scrollViewLinearLayout=(LinearLayout)root.findViewById(R.id.scrollViewlinearlayout); 
    for(int i=0;i<10;i++){ 
      View view =inflater.inflate(R.layout.lesson_row,null,false); 
      Button b1=(Button) view.findViewById(R.id.button); 
      b1.setOnClickListener(this); 
      scrollViewLinearLayout.addView(view); 
    } 

Теперь это работает отлично. У меня 10 макетов в моем макете scrollview. Теперь R.layout.lesson_row содержит кнопки, на которые я хочу нажать. Я могу легко добавить кнопку прослушивания кнопки к кнопке, но какая кнопка из 10 макетов была нажата?

+0

http://stackoverflow.com/a/15732374/1380752 – codeMagic

+0

или просто использовать ListView вместо этого. – Selvin

+0

Его горизонтальная компоновка. – AndroidDev

ответ

0

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

Или введите Button экземпляры в HashMap<Button, ThingTiedToButtonGoesHere> или другую структуру данных Java, чтобы вы могли выполнить поиск во время выполнения.

Смежные вопросы