我想用一個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;
}}
我有一些問題,第二個適配器。
你可以只嘗試它,如果它不工作,請諮詢詳細的問題與你做了什麼,一個清楚地說明什麼不起作用。是的,您需要爲每個列表/網格添加一個適配器。 –