├── LICENSE ├── screenshots ├── 01.png ├── 02.png └── 03.png ├── GridPagerLib ├── .settings │ └── org.eclipse.core.resources.prefs ├── libs │ └── android-support-v4.jar ├── AndroidManifest.xml ├── project.properties ├── res │ └── values │ │ └── attrs.xml ├── proguard-project.txt └── src │ └── com │ └── willli │ └── gridpager │ └── GridViewPager.java ├── GridPagerSample ├── ic_launcher-web.png ├── res │ ├── drawable-hdpi │ │ └── ic_launcher.png │ ├── drawable-mdpi │ │ └── ic_launcher.png │ ├── drawable-xhdpi │ │ └── ic_launcher.png │ ├── drawable-xxhdpi │ │ └── ic_launcher.png │ ├── values-sw600dp │ │ └── dimens.xml │ ├── values │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ ├── menu │ │ └── main.xml │ ├── values-sw720dp-land │ │ └── dimens.xml │ ├── values-v11 │ │ └── styles.xml │ ├── values-v14 │ │ └── styles.xml │ ├── drawable │ │ └── selector_gvp.xml │ └── layout │ │ ├── activity_main.xml │ │ └── item_gvp.xml ├── project.properties ├── proguard-project.txt ├── AndroidManifest.xml └── src │ └── com │ └── willli │ └── gridpager │ └── sample │ └── MainActivity.java ├── README.md └── .gitignore /LICENSE: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /screenshots/01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/109021017/GridViewPager/HEAD/screenshots/01.png -------------------------------------------------------------------------------- /screenshots/02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/109021017/GridViewPager/HEAD/screenshots/02.png -------------------------------------------------------------------------------- /screenshots/03.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/109021017/GridViewPager/HEAD/screenshots/03.png -------------------------------------------------------------------------------- /GridPagerLib/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding/=UTF-8 3 | -------------------------------------------------------------------------------- /GridPagerSample/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/109021017/GridViewPager/HEAD/GridPagerSample/ic_launcher-web.png -------------------------------------------------------------------------------- /GridPagerLib/libs/android-support-v4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/109021017/GridViewPager/HEAD/GridPagerLib/libs/android-support-v4.jar -------------------------------------------------------------------------------- /GridPagerSample/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/109021017/GridViewPager/HEAD/GridPagerSample/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /GridPagerSample/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/109021017/GridViewPager/HEAD/GridPagerSample/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /GridPagerSample/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/109021017/GridViewPager/HEAD/GridPagerSample/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /GridPagerSample/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/109021017/GridViewPager/HEAD/GridPagerSample/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /GridPagerSample/res/values-sw600dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /GridPagerSample/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16dp 5 | 16dp 6 | 7 | 8 | -------------------------------------------------------------------------------- /GridPagerSample/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | GridPagerSample 5 | Settings 6 | Hello world! 7 | 8 | 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | GridViewPager 2 | ============= 3 | 4 | Screenshots 5 | ---------------- 6 | 1. 在旧版布谷提醒中的应用 7 | 8 | ![01](https://raw.github.com/109021017/GridViewPager/master/screenshots/03.png) 9 | 10 | 2. 横屏的支持 11 | 12 | ![02](https://raw.github.com/109021017/GridViewPager/master/screenshots/02.png) 13 | -------------------------------------------------------------------------------- /GridPagerSample/res/menu/main.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /GridPagerSample/res/values-sw720dp-land/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 128dp 8 | 9 | 10 | -------------------------------------------------------------------------------- /GridPagerSample/res/values-v11/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /GridPagerLib/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 9 | 10 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /GridPagerSample/res/values-v14/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # files for the dex VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # generated files 12 | bin/ 13 | gen/ 14 | 15 | # Local configuration file (sdk path, etc) 16 | local.properties 17 | 18 | # Eclipse project files 19 | .classpath 20 | .project 21 | 22 | # Proguard folder generated by Eclipse 23 | proguard/ 24 | 25 | # Intellij project files 26 | *.iml 27 | *.ipr 28 | *.iws 29 | .idea/ 30 | -------------------------------------------------------------------------------- /GridPagerSample/res/drawable/selector_gvp.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /GridPagerLib/project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system edit 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | # 10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): 11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 12 | 13 | # Project target. 14 | target=android-19 15 | android.library=true 16 | -------------------------------------------------------------------------------- /GridPagerLib/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /GridPagerSample/project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system edit 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | # 10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): 11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 12 | 13 | # Project target. 14 | target=android-19 15 | android.library.reference.1=../GridPagerLib 16 | -------------------------------------------------------------------------------- /GridPagerSample/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /GridPagerSample/res/layout/item_gvp.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /GridPagerSample/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 14 | 15 | 16 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /GridPagerLib/proguard-project.txt: -------------------------------------------------------------------------------- 1 | # To enable ProGuard in your project, edit project.properties 2 | # to define the proguard.config property as described in that file. 3 | # 4 | # Add project specific ProGuard rules here. 5 | # By default, the flags in this file are appended to flags specified 6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt 7 | # You can edit the include path and order by changing the ProGuard 8 | # include property in project.properties. 9 | # 10 | # For more details, see 11 | # http://developer.android.com/guide/developing/tools/proguard.html 12 | 13 | # Add any project specific keep options here: 14 | 15 | # If your project uses WebView with JS, uncomment the following 16 | # and specify the fully qualified class name to the JavaScript interface 17 | # class: 18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 19 | # public *; 20 | #} 21 | -------------------------------------------------------------------------------- /GridPagerSample/proguard-project.txt: -------------------------------------------------------------------------------- 1 | # To enable ProGuard in your project, edit project.properties 2 | # to define the proguard.config property as described in that file. 3 | # 4 | # Add project specific ProGuard rules here. 5 | # By default, the flags in this file are appended to flags specified 6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt 7 | # You can edit the include path and order by changing the ProGuard 8 | # include property in project.properties. 9 | # 10 | # For more details, see 11 | # http://developer.android.com/guide/developing/tools/proguard.html 12 | 13 | # Add any project specific keep options here: 14 | 15 | # If your project uses WebView with JS, uncomment the following 16 | # and specify the fully qualified class name to the JavaScript interface 17 | # class: 18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 19 | # public *; 20 | #} 21 | -------------------------------------------------------------------------------- /GridPagerSample/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 10 | 11 | 16 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /GridPagerSample/src/com/willli/gridpager/sample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.willli.gridpager.sample; 2 | 3 | import java.util.Random; 4 | 5 | import android.app.Activity; 6 | import android.os.Bundle; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.view.View.OnClickListener; 10 | import android.widget.BaseAdapter; 11 | import android.widget.TextView; 12 | 13 | import com.willli.gridpager.GridViewPager; 14 | 15 | public class MainActivity extends Activity { 16 | 17 | @Override 18 | protected void onCreate(Bundle savedInstanceState) { 19 | super.onCreate(savedInstanceState); 20 | setContentView(R.layout.activity_main); 21 | GridViewPager mGridPager = (GridViewPager) findViewById(R.id.gvp); 22 | mGridPager.setAdapter(new GridPagerAdapter(34)); 23 | } 24 | 25 | public class GridPagerAdapter extends BaseAdapter { 26 | 27 | int mSize; 28 | Random mRandom; 29 | 30 | public GridPagerAdapter(int size){ 31 | mSize = size; 32 | mRandom = new Random(47); 33 | } 34 | 35 | @Override 36 | public int getCount() { 37 | return mSize; 38 | } 39 | 40 | @Override 41 | public Object getItem(int position) { 42 | return position; 43 | } 44 | 45 | @Override 46 | public long getItemId(int position) { 47 | return position; 48 | } 49 | 50 | @Override 51 | public View getView(int position, View convertView, ViewGroup parent) { 52 | if(convertView == null){ 53 | convertView = getLayoutInflater().inflate(R.layout.item_gvp, null); 54 | } 55 | int color = mRandom.nextInt(0xFFFFFF)+0xFF000000; 56 | convertView.setBackgroundColor(color); 57 | TextView number = (TextView) convertView.findViewById(R.id.tv_position); 58 | number.setText(""+position); 59 | convertView.setOnClickListener(new OnClickListener() { 60 | 61 | @Override 62 | public void onClick(View v) {} 63 | }); 64 | return convertView; 65 | } 66 | 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /GridPagerLib/src/com/willli/gridpager/GridViewPager.java: -------------------------------------------------------------------------------- 1 | package com.willli.gridpager; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import android.content.Context; 7 | import android.content.res.TypedArray; 8 | import android.os.Bundle; 9 | import android.os.Parcelable; 10 | import android.support.v4.view.PagerAdapter; 11 | import android.support.v4.view.ViewPager; 12 | import android.util.AttributeSet; 13 | import android.util.Log; 14 | import android.view.View; 15 | import android.view.ViewGroup; 16 | import android.widget.AdapterView; 17 | import android.widget.BaseAdapter; 18 | import android.widget.LinearLayout; 19 | import android.widget.ListAdapter; 20 | import android.widget.TextView; 21 | 22 | import com.fuwen.gridpager.R; 23 | 24 | public class GridViewPager extends ViewPager { 25 | 26 | private List mGridViewList = null; 27 | 28 | private static final int DEFAULT_COLUMN_NUMBER = 2; 29 | private static final int DEFAULT_ROW_NUMBER = 3; 30 | 31 | private int mRowNumber = 0; 32 | private int mColumnNumber = 0; 33 | 34 | private float mCellMinWidth = 0; 35 | private float mCellMinHeight = 0; 36 | private float mColumnMargin = 0; 37 | private float mRowMargin = 0; 38 | 39 | private BaseAdapter mAdapter; 40 | 41 | private View mEmptyView = null; 42 | 43 | private int mPaddingLeft = 0; 44 | private int mPaddingRight = 0; 45 | 46 | private int mSelection = -1; 47 | 48 | public GridViewPager(Context context) { 49 | this(context, null); 50 | } 51 | 52 | public GridViewPager(Context context, AttributeSet attrs) { 53 | super(context, attrs); 54 | if(attrs != null){ 55 | TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.GridViewPager); 56 | final int N = a.getIndexCount(); 57 | for (int i = 0; i < N; i++) { 58 | int attr = a.getIndex(i); 59 | if (attr == R.styleable.GridViewPager_gvpColumnNumber) { 60 | mColumnNumber = a.getInt(attr, -1); 61 | } else if (attr == R.styleable.GridViewPager_gvpRowNumber) { 62 | mRowNumber = a.getInt(attr, -1); 63 | } else if (attr == R.styleable.GridViewPager_gvpColumnMargin) { 64 | mColumnMargin = a.getDimension(attr, 0); 65 | } else if (attr == R.styleable.GridViewPager_gvpRowMargin) { 66 | mRowMargin = a.getDimension(attr, 0); 67 | } else if (attr == R.styleable.GridViewPager_gvpMinCellWidth) { 68 | mCellMinWidth = a.getDimension(attr, -1); 69 | } else if (attr == R.styleable.GridViewPager_gvpMinCellHeight) { 70 | mCellMinHeight = a.getDimension(attr, -1); 71 | } else if (attr == R.styleable.GridViewPager_android_padding) { 72 | int padding = a.getDimensionPixelSize(attr, 0); 73 | setPadding(padding, padding, padding, padding); 74 | } else if (attr == R.styleable.GridViewPager_android_paddingLeft) { 75 | mPaddingLeft = a.getDimensionPixelSize(attr, 0); 76 | } else if (attr == R.styleable.GridViewPager_android_paddingRight) { 77 | mPaddingRight = a.getDimensionPixelSize(attr, 0); 78 | } 79 | } 80 | if(mColumnNumber <=0 && mCellMinWidth <= 0){ 81 | mColumnNumber = DEFAULT_COLUMN_NUMBER; 82 | } 83 | 84 | if(mRowNumber <=0 && mCellMinHeight <= 0){ 85 | mRowNumber = DEFAULT_ROW_NUMBER; 86 | } 87 | a.recycle(); 88 | } 89 | init(); 90 | } 91 | 92 | private void init(){ 93 | mGridViewList = new ArrayList(); 94 | } 95 | 96 | @Override 97 | public void setPadding(int left, int top, int right, int bottom) { 98 | mPaddingLeft = left; 99 | mPaddingRight = right; 100 | super.setPadding(0, top, 0, bottom); 101 | } 102 | 103 | @Override 104 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 105 | // 设置宽度和高度 106 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 107 | int columnNumberOld = mColumnNumber; 108 | int rowNumberOld = mRowNumber; 109 | if (mCellMinWidth > 0) { 110 | mColumnNumber = (int) Math 111 | .floor((MeasureSpec.getSize(widthMeasureSpec) 112 | + mColumnMargin - mPaddingLeft - mPaddingRight) 113 | / (mCellMinWidth + mColumnMargin)); 114 | } 115 | if(mCellMinHeight > 0){ 116 | mRowNumber = (int) Math 117 | .floor((MeasureSpec.getSize(heightMeasureSpec) 118 | + mRowMargin) 119 | / (mCellMinHeight + mRowMargin)); 120 | } 121 | if(rowNumberOld != mRowNumber || columnNumberOld != mColumnNumber){ 122 | resetAdapter(); 123 | } 124 | } 125 | 126 | public int getPageCount(){ 127 | return mGridViewList.size(); 128 | } 129 | 130 | public int getPageSize(){ 131 | return mColumnNumber*mRowNumber; 132 | } 133 | 134 | public void setSelection(int position) { 135 | final int pageSize = getPageSize(); 136 | if(mAdapter==null || pageSize<=0){ 137 | mSelection = position; 138 | return; 139 | } 140 | mSelection = -1; 141 | setCurrentItem(position/pageSize, true); 142 | } 143 | 144 | public int getSelection(){ 145 | return getCurrentItem()*getPageSize(); 146 | } 147 | 148 | @Override 149 | public Parcelable onSaveInstanceState() { 150 | Bundle bundle = new Bundle(); 151 | bundle.putParcelable("instanceState", super.onSaveInstanceState()); 152 | bundle.putInt("selection", getSelection()); 153 | return bundle; 154 | } 155 | 156 | @Override 157 | public void onRestoreInstanceState(Parcelable state) { 158 | if (state instanceof Bundle) { 159 | Bundle bundle = (Bundle) state; 160 | this.mSelection = bundle.getInt("selection"); 161 | state = bundle.getParcelable("instanceState"); 162 | } 163 | super.onRestoreInstanceState(state); 164 | } 165 | 166 | public void setEmptyView(TextView emptyView) { 167 | mEmptyView = emptyView; 168 | } 169 | 170 | public void setAdapter(BaseAdapter adapter){ 171 | mAdapter = adapter; 172 | resetAdapter(); 173 | } 174 | 175 | public void notifyDataSetChanged(){ 176 | resetAdapter(); 177 | } 178 | 179 | private void resetAdapter() { 180 | int pageSize = mColumnNumber*mRowNumber; 181 | if(pageSize <= 0) 182 | return; 183 | 184 | if(mAdapter.getCount() == 0){ 185 | mGridViewList.removeAll(mGridViewList); 186 | if(mEmptyView != null) 187 | mEmptyView.setVisibility(View.VISIBLE); 188 | }else{ 189 | if(mEmptyView != null) 190 | mEmptyView.setVisibility(View.GONE); 191 | } 192 | int pageCount = mAdapter.getCount()/pageSize; 193 | if(mAdapter.getCount()%pageSize == 0){ 194 | pageCount--; 195 | } 196 | int listSize = mGridViewList.size()-1; 197 | BGGridView gridview; 198 | GridAdapter gridAdapter; 199 | for(int i=0;i<=Math.max(listSize, pageCount);i++){ 200 | if(i<=listSize&&i<=pageCount){ 201 | gridview = mGridViewList.get(i); 202 | gridAdapter = new GridAdapter(i,pageSize,mAdapter); 203 | gridview.setAdapter(gridAdapter); 204 | mGridViewList.set(i, gridview); 205 | continue; 206 | } 207 | if(i>listSize&&i<=pageCount){ 208 | gridview = new BGGridView(); 209 | gridAdapter = new GridAdapter(i,pageSize,mAdapter); 210 | gridview.setAdapter(gridAdapter); 211 | mGridViewList.add(gridview); 212 | continue; 213 | } 214 | if(i>pageCount&&i<=listSize){ 215 | mGridViewList.remove(pageCount+1); 216 | continue; 217 | } 218 | } 219 | super.setAdapter(new GridPagerAdapter()); 220 | if(mSelection >= 0) 221 | setSelection(mSelection); 222 | } 223 | 224 | private class GridPagerAdapter extends PagerAdapter { 225 | 226 | @Override 227 | public int getCount() { 228 | return mGridViewList.size(); 229 | } 230 | 231 | @Override 232 | public boolean isViewFromObject(View arg0, Object arg1) { 233 | return arg0 == arg1; 234 | } 235 | 236 | @Override 237 | public void destroyItem(ViewGroup container, int position, Object object) { 238 | container.removeView((View)object); 239 | } 240 | 241 | @Override 242 | public Object instantiateItem(ViewGroup container, int position) { 243 | container.addView(mGridViewList.get(position),new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 244 | return mGridViewList.get(position); 245 | } 246 | } 247 | 248 | private class GridAdapter extends BaseAdapter{ 249 | int mPage; 250 | int mSize; 251 | BaseAdapter mAdapter; 252 | public GridAdapter(int page,int size,BaseAdapter adapter){ 253 | mPage = page; 254 | mSize = size; 255 | mAdapter = adapter; 256 | } 257 | @Override 258 | public int getCount() { 259 | if(mAdapter.getCount()%mSize==0) 260 | return mSize; 261 | else if(mPage < mAdapter.getCount()/mSize){ 262 | return mSize; 263 | }else{ 264 | return mAdapter.getCount()%mSize; 265 | } 266 | } 267 | 268 | @Override 269 | public Object getItem(int position) { 270 | return mAdapter.getItem(mPage*mSize+position); 271 | } 272 | 273 | @Override 274 | public long getItemId(int position) { 275 | return mAdapter.getItemId(mPage*mSize+position); 276 | } 277 | 278 | @Override 279 | public View getView(int position, View convertView, ViewGroup parent) { 280 | return mAdapter.getView(mPage*mSize+position, convertView, parent); 281 | } 282 | 283 | } 284 | 285 | 286 | public class BGGridView extends AdapterView{ 287 | 288 | private ListAdapter mAdapter; 289 | 290 | public BGGridView() { 291 | super(GridViewPager.this.getContext()); 292 | } 293 | 294 | /** 295 | * 继承AdapterView需要实现以下四个方法 296 | * getAdapter() 297 | * setAdapter(ListAdapter adapter) 298 | * getSelectedView() 299 | * setSelection(int position) 300 | */ 301 | @Override 302 | public ListAdapter getAdapter() { 303 | return mAdapter; 304 | } 305 | 306 | @Override 307 | public void setAdapter(ListAdapter adapter) { 308 | this.mAdapter = adapter; 309 | //把所有的child添加到布局中 310 | int oldChildCount = getChildCount(); 311 | int newChildCount = mAdapter.getCount(); 312 | 313 | for(int i=0 ; i 0){ 322 | removeViewsInLayout(newChildCount, d); 323 | } 324 | } 325 | 326 | @Override 327 | public View getSelectedView() { 328 | if(getChildCount()>0){ 329 | return getChildAt(0); 330 | } 331 | return null; 332 | } 333 | 334 | @Override 335 | public void setSelection(int position) { } 336 | 337 | 338 | @Override 339 | public int getPaddingLeft() { 340 | return mPaddingLeft; 341 | } 342 | 343 | @Override 344 | public int getPaddingRight() { 345 | return mPaddingRight; 346 | } 347 | 348 | /** 349 | * 设置大小 350 | */ 351 | @Override 352 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 353 | //设置宽度和高度 354 | int childWidth = (int)(MeasureSpec.getSize(widthMeasureSpec)-mColumnMargin*(mColumnNumber-1)-getPaddingLeft() - getPaddingRight())/mColumnNumber; 355 | int childHeight = (int)(MeasureSpec.getSize(heightMeasureSpec)-mRowMargin*(mRowNumber-1))/mRowNumber; 356 | for(int i = 0;i