问题
Libraries:
- https://github.com/commonsguy/cwac-merge
- https://github.com/maurycyw/StaggeredGridView
- https://github.com/chrisbanes/Android-PullToRefresh
I'd like to add a gridview adapter with MergeAdapter.
1st way, If you set adapter:
plv = (PullToRefreshListView) LayoutInflater.from(context).inflate(
R.layout.layout_listview_in_viewpager, container, false);
adapter = new MergeAdapter();
sadapter = new StaggeredAdapter(BaseSampleActivity.this, R.id.imageView1, urls);
adapter.addAdapter(sadapter);
plv.setAdapter(adapter);
Then it works like a list view.
2nd way, if you build a view:
plv = (PullToRefreshListView) LayoutInflater.from(context).inflate(
R.layout.layout_listview_in_viewpager, container, false);
adapter = new MergeAdapter();
adapter.addView(buildlabel3(context));
plv.setAdapter(adapter);
public View buildlabel3(Context context) {
// TODO Auto-generated method stub
RelativeLayout v = (RelativeLayout)LayoutInflater.from(context).inflate(R.layout.activity_main, null);
StaggeredGridView gridView = (StaggeredGridView) v.findViewById(R.id.staggeredGridView1);
int margin = getResources().getDimensionPixelSize(R.dimen.margin);
gridView.setItemMargin(margin); // set the GridView margin
gridView.setPadding(margin, 0, margin, 0); // have the margin on the sides as well
StaggeredAdapter sadapter = new StaggeredAdapter(BaseSampleActivity.this, R.id.imageView1, urls);
gridView.setAdapter(sadapter);
sadapter.notifyDataSetChanged();
Log.e("view", v.toString());
return v;
}
Then it doesn't show anything and with no error.
回答1:
addView()
on MergeAdapter
is designed for ListView
headers and the like. You, instead, are attempting to put a GridView
as an element inside of some other AdapterView
that is using the MergeAdapter
. That is not supported by MergeAdapter
, and putting a GridView
inside of another AdapterView
is unlikely to work no matter how you try to do it.
Also, I do not recommend making an Adapter
be static
, and certainly not a MergeAdapter
, as it would represent a significant memory leak (and is unnecessary in the first place).
回答2:
You problem is very generic. As in your code
private static MergeAdapter adapter = null;
adapter.addView(buildlabel3(context));
adapter is null, and not initialised. So whatever you add to it, it still null.
I have seen the soucre code of MergeAdapter. It has a constructor:
public MergeAdapter() {
super();
}
So just change your code to
private static MergeAdapter adapter = new MergeAdapter();
adapter.addView(buildlabel3(context));
and then try again.
来源:https://stackoverflow.com/questions/18197626/mergeadapter-with-gridview