Android GridView适配器填充多个部分

时间:2014-01-07 16:47:03

标签: java android gridview android-fragments adapter

我希望使用多个自适应GridViews在屏幕上用不同的数据填充多个部分。每个部分都有不确定数量的部分和项目,因此解决方案必须灵活。每个部分都有一个部分标题标题和自己的gridview,填充了未知数量的项目。必须可以单击这些元素才能转到该元素的另一个屏幕。所以所需的屏幕看起来像这样:

 __________________________________
|         **Section1**             |
|                                  |
| GridView1  GridView1  GridView1  |
| Item1      Item2      Item3      |
|                                  |
| GridView1  GridView1             |
| Item4      Item5                 |
|                                  |
|                                  |
|         **Section2**             |
|                                  |
| GridView2  GridView2  GridView3  |
| Item1      Item2      Item3      |
|                                  |
|                                  |
|         **Section3**             |
|                                  |
| GridView3  GridView3             |
| Item1      Item2                 |
|                                  |
|                                  |
|         **Section4**             |
|                                  |
| GirdView4  GridView4  GridView4  |
| Item1      Item2      Item3      |
|__________________________________|

提前感谢您的帮助!


1 个答案:

答案 0 :(得分:0)

因此,经过一些摆弄,我通过编程方式呈现每个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;
      }
    }
  }

使用如下所示的Apps适配器:

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;
    }
  }

我没有提供有关我如何实施此解决方案的所有具体细节,但如果您想了解更多信息或有任何意见或建议,请告诉我们!