所以一些摸索之後,我想出了一個解決我的問題,通過編程方式呈現每個GridView和標題:
使用這樣的基本佈局(因爲需要GridView的數量不詳):
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/appListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</ScrollView>
而一個buildAdapter方法在該片段中,像這樣:
boolean headerSectionsRendered = false;
...
private void buildAdapter() {
String gridViewIdString;
/* fill the adapter */
for (String category : mApps.keySet()) {
/* get all of the apps for current category */
mAppAdapter = new AppsAdapter(getActivity(), mApps.get(category));
/* begin programmatically creating the view */
LinearLayout appListLayout = (LinearLayout) getActivity()
.findViewById(R.id.appListFragment);
/* create the section header */
TextView appSectionHeader = new TextView(getActivity());
appSectionHeader.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
appSectionHeader.setText(category);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) appSectionHeader
.getLayoutParams();
params.setMargins(15, 8, 15, 5); // left, top, right, bottom
appSectionHeader.setLayoutParams(params);
appSectionHeader.setTextSize(16);
/* create GridView for the current section*/
GridView appSectionGridView = new GridView(getActivity());
appSectionGridView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
appSectionGridView.setVerticalSpacing(10);
appSectionGridView.setHorizontalSpacing(10);
appSectionGridView.setNumColumns(3);
appSectionGridView.setGravity(Gravity.CENTER);
appSectionGridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
appSectionGridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
/* position of clicked app and adapter it was located in */
showApp(position, parent.getAdapter());
}
});
if (!headerSectionsRendered) {
headerCount++;
appSectionGridView.setAdapter(mAppAdapter);
appListLayout.addView(appSectionHeader);
appListLayout.addView(appSectionGridView);
}
if (headerCount == mApps.keySet().size()) {
headerSectionsRendered = true;
}
}
}
隨着應用適配器,看起來像這樣:
private class AppsAdapter extends BaseAdapter {
private final Activity mContext;
private final App[] apps;
public AppsAdapter(Activity context, App[] appList) {
mContext = context;
apps = appList;
}
public int getCount() {
return apps.length;
}
public App getItem(int position) {
return apps[position];
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View gridItem;
gridItem = new View(mContext);
/* app_cell is the layout for the individual items */
gridItem = inflater.inflate(R.layout.app_cell, null);
ImageView appIcon = (ImageView) gridItem.findViewById(R.id.app_cell_icon);
TextView appTitle = (TextView) gridItem.findViewById(R.id.app_cell_title);
if (appIcon != null && appTitle != null) {
mContext.loadImage(apps[position].getIcon(), appIcon);
appTitle.setText(apps[position].getTitle());
}
return gridItem;
}
@Override
public int getViewTypeCount() {
return 1;
}
}
我不包括的我是如何實現這個解決方案的所有具體細節,但如果你會好奇,瞭解更多信息或有任何意見或建議,請讓我知道!
任何想法如何實現這個解決方案?我也想顯示gridview與alreay已知數字的部分,但每個部分將包含頁眉和頁腳 – Erum