GridView中的ListView

时间:2017-07-05 16:35:28

标签: android listview gridview android-adapter

我想使用GridView布局,以便Gridlayout的每个项目都有自己的ListView。我一直在寻找代码示例,但我还没找到。我应该为GridView和ListView使用不同的适配器类吗? 提前谢谢!

编辑:我创建了一个显示所有部分图块的GridView,我希望每个网格项都包含一个列表,所以我在GridView项目的布局中添加了一个ListView。不幸的是,我在将listview连接到适配器时遇到了问题。

这是gridView的适配器

public class MainViewAdapter extends ArrayAdapter<Category> {
private Context mContext;
private int layoutResourceId;
private ArrayList<Category> mGridData = new ArrayList<Category>();
ArrayAdapter adapter;

public MainViewAdapter(Context mContext, int layoutResourceId, ArrayList<Category> mGridData) {
    super(mContext, layoutResourceId, mGridData);
    this.layoutResourceId = layoutResourceId;
    this.mContext = mContext;
    this.mGridData = mGridData;
}

public void setGridData(ArrayList<Category> mGridData) {
    this.mGridData = mGridData;
    notifyDataSetChanged();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ViewHolder holder;
    String imageUrl;

    if (row == null) {
        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new ViewHolder();
        holder.SubListView = (ListView) row.findViewById(R.id.SubList);
        holder.titleTextView = (TextView) row.findViewById(R.id.category_title);
        holder.simpleDraweeView = (SimpleDraweeView) row.findViewById(R.id.category_image);
        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }

    Category category = mGridData.get(position);
    Log.i("position", "" + position);
    holder.titleTextView.setText(Html.fromHtml(category.getTitle()));
    imageUrl = category.getImage();
    Log.i("image url",imageUrl );
    Uri imageUri = Uri.parse(imageUrl);
    holder.simpleDraweeView.setImageURI(imageUri);

    List SubCategories = category.getSubCateg();
    Log.i("@MainViewAdapter - SubCategories", category.getSubCateg().toString());

    //list SubCategories to ArrayList
    ArrayList<SubCategory> listOfSub = new ArrayList<>(SubCategories.size());
    listOfSub.addAll(SubCategories);
    Log.i("listOfSub",listOfSub.toString());

    //ArrayList to ListView
    holder.SubListView.setAdapter(adapter);

    return row;
}

static class ViewHolder {
    TextView titleTextView;
    SimpleDraweeView simpleDraweeView;
    ListView SubListView;
}}

这是listview的适配器

public class SubListAdapter extends ArrayAdapter<SubCategory> {
private int layoutResourceId;
private ArrayList<SubCategory> mSubGridData = new ArrayList<SubCategory>();
ArrayAdapter adapter;

private final Context mContext;

public SubListAdapter(Context mContext, int layoutResourceId, ArrayList<SubCategory> mSubGridData) {
    super(mContext, R.layout.subcategory_item, mSubGridData);

    this.layoutResourceId = layoutResourceId;
    this.mContext = mContext;
    this.mSubGridData = mSubGridData;
}

public void setSubGridData(ArrayList<SubCategory> mSubGridData) {
    this.mSubGridData = mSubGridData;
    notifyDataSetChanged();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row = convertView;
    SubListAdapter.ViewHolder holder;
    String imageUrl;

    if (row == null) {
        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new ViewHolder();
        holder.SubTitleTextView = (TextView) row.findViewById(R.id.subCatItemTitle);
        row.setTag(holder);
    } else {
        holder = (SubListAdapter.ViewHolder) row.getTag();

    return rowView;
}

static class ViewHolder {
    TextView SubTitleTextView;
    ListView SubListView;
}}

我遇到第二个适配器的问题。

2 个答案:

答案 0 :(得分:0)

答案是肯定的。 还有另一个解决方案。

  • 将相对/线性布局添加为GridView的项目。
  • 在GridView的适配器类中,将子项添加到@getView()中 动态。 但由于性能的原因,我不推荐它。加载此类型视图需要很长时间。

写下你需要什么然后我会更新我的答案以获得另一种解决方案(如果可能的话) 祝你好运

答案 1 :(得分:0)

你只需为它创建一个GridView android setAdapter。在这个适配器中,创建一个layout.xml,里面有一个listView。然后像往常一样。

ps:如果你使用layout_width =&#34; warp_content&#34;你将无法获得嵌套listView的真实高度。因为嵌套的listView无法通过&#34; warp_content

获得真正的高度

测量它会没事的。