Создать один XML для вертикальный LinearLayout с ScrollView как родительский, поэтому всякий раз, когда LinearLayout no of child больше, он будет применять прокрутку.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
Другой xml для вертикального LinearLayout child, который содержит 4 TextView в горизонтальном LinearLayout.
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
Теперь, как добавить элемент в вертикальной LinearLayout:
private LinearLayout container;
private Context context;
private int size=3;
private int counter=1;
context = this;
container = (LinearLayout) findViewById(R.id.container);
for (int i=0;i<size;i++){
View view = LayoutInflater.from(context).inflate(R.layout.row_item,null);
TextView textView1 = (TextView) view.findViewById(R.id.textView1);
TextView textView2 = (TextView) view.findViewById(R.id.textView2);
TextView textView3 = (TextView) view.findViewById(R.id.textView3);
TextView textView4 = (TextView) view.findViewById(R.id.textView4);
textView1.setText(String.valueOf(counter++));
textView2.setText(String.valueOf(counter++));
textView3.setText(String.valueOf(counter++));
textView4.setText(String.valueOf(counter++));
view.setTag((i+1));
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,String.valueOf((Integer)v.getTag()),Toast.LENGTH_SHORT).show();
}
});
container.addView(view);
}
Это не очень сложно. Вам нужно установить 'LayoutParams' для всех' LinearLayout' и добавить представление в соответствии с вашим требованием. – Piyush
не так много сложного может также создать список рассылки –