java - 用于填充多个部分的 Android GridView 适配器

标签 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      |
|__________________________________|

预先感谢您的帮助!

<小时/>

最佳答案

经过一番摆弄后,我通过以编程方式渲染每个 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>

fragment 中的 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;
    }
  }

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

关于java - 用于填充多个部分的 Android GridView 适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20977210/

相关文章:

java - TCP 按字节发送/检索 header

java - 如何始终在 Recyclerview 中显示完整项目而不是项目的一部分?

android - 如果拍摄图像时方向发生变化,RxImagePicker 不会调用完成

java - 在 kotlin 中将 String 转换为 JsonObject 返回 null

android - 当我按下后退按钮时关闭 VideoView 中的视频

Java CompareTo 方法失败

java - 我正在使用适用于 Android 的 OData4j,我无法获取实体

android - 无需本地消息应用程序即可发送和接收短信

asp.net - 使用哪个 ASP.NET 服务器控件?

php - Yii - 在 GridView 单元格内渲染小部件