Created
February 1, 2013 05:48
-
-
Save holmeszyx/4689562 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class SearchAppAdapter extends PagerAdapter{ | |
| public static final int DEFAULT_MAX_ITEM_SIZE = 4; | |
| /** | |
| * 全部数据集 | |
| */ | |
| private List<AppInfo> data; | |
| private Context mContext; | |
| private int mMaxItemSize = DEFAULT_MAX_ITEM_SIZE; | |
| /** | |
| * 页面数据集 | |
| */ | |
| private List<List<AppInfo>> pageData; | |
| private SparseArray<WeakReference<View>> mViewCache; | |
| public SearchAppAdapter(Context context, List<AppInfo> data){ | |
| mContext = context; | |
| this.data = data; | |
| pageData = new ArrayList<List<AppInfo>>(); | |
| mViewCache = new SparseArray<WeakReference<View>>(); | |
| fillPageData(); | |
| } | |
| private void fillPageData(){ | |
| List<AppInfo> page = null; | |
| for (int i = 0; i < data.size(); i ++){ | |
| if (i % mMaxItemSize == 0){ | |
| page = new ArrayList<AppInfo>(mMaxItemSize); | |
| } | |
| if (page != null) | |
| page.add(data.get(i)); | |
| if (i % mMaxItemSize == mMaxItemSize -1 || i == data.size() - 1){ | |
| pageData.add(page); | |
| } | |
| } | |
| } | |
| @Override | |
| public int getCount() { | |
| // TODO Auto-generated method stub | |
| return pageData.size(); | |
| } | |
| @Override | |
| public boolean isViewFromObject(View view, Object object) { | |
| // TODO Auto-generated method stub | |
| return view == object; | |
| } | |
| @Override | |
| public void destroyItem(ViewGroup container, int position, Object object) { | |
| // TODO Auto-generated method stub | |
| if (object instanceof GridView){ | |
| container.removeView((View) object); | |
| // System.out.println("remove"); | |
| } | |
| } | |
| @Override | |
| public Object instantiateItem(ViewGroup container, int position) { | |
| // TODO Auto-generated method stub | |
| //return super.instantiateItem(container, position); | |
| // System.out.println("add----" + position); | |
| WeakReference<View> vr = mViewCache.get(position); | |
| if (vr != null){ | |
| View v = vr.get(); | |
| if (v != null){ | |
| container.addView(v); | |
| return v; | |
| } | |
| mViewCache.remove(position); | |
| } | |
| GridView grid = new OneRowGridView(mContext); | |
| grid.setStretchMode(GridView.STRETCH_COLUMN_WIDTH); | |
| grid.setNumColumns(mMaxItemSize); | |
| grid.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); | |
| grid.setHorizontalSpacing(6); | |
| grid.setCacheColorHint(Color.TRANSPARENT); | |
| grid.setSelector(R.drawable.bg_transparent); | |
| GridAppAdapter adapter = new GridAppAdapter(mContext, pageData.get(position)); | |
| grid.setAdapter(adapter); | |
| grid.setOnItemClickListener(new SimpleAppItemClickListener(mContext, adapter)); | |
| container.addView(grid); | |
| mViewCache.put(position, new WeakReference<View>(grid)); | |
| return grid; | |
| } | |
| @Override | |
| public void notifyDataSetChanged() { | |
| // TODO Auto-generated method stub | |
| mViewCache.clear(); | |
| super.notifyDataSetChanged(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment