简体   繁体   English

Android GridView适配器可填充多个部分

[英]Android GridView Adapter to Populate Multiple Sections

It is my desire to use multiple adaptable GridViews to populate several sections on the screen with different data. 我希望使用多个可适应的GridView,以使用不同的数据填充屏幕上的多个部分。 There is an undetermined amount of sections, and items in each section so the solution must be flexible. 节的数量不确定,每个节中都有项目,因此解决方案必须灵活。 Each section will have a section header title and its own gridview, filled with an unknown number of items. 每个节都有一个节标题和其自己的网格视图,其中填充了未知数量的项目。 These elements must be clickable to take you to another screen for that element. 这些元素必须可单击才能转到该元素的另一个屏幕。 So the desired screen would look something like this: 因此,所需的屏幕将如下所示:

 __________________________________
|         **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      |
|__________________________________|

Thanks in advance for the help! 先谢谢您的帮助!


So after some fiddling around I figured out a solution to my question by programmatically rendering each gridview and header: 因此,经过一番摆弄之后,我通过以编程方式呈现每个gridview和header找出了问题的解决方案:

Using a basic layout like this (since the number of gridviews needed is unknown): 使用这样的基本布局(因为所需的网格视图数量未知):

<?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>

And a buildAdapter method in the fragment like so: 片段中的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;
      }
    }
  }

With an Apps adapter that looks like this: 使用看起来像这样的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;
    }
  }

I did not include all the specific details of how I implemented this solution, but if you would be curious to learn more or have any comments or suggestions please let me know! 我没有提供有关如何实现此解决方案的所有具体细节,但是如果您想了解更多信息或有任何意见或建议,请告诉我!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM