我想在LinearLayout
其中ScrollView
的孩子LinearLayout
是一堆GridLayouts
包含 4 列的地方添加一个。中的项目GridLayout
是从代码中动态填充的。根据GridLayout
MainRows 类中设置的内容数量,可以有 2 行或 1 行。
现在,由于可以有很多组GridLayout
,所以 aScrollView
是必需的。问题是,ScrollView
只能滚动 2 行网格。如果有意义的话,这 2 行位于一组 MainRows 中。我想滚动布局中包含的所有网格。
这是我的activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/parent">
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/topEight"
android:layout_marginTop="5dp">
</TextView>
</LinearLayout>
</ScrollView>
single_video_grid.xml:
<?xml version="1.0" encoding="utf-8"?>
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_marginTop="8dp"
android:numColumns="4">
</GridView>
从代码中我动态添加和设置视图,如下所示:
ArrayList<MainRows> rows = JSONVideoDataHandler.getRowElements();
for (MainRows row : rows) {
LinearLayout parentLayout = (LinearLayout) findViewById(R.id.parent);
if(row instanceof Ads){
// TODO implement ads layout
}else{
int numberOfItems = row.getNumberOfItems();
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(numberOfItems > 4){
GridView gridview = (GridView) inflater.inflate(R.layout.single_video_grid, null);
// if number of vids exceeds 4, 2 rows are required.
// therefore setting 8 imageplaceholders.
gridview.setAdapter(new ImageAdapter(this, row, 8));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(activity, "haha " + position,
Toast.LENGTH_SHORT).show();
}
});
parentLayout.addView(gridview);
}else{
GridView gridview = (GridView) inflater.inflate(R.layout.single_video_grid, null);
// if number of vids does not exceed 4, 1 row is required.
// therefore setting 4 imageplaceholders.
gridview.setAdapter(new ImageAdapter(this, row, 4));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(activity, "haha " + position,
Toast.LENGTH_SHORT).show();
}
});
// linearLayout = (LinearLayout) findViewById(R.id.parent);
parentLayout.addView(gridview);
}
}
Let me know if the question is not clear enough. I would appreciate any kind of help. Thank you!