2017-07-05 85 views
0

我想用一個GridView的佈局,使網格佈局的每一個項目都有自己的ListView。我一直在尋找代碼示例,但我還沒有找到任何代碼。我應該爲GridView和ListView使用不同的適配器類嗎? 提前謝謝!的ListView裏面的GridView

編輯:我已經創建了一個顯示所有的一些瓷磚的GridView中,我希望每一個網格項目列入清單,讓我增加了一個ListView到GridView項目的佈局。不幸的是,我有問題連接到適配器的列表視圖。

這是適配器在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; 
}} 

這裏是適配器列表視圖的:

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

我有一些問題,第二個適配器。

+0

你可以只嘗試它,如果它不工作,請諮詢詳細的問題與你做了什麼,一個清楚地說明什麼不起作用。是的,您需要爲每個列表/網格添加一個適配器。 –

回答

0

答案是肯定的。 也有另一種解決方案。

  • 將相對/線性佈局添加爲GridView的項目。
  • 在適配器類的GridView的,在@getView添加子項目到() 動態。 但我不推薦它,因爲性能。加載此類視圖將需要很長時間。

寫你需要什麼,然後我將更新到我的回答另一種解決方案(如果可能) 好運

0

你只爲它創建一個GridView的Android setAdapter。在這個適配器,創建佈局。其中有一個listView的xml,然後像往常一樣。

PS:如果你使用layout_width =「warp_content」你不會得到嵌套的ListView真正的高度。因爲嵌套的ListView無法與「warp_content得到真正的高度

衡量它會被罰款。