├── sample ├── .gitignore ├── src │ └── main │ │ ├── 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 │ │ │ ├── dimens.xml │ │ │ ├── styles.xml │ │ │ └── strings.xml │ │ ├── values-w820dp │ │ │ └── dimens.xml │ │ ├── menu │ │ │ └── main.xml │ │ └── layout │ │ │ └── activity_main.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ └── com │ │ └── tundem │ │ └── widget │ │ └── gridview │ │ └── sample │ │ ├── MainActivity.java │ │ └── SampleAdapter.java ├── build.gradle └── proguard-rules.txt ├── library ├── .gitignore ├── src │ └── main │ │ ├── res │ │ └── values │ │ │ ├── strings.xml │ │ │ └── info_strings.xml │ │ ├── java │ │ └── com │ │ │ └── tundem │ │ │ └── widget │ │ │ └── gridview │ │ │ ├── listener │ │ │ └── AnimationListener.java │ │ │ ├── helper │ │ │ ├── AnimationHelper.java │ │ │ └── Helper.java │ │ │ ├── animation │ │ │ ├── ScaleUpAnimation.java │ │ │ └── ScaleDownAnimation.java │ │ │ ├── adapter │ │ │ └── AnimatedAdapter.java │ │ │ ├── IAnimatedGridView.java │ │ │ ├── AnimatedGridView.java │ │ │ ├── AnimatedHeaderGridView.java │ │ │ └── HeaderGridView.java │ │ └── AndroidManifest.xml ├── build.gradle ├── proguard-rules.txt ├── gradle.properties └── gradle-mvn-push.gradle ├── settings.gradle ├── .gitignore ├── gradle.properties ├── README.md └── LICENSE /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':library', ':sample' 2 | -------------------------------------------------------------------------------- /library/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | AnimatedGridView 3 | 4 | -------------------------------------------------------------------------------- /sample/src/main/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mikepenz/AnimatedGridView/HEAD/sample/src/main/ic_launcher-web.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mikepenz/AnimatedGridView/HEAD/sample/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mikepenz/AnimatedGridView/HEAD/sample/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mikepenz/AnimatedGridView/HEAD/sample/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mikepenz/AnimatedGridView/HEAD/sample/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /library/src/main/java/com/tundem/widget/gridview/listener/AnimationListener.java: -------------------------------------------------------------------------------- 1 | package com.tundem.widget.gridview.listener; 2 | 3 | public interface AnimationListener { 4 | public void onAnimationFinish(); 5 | } -------------------------------------------------------------------------------- /sample/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /sample/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /sample/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Sample 5 | Hello world! 6 | Settings 7 | 8 | 9 | -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | 19 | # Local configuration file (sdk path, etc) 20 | local.properties 21 | 22 | # Proguard folder generated by Eclipse 23 | proguard/ 24 | gradlew 25 | gradlew.bat 26 | -------------------------------------------------------------------------------- /sample/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /sample/src/main/res/menu/main.xml: -------------------------------------------------------------------------------- 1 | 4 | 6 | 8 | 10 | 11 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'android' 2 | 3 | android { 4 | compileSdkVersion 19 5 | buildToolsVersion '19.1.0' 6 | defaultConfig { 7 | minSdkVersion 15 8 | targetSdkVersion 19 9 | versionCode 1 10 | versionName '1.0' 11 | } 12 | buildTypes { 13 | release { 14 | runProguard false 15 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' 16 | } 17 | } 18 | productFlavors { 19 | } 20 | } 21 | 22 | dependencies { 23 | compile project(':library') 24 | } 25 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'android-library' 2 | 3 | android { 4 | compileSdkVersion 19 5 | buildToolsVersion '19.1.0' 6 | defaultConfig { 7 | minSdkVersion 14 8 | targetSdkVersion 19 9 | versionCode 113 10 | versionName '1.1.3' 11 | } 12 | buildTypes { 13 | release { 14 | runProguard false 15 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' 16 | } 17 | } 18 | productFlavors { 19 | } 20 | } 21 | apply from: 'gradle-mvn-push.gradle' 22 | 23 | dependencies { 24 | compile 'com.android.support:support-v4:+' 25 | } -------------------------------------------------------------------------------- /sample/proguard-rules.txt: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Entwicklung/android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the ProGuard 5 | # include property in project.properties. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} -------------------------------------------------------------------------------- /library/proguard-rules.txt: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Entwicklung/android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the ProGuard 5 | # include property in project.properties. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /library/gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Settings specified in this file will override any Gradle settings 5 | # configured through the IDE. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true 19 | 20 | POM_NAME=AnimatedGridView Library 21 | POM_ARTIFACT_ID=library 22 | POM_PACKAGING=aar -------------------------------------------------------------------------------- /library/src/main/java/com/tundem/widget/gridview/helper/AnimationHelper.java: -------------------------------------------------------------------------------- 1 | package com.tundem.widget.gridview.helper; 2 | 3 | import android.view.animation.AlphaAnimation; 4 | import android.view.animation.Animation; 5 | import android.view.animation.AnimationSet; 6 | import android.view.animation.GridLayoutAnimationController; 7 | import android.view.animation.TranslateAnimation; 8 | 9 | /** 10 | * Created by mikepenz on 31.05.14. 11 | */ 12 | public class AnimationHelper { 13 | 14 | public static GridLayoutAnimationController getLayoutAnimation() { 15 | AnimationSet set = new AnimationSet(true); 16 | Animation animation = new AlphaAnimation(0.0f, 1.0f); 17 | animation.setDuration(40); 18 | set.addAnimation(animation); 19 | animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 20 | 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, 21 | Animation.RELATIVE_TO_SELF, -1.0f, 22 | Animation.RELATIVE_TO_SELF, 0.0f); 23 | animation.setDuration(75); 24 | set.addAnimation(animation); 25 | GridLayoutAnimationController controller = new GridLayoutAnimationController(set); 26 | return controller; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /library/src/main/java/com/tundem/widget/gridview/animation/ScaleUpAnimation.java: -------------------------------------------------------------------------------- 1 | package com.tundem.widget.gridview.animation; 2 | 3 | import android.view.View; 4 | import android.view.animation.Animation; 5 | import android.view.animation.Transformation; 6 | 7 | public class ScaleUpAnimation extends Animation { 8 | 9 | int mToHeight; 10 | View mView; 11 | 12 | public ScaleUpAnimation(View view) { 13 | this.mView = view; 14 | this.mToHeight = view.getHeight(); 15 | } 16 | 17 | @Override 18 | protected void applyTransformation(float interpolatedTime, Transformation t) { 19 | int newHeight; 20 | newHeight = (int) (mToHeight * interpolatedTime); 21 | mView.getLayoutParams().height = newHeight; 22 | mView.requestLayout(); 23 | 24 | if (interpolatedTime == 1) { 25 | mView.invalidate(); 26 | mView.clearAnimation(); 27 | } 28 | } 29 | 30 | @Override 31 | public void initialize(int width, int height, int parentWidth, 32 | int parentHeight) { 33 | super.initialize(width, height, parentWidth, parentHeight); 34 | } 35 | 36 | @Override 37 | public boolean willChangeBounds() { 38 | return true; 39 | } 40 | } -------------------------------------------------------------------------------- /library/src/main/java/com/tundem/widget/gridview/animation/ScaleDownAnimation.java: -------------------------------------------------------------------------------- 1 | package com.tundem.widget.gridview.animation; 2 | 3 | import android.view.View; 4 | import android.view.animation.Animation; 5 | import android.view.animation.Transformation; 6 | 7 | public class ScaleDownAnimation extends Animation { 8 | 9 | int mFromHeight; 10 | View mView; 11 | 12 | public ScaleDownAnimation(View view) { 13 | this.mView = view; 14 | this.mFromHeight = view.getHeight(); 15 | } 16 | 17 | @Override 18 | protected void applyTransformation(float interpolatedTime, Transformation t) { 19 | int newHeight; 20 | newHeight = (int) (mFromHeight * (1 - interpolatedTime)); 21 | mView.getLayoutParams().height = newHeight; 22 | mView.requestLayout(); 23 | 24 | if (interpolatedTime == 1) { 25 | mView.invalidate(); 26 | mView.clearAnimation(); 27 | } 28 | } 29 | 30 | @Override 31 | public void initialize(int width, int height, int parentWidth, 32 | int parentHeight) { 33 | super.initialize(width, height, parentWidth, parentHeight); 34 | } 35 | 36 | @Override 37 | public boolean willChangeBounds() { 38 | return true; 39 | } 40 | } -------------------------------------------------------------------------------- /library/src/main/res/values/info_strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Mike Penz 5 | http://mikepenz.com/ 6 | AnimatedGridView 7 | AnimatedGridView extends the default GridView and adds some new functions to animate the deletion 8 | of rows. You just have to choose the AnimatedGridView and extend the AnimatedAdapter (which extends the BaseAdapter). 9 | \n\n 10 | This library also adds a HeaderGridView, like the normal ListView. The HeaderGridView is from the Google Source Code of their gallery app. 11 | 12 | 1.1.3 13 | https://github.com/mikepenz/AnimatedGridView/ 14 | apache_2_0 15 | true 16 | https://github.com/mikepenz/AnimatedGridView 17 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Settings specified in this file will override any Gradle settings 5 | # configured through the IDE. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true 19 | 20 | VERSION_NAME=1.1.3-SNAPSHOT 21 | VERSION_CODE=113 22 | GROUP=com.tundem.widget.gridview 23 | 24 | POM_DESCRIPTION=AnimatedGridView Library 25 | POM_URL=https://github.com/mikepenz/AnimatedGridView 26 | POM_SCM_URL=https://github.com/mikepenz/AnimatedGridView 27 | POM_SCM_CONNECTION=scm:git@github.com:mikepenz/AnimatedGridView.git 28 | POM_SCM_DEV_CONNECTION=scm:git@github.com:mikepenz/AnimatedGridView.git 29 | POM_LICENCE_NAME=The Apache Software License, Version 2.0 30 | POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt 31 | POM_LICENCE_DIST=repo 32 | POM_DEVELOPER_ID=mikepenz 33 | POM_DEVELOPER_NAME=Mike Penz -------------------------------------------------------------------------------- /sample/src/main/java/com/tundem/widget/gridview/sample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.tundem.widget.gridview.sample; 2 | 3 | import android.app.Activity; 4 | import android.os.Bundle; 5 | import android.view.Menu; 6 | import android.view.MenuItem; 7 | 8 | import com.tundem.widget.gridview.AnimatedGridView; 9 | 10 | import java.util.LinkedList; 11 | import java.util.TreeSet; 12 | 13 | 14 | public class MainActivity extends Activity { 15 | 16 | AnimatedGridView agv; 17 | 18 | @Override 19 | protected void onCreate(Bundle savedInstanceState) { 20 | super.onCreate(savedInstanceState); 21 | setContentView(R.layout.activity_main); 22 | 23 | agv = (AnimatedGridView) findViewById(R.id.gridview); 24 | agv.setAdapter(new SampleAdapter()); 25 | } 26 | 27 | 28 | @Override 29 | public boolean onCreateOptionsMenu(Menu menu) { 30 | // Inflate the menu; this adds items to the action bar if it is present. 31 | getMenuInflater().inflate(R.menu.main, menu); 32 | return true; 33 | } 34 | 35 | @Override 36 | public boolean onOptionsItemSelected(MenuItem item) { 37 | // Handle action bar item clicks here. The action bar will 38 | // automatically handle clicks on the Home/Up button, so long 39 | // as you specify a parent activity in AndroidManifest.xml. 40 | int id = item.getItemId(); 41 | if (id == R.id.action_remove) { 42 | if (agv.getCount() >= 6) { 43 | TreeSet row = new TreeSet(); 44 | row.add(0); 45 | agv.animateDeleteRow(row, 200); 46 | } 47 | 48 | return true; 49 | } else if (id == R.id.action_add) { 50 | LinkedList items = new LinkedList(); 51 | items.add(1); 52 | items.add(2); 53 | items.add(3); 54 | items.add(4); 55 | items.add(5); 56 | items.add(6); 57 | agv.animateAddCells(items, 200); 58 | } 59 | return super.onOptionsItemSelected(item); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /library/src/main/java/com/tundem/widget/gridview/adapter/AnimatedAdapter.java: -------------------------------------------------------------------------------- 1 | package com.tundem.widget.gridview.adapter; 2 | 3 | import android.support.v4.view.ViewCompat; 4 | import android.view.View; 5 | import android.view.ViewGroup; 6 | import android.view.animation.Animation; 7 | import android.widget.BaseAdapter; 8 | 9 | import com.tundem.widget.gridview.animation.ScaleUpAnimation; 10 | 11 | import java.util.LinkedList; 12 | import java.util.List; 13 | 14 | /** 15 | * Created by mikepenz on 16.05.14. 16 | */ 17 | public abstract class AnimatedAdapter extends BaseAdapter { 18 | public int duration = 500; 19 | private List newFieldsToAnimate = new LinkedList(); 20 | 21 | abstract public void removeItem(int position); 22 | 23 | public void addItem(T item, boolean visible) { 24 | if (visible) { 25 | newFieldsToAnimate.add(getCount()); 26 | } 27 | } 28 | 29 | @Override 30 | public View getView(int i, final View view, ViewGroup parent) { 31 | 32 | if (view != null && newFieldsToAnimate.contains(i)) { 33 | ViewCompat.setHasTransientState(view, true); 34 | ScaleUpAnimation fd = new ScaleUpAnimation(view); 35 | view.getLayoutParams().height = 0; 36 | fd.setDuration(duration); 37 | fd.setAnimationListener(new Animation.AnimationListener() { 38 | @Override 39 | public void onAnimationStart(Animation animation) { 40 | 41 | } 42 | 43 | @Override 44 | public void onAnimationEnd(Animation animation) { 45 | ViewCompat.setHasTransientState(view, false); 46 | view.clearAnimation(); 47 | view.invalidate(); 48 | } 49 | 50 | @Override 51 | public void onAnimationRepeat(Animation animation) { 52 | 53 | } 54 | }); 55 | view.startAnimation(fd); 56 | newFieldsToAnimate.remove(newFieldsToAnimate.indexOf(i)); 57 | } 58 | 59 | return view; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /library/src/main/java/com/tundem/widget/gridview/IAnimatedGridView.java: -------------------------------------------------------------------------------- 1 | package com.tundem.widget.gridview; 2 | 3 | import android.view.View; 4 | import android.widget.Adapter; 5 | import android.widget.BaseAdapter; 6 | 7 | import com.tundem.widget.gridview.adapter.AnimatedAdapter; 8 | import com.tundem.widget.gridview.listener.AnimationListener; 9 | 10 | import java.util.LinkedList; 11 | import java.util.Set; 12 | 13 | /** 14 | * Created by mikepenz on 31.05.14. 15 | */ 16 | public interface IAnimatedGridView { 17 | /*** 18 | * ANIMATION METHODS ;D 19 | */ 20 | 21 | /** 22 | * @param cells 23 | * @param duration 24 | */ 25 | public void animateAddCells(LinkedList cells, int duration); 26 | 27 | /** 28 | * @param rows 29 | * @param duration 30 | */ 31 | public void animateDeleteRow(Set rows, int duration); 32 | 33 | /** 34 | * @param cells 35 | * @param duration 36 | */ 37 | public void animateDeleteCells(final Set cells, int duration); 38 | 39 | 40 | /*** 41 | * ADAPTER HELPER :D 42 | */ 43 | 44 | 45 | /** 46 | * @return 47 | */ 48 | public BaseAdapter getBaseAdapter(); 49 | 50 | /** 51 | * @return 52 | */ 53 | public AnimatedAdapter getAnimatedAdapter(); 54 | 55 | 56 | /** 57 | * AWESOME HELPER METHODS ;D 58 | */ 59 | 60 | public void setAnimationListener(AnimationListener animationListener); 61 | 62 | /** 63 | * 64 | */ 65 | public void onAnimationFinish(); 66 | 67 | /** 68 | * @param position 69 | */ 70 | public void smoothScrollToCenterPosition(int position); 71 | 72 | /** 73 | * @param position 74 | * @return 75 | */ 76 | public View getViewByPosition(int position); 77 | 78 | /** 79 | * @param position 80 | * @return 81 | */ 82 | public boolean isVisible(int position); 83 | 84 | /** 85 | * @return 86 | */ 87 | public int getCenterPosition(); 88 | 89 | 90 | /** 91 | * Some Default Methods to be able to use the interface in the helper class 92 | */ 93 | public Adapter getAdapter(); 94 | 95 | public int getNumColumns(); 96 | } 97 | -------------------------------------------------------------------------------- /sample/src/main/java/com/tundem/widget/gridview/sample/SampleAdapter.java: -------------------------------------------------------------------------------- 1 | package com.tundem.widget.gridview.sample; 2 | 3 | import android.graphics.Color; 4 | import android.view.Gravity; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | import android.widget.AbsListView; 8 | import android.widget.TextView; 9 | 10 | import com.tundem.widget.gridview.adapter.AnimatedAdapter; 11 | 12 | import java.util.LinkedList; 13 | import java.util.List; 14 | import java.util.Random; 15 | 16 | /** 17 | * Created by mikepenz on 17.05.14. 18 | */ 19 | public class SampleAdapter extends AnimatedAdapter { 20 | int columnCount = 6; 21 | List cells = new LinkedList(); 22 | 23 | Random r = new Random(); 24 | 25 | public SampleAdapter() { 26 | for (int i = 0; i < 48; i++) { 27 | cells.add(r.nextInt(10)); 28 | } 29 | } 30 | 31 | @Override 32 | public void removeItem(int position) { 33 | cells.remove(position); 34 | } 35 | 36 | @Override 37 | public void addItem(Object item, boolean visible) { 38 | super.addItem(item, visible); 39 | cells.add((Integer) item); 40 | } 41 | 42 | @Override 43 | public int getCount() { 44 | return cells.size(); 45 | } 46 | 47 | @Override 48 | public Object getItem(int i) { 49 | return cells.get(i); 50 | } 51 | 52 | @Override 53 | public long getItemId(int i) { 54 | return 0; 55 | } 56 | 57 | @Override 58 | public View getView(int i, View view, ViewGroup parent) { 59 | TextView tv; 60 | if (view != null) { 61 | tv = (TextView) view; 62 | } else { 63 | tv = new TextView(parent.getContext()); 64 | tv.setTextColor(Color.BLACK); 65 | tv.setGravity(Gravity.CENTER); 66 | tv.setBackgroundResource(android.R.color.holo_blue_dark); 67 | } 68 | 69 | int item_side = parent.getWidth() / columnCount; 70 | 71 | ViewGroup.LayoutParams lp = tv.getLayoutParams(); 72 | if (lp == null) { 73 | lp = new AbsListView.LayoutParams(item_side, item_side); 74 | } else { 75 | lp.height = item_side; 76 | lp.width = item_side; 77 | } 78 | tv.setLayoutParams(lp); 79 | 80 | tv.setText(getItem(i).toString()); 81 | 82 | super.getView(i, tv, parent); 83 | 84 | return tv; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | AnimatedGridView 2 | ================ 3 | 4 | AnimatedGridView extends the default GridView and adds some new functions to animate the deletion of rows. 5 | You just have to choose the AnimatedGridView and extend the AnimatedAdapter (which extends the BaseAdapter). 6 | 7 | This library also adds a HeaderGridView, like the normal ListView. The HeaderGridView is from the Google 8 | Source Code of their gallery app. 9 | 10 | 11 | Including in your project 12 | ------------------------- 13 | ###Using Maven 14 | AnimatedGridView Library is pushed to [Maven Central], so you just need to add the following dependency to your `build.gradle`. 15 | 16 | ```javascript 17 | dependencies { 18 | compile 'com.tundem.widget.gridview:library:1.1.3-SNAPSHOT@aar' 19 | } 20 | ``` 21 | 22 | Usage 23 | ------------------------- 24 | ### via code or via xml (sample) 25 | 26 | ```xml 27 | 30 | ``` 31 | 32 | ### delete fields and animate the gridview 33 | - start the animation by calling following method 34 | ```java 35 | animateDeleteCells(mRemovedFieldsToAnimate, 200); 36 | ``` 37 | 38 | - you can also define a listener to do something after the fields were removed 39 | ```java 40 | setAnimationListener(new AnimationListener() { 41 | @Override 42 | public void onAnimationFinish() { 43 | ... 44 | } 45 | }); 46 | ``` 47 | 48 | ### add fields and animate the gridview 49 | - start the animation by calling following method 50 | ```java 51 | //Items to add: 52 | LinkedList items = new LinkedList(); 53 | items.add(1); 54 | items.add(2); 55 | items.add(3); 56 | items.add(4); 57 | items.add(5); 58 | items.add(6); 59 | //animate the new items 60 | agv.animateAddCells(items, 200); 61 | ``` 62 | 63 | Used in following projects 64 | ------ 65 | [Numbers](https://play.google.com/store/apps/details?id=com.tundem.numbersreloaded.free) (Only remove animation) 66 | 67 | 68 | Developed By 69 | ------- 70 | * Mike Penz 71 | * [mikepenz.com](http://mikepenz.com) - 72 | * [paypal.me/mikepenz](http://paypal.me/mikepenz) 73 | 74 | License 75 | ------- 76 | Copyright 2014 Mike Penz 77 | 78 | Licensed under the Apache License, Version 2.0 (the "License"); 79 | you may not use this file except in compliance with the License. 80 | You may obtain a copy of the License at 81 | 82 | http://www.apache.org/licenses/LICENSE-2.0 83 | 84 | Unless required by applicable law or agreed to in writing, software 85 | distributed under the License is distributed on an "AS IS" BASIS, 86 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 87 | See the License for the specific language governing permissions and 88 | limitations under the License. 89 | 90 | -------------------------------------------------------------------------------- /library/src/main/java/com/tundem/widget/gridview/AnimatedGridView.java: -------------------------------------------------------------------------------- 1 | package com.tundem.widget.gridview; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | import android.view.View; 6 | import android.widget.Adapter; 7 | import android.widget.BaseAdapter; 8 | import android.widget.WrapperListAdapter; 9 | 10 | import com.tundem.widget.gridview.adapter.AnimatedAdapter; 11 | import com.tundem.widget.gridview.helper.AnimationHelper; 12 | import com.tundem.widget.gridview.helper.Helper; 13 | import com.tundem.widget.gridview.listener.AnimationListener; 14 | 15 | import java.util.LinkedList; 16 | import java.util.Set; 17 | 18 | /** 19 | * Created by mikepenz on 16.05.14. 20 | */ 21 | public class AnimatedGridView extends HeaderGridView implements IAnimatedGridView { 22 | public AnimatedGridView(Context context) { 23 | super(context); 24 | this.setLayoutAnimation(AnimationHelper.getLayoutAnimation()); 25 | } 26 | 27 | public AnimatedGridView(Context context, AttributeSet attrs) { 28 | super(context, attrs); 29 | this.setLayoutAnimation(AnimationHelper.getLayoutAnimation()); 30 | } 31 | 32 | public AnimatedGridView(Context context, AttributeSet attrs, int defStyle) { 33 | super(context, attrs, defStyle); 34 | this.setLayoutAnimation(AnimationHelper.getLayoutAnimation()); 35 | } 36 | 37 | 38 | /** 39 | * ANIMATION LOGIC :D 40 | */ 41 | public void animateAddCells(LinkedList cells, int duration) { 42 | Helper.animateAddCells(this, cells, duration); 43 | } 44 | 45 | public void animateDeleteRow(Set rows, int duration) { 46 | Helper.animateDeleteRow(this, rows, duration); 47 | } 48 | 49 | public void animateDeleteCells(final Set cells, int duration) { 50 | Helper.animateDeleteCells(this, cells, duration); 51 | } 52 | 53 | public BaseAdapter getBaseAdapter() { 54 | Adapter adapter = getAdapter(); 55 | if (adapter != null) { 56 | if (adapter instanceof WrapperListAdapter) { 57 | adapter = ((WrapperListAdapter) adapter).getWrappedAdapter(); 58 | } 59 | 60 | if (adapter instanceof BaseAdapter) { 61 | return (BaseAdapter) adapter; 62 | } 63 | } 64 | return null; 65 | } 66 | 67 | public AnimatedAdapter getAnimatedAdapter() { 68 | Adapter adapter = getBaseAdapter(); 69 | if (adapter != null && adapter instanceof AnimatedAdapter) { 70 | return (AnimatedAdapter) adapter; 71 | } 72 | return null; 73 | } 74 | 75 | /** 76 | * LISTENER!! 77 | */ 78 | 79 | private AnimationListener animationListener; 80 | 81 | public void setAnimationListener(AnimationListener animationListener) { 82 | this.animationListener = animationListener; 83 | } 84 | 85 | public void onAnimationFinish() { 86 | if (animationListener != null) { 87 | animationListener.onAnimationFinish(); 88 | } 89 | } 90 | 91 | 92 | /** 93 | * A SMALL EXTRA FOR THOSE WHO LOVE OPEN SOURCE ;D 94 | */ 95 | 96 | /** 97 | * smoothScroll a specific item to the center of the list :D 98 | * 99 | * @param position 100 | */ 101 | 102 | public void smoothScrollToCenterPosition(int position) { 103 | Helper.smoothScrollToCenterPosition(this, position); 104 | } 105 | 106 | /** 107 | * HELPER METHODS!! 108 | */ 109 | 110 | public View getViewByPosition(int position) { 111 | return Helper.getViewByPosition(this, position); 112 | } 113 | 114 | /** 115 | * Helper to calculate if a specific position is visible 116 | * 117 | * @param position 118 | * @return 119 | */ 120 | 121 | public boolean isVisible(int position) { 122 | return Helper.isVisible(this, position); 123 | } 124 | 125 | 126 | public int getCenterPosition() { 127 | return Helper.getCenterPosition(this); 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /library/src/main/java/com/tundem/widget/gridview/AnimatedHeaderGridView.java: -------------------------------------------------------------------------------- 1 | package com.tundem.widget.gridview; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | import android.view.View; 6 | import android.widget.Adapter; 7 | import android.widget.BaseAdapter; 8 | import android.widget.WrapperListAdapter; 9 | 10 | import com.tundem.widget.gridview.adapter.AnimatedAdapter; 11 | import com.tundem.widget.gridview.helper.AnimationHelper; 12 | import com.tundem.widget.gridview.helper.Helper; 13 | import com.tundem.widget.gridview.listener.AnimationListener; 14 | 15 | import java.util.LinkedList; 16 | import java.util.Set; 17 | 18 | /** 19 | * Created by mikepenz on 16.05.14. 20 | */ 21 | public class AnimatedHeaderGridView extends HeaderGridView implements IAnimatedGridView { 22 | public AnimatedHeaderGridView(Context context) { 23 | super(context); 24 | this.setLayoutAnimation(AnimationHelper.getLayoutAnimation()); 25 | } 26 | 27 | public AnimatedHeaderGridView(Context context, AttributeSet attrs) { 28 | super(context, attrs); 29 | this.setLayoutAnimation(AnimationHelper.getLayoutAnimation()); 30 | } 31 | 32 | public AnimatedHeaderGridView(Context context, AttributeSet attrs, int defStyle) { 33 | super(context, attrs, defStyle); 34 | this.setLayoutAnimation(AnimationHelper.getLayoutAnimation()); 35 | } 36 | 37 | /** 38 | * ANIMATION LOGIC :D 39 | */ 40 | public void animateAddCells(LinkedList cells, int duration) { 41 | Helper.animateAddCells(this, cells, duration); 42 | } 43 | 44 | public void animateDeleteRow(Set rows, int duration) { 45 | Helper.animateDeleteRow(this, rows, duration); 46 | } 47 | 48 | public void animateDeleteCells(final Set cells, int duration) { 49 | Helper.animateDeleteCells(this, cells, duration); 50 | } 51 | 52 | public BaseAdapter getBaseAdapter() { 53 | Adapter adapter = getAdapter(); 54 | if (adapter != null) { 55 | if (adapter instanceof WrapperListAdapter) { 56 | adapter = ((WrapperListAdapter) adapter).getWrappedAdapter(); 57 | } 58 | 59 | if (adapter instanceof BaseAdapter) { 60 | return (BaseAdapter) adapter; 61 | } 62 | } 63 | return null; 64 | } 65 | 66 | public AnimatedAdapter getAnimatedAdapter() { 67 | Adapter adapter = getBaseAdapter(); 68 | if (adapter != null && adapter instanceof AnimatedAdapter) { 69 | return (AnimatedAdapter) adapter; 70 | } 71 | return null; 72 | } 73 | 74 | /** 75 | * LISTENER!! 76 | */ 77 | 78 | private AnimationListener animationListener; 79 | 80 | public void setAnimationListener(AnimationListener animationListener) { 81 | this.animationListener = animationListener; 82 | } 83 | 84 | public void onAnimationFinish() { 85 | if (animationListener != null) { 86 | animationListener.onAnimationFinish(); 87 | } 88 | } 89 | 90 | /** 91 | * A SMALL EXTRA FOR THOSE WHO LOVE OPEN SOURCE ;D 92 | */ 93 | 94 | /** 95 | * smoothScroll a specific item to the center of the list :D 96 | * 97 | * @param position 98 | */ 99 | 100 | public void smoothScrollToCenterPosition(int position) { 101 | Helper.smoothScrollToCenterPosition(this, position); 102 | } 103 | 104 | /** 105 | * HELPER METHODS!! 106 | */ 107 | 108 | public View getViewByPosition(int position) { 109 | return Helper.getViewByPosition(this, position); 110 | } 111 | 112 | /** 113 | * Helper to calculate if a specific position is visible 114 | * 115 | * @param position 116 | * @return 117 | */ 118 | 119 | public boolean isVisible(int position) { 120 | return Helper.isVisible(this, position); 121 | } 122 | 123 | 124 | public int getCenterPosition() { 125 | return Helper.getCenterPosition(this); 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /library/gradle-mvn-push.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 Chris Banes 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | apply plugin: 'maven' 18 | apply plugin: 'signing' 19 | 20 | def isReleaseBuild() { 21 | return VERSION_NAME.contains("SNAPSHOT") == false 22 | } 23 | 24 | def getReleaseRepositoryUrl() { 25 | return hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL 26 | : "https://oss.sonatype.org/service/local/staging/deploy/maven2/" 27 | } 28 | 29 | def getSnapshotRepositoryUrl() { 30 | return hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL 31 | : "https://oss.sonatype.org/content/repositories/snapshots/" 32 | } 33 | 34 | def getRepositoryUsername() { 35 | return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : "" 36 | } 37 | 38 | def getRepositoryPassword() { 39 | return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : "" 40 | } 41 | 42 | afterEvaluate { project -> 43 | uploadArchives { 44 | repositories { 45 | mavenDeployer { 46 | beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } 47 | 48 | pom.groupId = GROUP 49 | pom.artifactId = POM_ARTIFACT_ID 50 | pom.version = VERSION_NAME 51 | 52 | repository(url: getReleaseRepositoryUrl()) { 53 | authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) 54 | } 55 | snapshotRepository(url: getSnapshotRepositoryUrl()) { 56 | authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) 57 | } 58 | 59 | pom.project { 60 | name POM_NAME 61 | packaging POM_PACKAGING 62 | description POM_DESCRIPTION 63 | url POM_URL 64 | 65 | scm { 66 | url POM_SCM_URL 67 | connection POM_SCM_CONNECTION 68 | developerConnection POM_SCM_DEV_CONNECTION 69 | } 70 | 71 | licenses { 72 | license { 73 | name POM_LICENCE_NAME 74 | url POM_LICENCE_URL 75 | distribution POM_LICENCE_DIST 76 | } 77 | } 78 | 79 | developers { 80 | developer { 81 | id POM_DEVELOPER_ID 82 | name POM_DEVELOPER_NAME 83 | } 84 | } 85 | } 86 | } 87 | } 88 | } 89 | 90 | signing { 91 | required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") } 92 | sign configurations.archives 93 | } 94 | 95 | android.libraryVariants.all { variant -> 96 | def javadocTask = task("generate${variant.name.capitalize()}Javadoc", type: Javadoc) { 97 | description "Generates Javadoc for $variant.name." 98 | source = variant.javaCompile.source 99 | try { 100 | ext.androidJar = project.files(android.getBootClasspath().join(File.pathSeparator)) 101 | } catch (Exception e) { 102 | ext.androidJar = project.files(android.plugin.getRuntimeJarList().join(File.pathSeparator)) 103 | } 104 | classpath = files(variant.javaCompile.classpath.files) + files(ext.androidJar) 105 | exclude '**/BuildConfig.java' 106 | exclude '**/R.java' 107 | } 108 | 109 | javadocTask.dependsOn variant.javaCompile 110 | 111 | def jarJavadocTask = task("jar${variant.name.capitalize()}Javadoc", type: Jar) { 112 | description "Generate Javadoc Jar for $variant.name" 113 | classifier = 'javadoc' 114 | from javadocTask.destinationDir 115 | } 116 | 117 | jarJavadocTask.dependsOn javadocTask 118 | artifacts.add('archives', jarJavadocTask) 119 | 120 | def jarSourceTask = task("jar${variant.name.capitalize()}Sources", type: Jar) { 121 | description "Generates Java Sources for $variant.name." 122 | classifier = 'sources' 123 | from variant.javaCompile.source 124 | } 125 | 126 | jarSourceTask.dependsOn variant.javaCompile 127 | artifacts.add('archives', jarSourceTask) 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /library/src/main/java/com/tundem/widget/gridview/helper/Helper.java: -------------------------------------------------------------------------------- 1 | package com.tundem.widget.gridview.helper; 2 | 3 | import android.os.Handler; 4 | import android.support.v4.view.ViewCompat; 5 | import android.view.View; 6 | import android.widget.BaseAdapter; 7 | import android.widget.GridView; 8 | import android.widget.HeaderViewListAdapter; 9 | 10 | import com.tundem.widget.gridview.HeaderGridView; 11 | import com.tundem.widget.gridview.IAnimatedGridView; 12 | import com.tundem.widget.gridview.adapter.AnimatedAdapter; 13 | import com.tundem.widget.gridview.animation.ScaleDownAnimation; 14 | 15 | import java.util.LinkedList; 16 | import java.util.List; 17 | import java.util.Set; 18 | import java.util.TreeSet; 19 | 20 | /** 21 | * Created by mikepenz on 17.05.14. 22 | */ 23 | 24 | 25 | public class Helper { 26 | 27 | public static void animateAddCells(IAnimatedGridView gridView, LinkedList cells, int duration) { 28 | AnimatedAdapter aa = gridView.getAnimatedAdapter(); 29 | if (aa != null) { 30 | aa.duration = duration; 31 | 32 | 33 | boolean isVisible = gridView.isVisible(aa.getCount() - 1); 34 | for (Object o : cells) { 35 | aa.addItem(o, isVisible); 36 | } 37 | 38 | aa.notifyDataSetChanged(); 39 | } 40 | } 41 | 42 | public static void animateDeleteRow(IAnimatedGridView gridView, Set rows, int duration) { 43 | Set cells = new TreeSet(); 44 | for (int row : rows) { 45 | for (int i = 0; i < gridView.getNumColumns(); i++) { 46 | cells.add(row * gridView.getNumColumns() + i); 47 | } 48 | } 49 | animateDeleteCells(gridView, cells, duration); 50 | } 51 | 52 | public static void animateDeleteCells(final IAnimatedGridView gridView, final Set cells, int duration) { 53 | final List views = new LinkedList(); 54 | 55 | int fieldOffset = 0; 56 | if (gridView.getAdapter() instanceof HeaderGridView.HeaderViewGridAdapter || gridView.getAdapter() instanceof HeaderViewListAdapter) { 57 | fieldOffset = gridView.getNumColumns(); 58 | } 59 | for (int removedFieldToAnimate : cells) { 60 | final View v = gridView.getViewByPosition(removedFieldToAnimate + fieldOffset); 61 | if (v != null) { 62 | views.add(v); 63 | } 64 | } 65 | 66 | int tempHeight = -1; 67 | for (int i = 0; i < views.size(); i++) { 68 | View v = views.get(i); 69 | tempHeight = v.getHeight(); 70 | 71 | ScaleDownAnimation sa = new ScaleDownAnimation(v); 72 | sa.setDuration(duration); 73 | 74 | ViewCompat.setHasTransientState(v, true); 75 | v.startAnimation(sa); 76 | } 77 | 78 | /* 79 | BaseAdapter adapter = gridView.getBaseAdapter(); 80 | if (adapter != null) { 81 | adapter.notifyDataSetChanged(); 82 | } 83 | */ 84 | 85 | 86 | final int height = tempHeight; 87 | new Handler().postDelayed(new Runnable() { 88 | public void run() { 89 | if (height != -1) { 90 | for (View v : views) { 91 | v.getLayoutParams().height = height; 92 | } 93 | } 94 | 95 | for (View v : views) { 96 | ViewCompat.setHasTransientState(v, false); 97 | v.clearAnimation(); 98 | v.invalidate(); 99 | } 100 | 101 | BaseAdapter adapter = gridView.getBaseAdapter(); 102 | if (adapter != null && adapter instanceof AnimatedAdapter) { 103 | AnimatedAdapter animatedAdapter = ((AnimatedAdapter) adapter); 104 | 105 | int removedFields = 0; 106 | for (int cell : cells) { 107 | animatedAdapter.removeItem(cell - removedFields); 108 | removedFields = removedFields + 1; 109 | } 110 | } 111 | 112 | adapter.notifyDataSetChanged(); 113 | 114 | gridView.onAnimationFinish(); 115 | } 116 | }, duration + 50); 117 | } 118 | 119 | 120 | /** 121 | * A SMALL EXTRA FOR THOSE WHO LOVE OPEN SOURCE ;D 122 | */ 123 | 124 | /** 125 | * smoothScroll a specific item to the center of the list :D 126 | * 127 | * @param position 128 | */ 129 | 130 | public static void smoothScrollToCenterPosition(GridView gridView, int position) { 131 | int pos = position; 132 | if (position <= gridView.getFirstVisiblePosition()) { 133 | pos = position - (gridView.getChildCount() / 2); 134 | if (pos < 0) { 135 | pos = 0; 136 | } 137 | gridView.smoothScrollToPosition(pos); 138 | } else { 139 | pos = position + (gridView.getChildCount() / 2); 140 | if (pos >= gridView.getCount()) { 141 | pos = gridView.getCount() - 1; 142 | if (pos < 0) { 143 | pos = 0; 144 | } 145 | } 146 | } 147 | gridView.smoothScrollToPosition(pos); 148 | } 149 | 150 | /** 151 | * HELPER METHODS!! 152 | */ 153 | 154 | public static View getViewByPosition(GridView gridView, int position) { 155 | int firstPosition = gridView.getFirstVisiblePosition(); 156 | int lastPosition = gridView.getLastVisiblePosition(); 157 | 158 | if ((position < firstPosition) || (position > lastPosition)) { 159 | return null; 160 | } 161 | 162 | return gridView.getChildAt(position - firstPosition); 163 | } 164 | 165 | /** 166 | * Helper to calculate if a specific position is visible 167 | * 168 | * @param position 169 | * @return 170 | */ 171 | 172 | public static boolean isVisible(GridView gridView, int position) { 173 | int wantedPosition = position; // Whatever position you're looking for 174 | int firstPosition = gridView.getFirstVisiblePosition(); // - getHeaderViewsCount(); // This is the same as child #0 175 | int wantedChild = wantedPosition - firstPosition; 176 | // Say, first visible position is 8, you want position 10, wantedChild will now be 2 177 | // So that means your view is child #2 in the ViewGroup: 178 | if (wantedChild < 0 || wantedChild >= gridView.getChildCount()) { 179 | return false; 180 | } 181 | 182 | return true; 183 | } 184 | 185 | 186 | public static int getCenterPosition(GridView gridView) { 187 | return gridView.getFirstVisiblePosition() + (gridView.getChildCount() / 2); // - getHeaderViewsCount(); // This is the same as child #0 188 | } 189 | } 190 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /library/src/main/java/com/tundem/widget/gridview/HeaderGridView.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.tundem.widget.gridview; 18 | 19 | import android.content.Context; 20 | import android.database.DataSetObservable; 21 | import android.database.DataSetObserver; 22 | import android.util.AttributeSet; 23 | import android.view.View; 24 | import android.view.ViewGroup; 25 | import android.widget.AdapterView; 26 | import android.widget.Filter; 27 | import android.widget.Filterable; 28 | import android.widget.FrameLayout; 29 | import android.widget.GridView; 30 | import android.widget.ListAdapter; 31 | import android.widget.WrapperListAdapter; 32 | 33 | import java.util.ArrayList; 34 | 35 | /** 36 | * A {@link GridView} that supports adding header rows in a 37 | * very similar way to {@link ListView}. 38 | * See {@link HeaderGridView#addHeaderView(View, Object, boolean)} 39 | */ 40 | public class HeaderGridView extends GridView { 41 | private static final String TAG = "HeaderGridView"; 42 | 43 | /** 44 | * A class that represents a fixed view in a list, for example a header at the top 45 | * or a footer at the bottom. 46 | */ 47 | private static class FixedViewInfo { 48 | /** The view to add to the grid */ 49 | public View view; 50 | public ViewGroup viewContainer; 51 | /** The data backing the view. This is returned from {@link ListAdapter#getItem(int)}. */ 52 | public Object data; 53 | /** true if the fixed view should be selectable in the grid */ 54 | public boolean isSelectable; 55 | } 56 | 57 | private ArrayList mHeaderViewInfos = new ArrayList(); 58 | 59 | private void initHeaderGridView() { 60 | super.setClipChildren(false); 61 | } 62 | 63 | public HeaderGridView(Context context) { 64 | super(context); 65 | initHeaderGridView(); 66 | } 67 | 68 | public HeaderGridView(Context context, AttributeSet attrs) { 69 | super(context, attrs); 70 | initHeaderGridView(); 71 | } 72 | 73 | public HeaderGridView(Context context, AttributeSet attrs, int defStyle) { 74 | super(context, attrs, defStyle); 75 | initHeaderGridView(); 76 | } 77 | 78 | @Override 79 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 80 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 81 | ListAdapter adapter = getAdapter(); 82 | if (adapter != null && adapter instanceof HeaderViewGridAdapter) { 83 | ((HeaderViewGridAdapter) adapter).setNumColumns(getNumColumns()); 84 | } 85 | } 86 | 87 | @Override 88 | public void setClipChildren(boolean clipChildren) { 89 | // Ignore, since the header rows depend on not being clipped 90 | } 91 | 92 | /** 93 | * Add a fixed view to appear at the top of the grid. If addHeaderView is 94 | * called more than once, the views will appear in the order they were 95 | * added. Views added using this call can take focus if they want. 96 | *

97 | * NOTE: Call this before calling setAdapter. This is so HeaderGridView can wrap 98 | * the supplied cursor with one that will also account for header views. 99 | * 100 | * @param v The view to add. 101 | * @param data Data to associate with this view 102 | * @param isSelectable whether the item is selectable 103 | */ 104 | public void addHeaderView(View v, Object data, boolean isSelectable) { 105 | ListAdapter adapter = getAdapter(); 106 | 107 | if (adapter != null && ! (adapter instanceof HeaderViewGridAdapter)) { 108 | throw new IllegalStateException( 109 | "Cannot add header view to grid -- setAdapter has already been called."); 110 | } 111 | 112 | FixedViewInfo info = new FixedViewInfo(); 113 | FrameLayout fl = new FullWidthFixedViewLayout(getContext()); 114 | fl.addView(v); 115 | info.view = v; 116 | info.viewContainer = fl; 117 | info.data = data; 118 | info.isSelectable = isSelectable; 119 | mHeaderViewInfos.add(info); 120 | 121 | // in the case of re-adding a header view, or adding one later on, 122 | // we need to notify the observer 123 | if (adapter != null) { 124 | ((HeaderViewGridAdapter) adapter).notifyDataSetChanged(); 125 | } 126 | } 127 | 128 | /** 129 | * Add a fixed view to appear at the top of the grid. If addHeaderView is 130 | * called more than once, the views will appear in the order they were 131 | * added. Views added using this call can take focus if they want. 132 | *

133 | * NOTE: Call this before calling setAdapter. This is so HeaderGridView can wrap 134 | * the supplied cursor with one that will also account for header views. 135 | * 136 | * @param v The view to add. 137 | */ 138 | public void addHeaderView(View v) { 139 | addHeaderView(v, null, true); 140 | } 141 | 142 | public int getHeaderViewCount() { 143 | return mHeaderViewInfos.size(); 144 | } 145 | 146 | /** 147 | * Removes a previously-added header view. 148 | * 149 | * @param v The view to remove 150 | * @return true if the view was removed, false if the view was not a header 151 | * view 152 | */ 153 | public boolean removeHeaderView(View v) { 154 | if (mHeaderViewInfos.size() > 0) { 155 | boolean result = false; 156 | ListAdapter adapter = getAdapter(); 157 | if (adapter != null && ((HeaderViewGridAdapter) adapter).removeHeader(v)) { 158 | result = true; 159 | } 160 | removeFixedViewInfo(v, mHeaderViewInfos); 161 | return result; 162 | } 163 | return false; 164 | } 165 | 166 | private void removeFixedViewInfo(View v, ArrayList where) { 167 | int len = where.size(); 168 | for (int i = 0; i < len; ++i) { 169 | FixedViewInfo info = where.get(i); 170 | if (info.view == v) { 171 | where.remove(i); 172 | break; 173 | } 174 | } 175 | } 176 | 177 | @Override 178 | public void setAdapter(ListAdapter adapter) { 179 | if (mHeaderViewInfos.size() > 0) { 180 | HeaderViewGridAdapter hadapter = new HeaderViewGridAdapter(mHeaderViewInfos, adapter); 181 | int numColumns = getNumColumns(); 182 | if (numColumns > 1) { 183 | hadapter.setNumColumns(numColumns); 184 | } 185 | super.setAdapter(hadapter); 186 | } else { 187 | super.setAdapter(adapter); 188 | } 189 | } 190 | 191 | private class FullWidthFixedViewLayout extends FrameLayout { 192 | public FullWidthFixedViewLayout(Context context) { 193 | super(context); 194 | } 195 | 196 | @Override 197 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 198 | int targetWidth = HeaderGridView.this.getMeasuredWidth() 199 | - HeaderGridView.this.getPaddingLeft() 200 | - HeaderGridView.this.getPaddingRight(); 201 | widthMeasureSpec = MeasureSpec.makeMeasureSpec(targetWidth, 202 | MeasureSpec.getMode(widthMeasureSpec)); 203 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 204 | } 205 | } 206 | 207 | /** 208 | * ListAdapter used when a HeaderGridView has header views. This ListAdapter 209 | * wraps another one and also keeps track of the header views and their 210 | * associated data objects. 211 | *

This is intended as a base class; you will probably not need to 212 | * use this class directly in your own code. 213 | */ 214 | public static class HeaderViewGridAdapter implements WrapperListAdapter, Filterable { 215 | 216 | // This is used to notify the container of updates relating to number of columns 217 | // or headers changing, which changes the number of placeholders needed 218 | private final DataSetObservable mDataSetObservable = new DataSetObservable(); 219 | 220 | private final ListAdapter mAdapter; 221 | private int mNumColumns = 1; 222 | 223 | // This ArrayList is assumed to NOT be null. 224 | ArrayList mHeaderViewInfos; 225 | 226 | boolean mAreAllFixedViewsSelectable; 227 | 228 | private final boolean mIsFilterable; 229 | 230 | public HeaderViewGridAdapter(ArrayList headerViewInfos, ListAdapter adapter) { 231 | mAdapter = adapter; 232 | mIsFilterable = adapter instanceof Filterable; 233 | 234 | if (headerViewInfos == null) { 235 | throw new IllegalArgumentException("headerViewInfos cannot be null"); 236 | } 237 | mHeaderViewInfos = headerViewInfos; 238 | 239 | mAreAllFixedViewsSelectable = areAllListInfosSelectable(mHeaderViewInfos); 240 | } 241 | 242 | public int getHeadersCount() { 243 | return mHeaderViewInfos.size(); 244 | } 245 | 246 | @Override 247 | public boolean isEmpty() { 248 | return (mAdapter == null || mAdapter.isEmpty()) && getHeadersCount() == 0; 249 | } 250 | 251 | public void setNumColumns(int numColumns) { 252 | if (numColumns < 1) { 253 | throw new IllegalArgumentException("Number of columns must be 1 or more"); 254 | } 255 | if (mNumColumns != numColumns) { 256 | mNumColumns = numColumns; 257 | notifyDataSetChanged(); 258 | } 259 | } 260 | 261 | private boolean areAllListInfosSelectable(ArrayList infos) { 262 | if (infos != null) { 263 | for (FixedViewInfo info : infos) { 264 | if (!info.isSelectable) { 265 | return false; 266 | } 267 | } 268 | } 269 | return true; 270 | } 271 | 272 | public boolean removeHeader(View v) { 273 | for (int i = 0; i < mHeaderViewInfos.size(); i++) { 274 | FixedViewInfo info = mHeaderViewInfos.get(i); 275 | if (info.view == v) { 276 | mHeaderViewInfos.remove(i); 277 | 278 | mAreAllFixedViewsSelectable = areAllListInfosSelectable(mHeaderViewInfos); 279 | 280 | mDataSetObservable.notifyChanged(); 281 | return true; 282 | } 283 | } 284 | 285 | return false; 286 | } 287 | 288 | @Override 289 | public int getCount() { 290 | if (mAdapter != null) { 291 | return getHeadersCount() * mNumColumns + mAdapter.getCount(); 292 | } else { 293 | return getHeadersCount() * mNumColumns; 294 | } 295 | } 296 | 297 | @Override 298 | public boolean areAllItemsEnabled() { 299 | if (mAdapter != null) { 300 | return mAreAllFixedViewsSelectable && mAdapter.areAllItemsEnabled(); 301 | } else { 302 | return true; 303 | } 304 | } 305 | 306 | @Override 307 | public boolean isEnabled(int position) { 308 | // Header (negative positions will throw an ArrayIndexOutOfBoundsException) 309 | int numHeadersAndPlaceholders = getHeadersCount() * mNumColumns; 310 | if (position < numHeadersAndPlaceholders) { 311 | return (position % mNumColumns == 0) 312 | && mHeaderViewInfos.get(position / mNumColumns).isSelectable; 313 | } 314 | 315 | // Adapter 316 | final int adjPosition = position - numHeadersAndPlaceholders; 317 | int adapterCount = 0; 318 | if (mAdapter != null) { 319 | adapterCount = mAdapter.getCount(); 320 | if (adjPosition < adapterCount) { 321 | return mAdapter.isEnabled(adjPosition); 322 | } 323 | } 324 | 325 | throw new ArrayIndexOutOfBoundsException(position); 326 | } 327 | 328 | @Override 329 | public Object getItem(int position) { 330 | // Header (negative positions will throw an ArrayIndexOutOfBoundsException) 331 | int numHeadersAndPlaceholders = getHeadersCount() * mNumColumns; 332 | if (position < numHeadersAndPlaceholders) { 333 | if (position % mNumColumns == 0) { 334 | return mHeaderViewInfos.get(position / mNumColumns).data; 335 | } 336 | return null; 337 | } 338 | 339 | // Adapter 340 | final int adjPosition = position - numHeadersAndPlaceholders; 341 | int adapterCount = 0; 342 | if (mAdapter != null) { 343 | adapterCount = mAdapter.getCount(); 344 | if (adjPosition < adapterCount) { 345 | return mAdapter.getItem(adjPosition); 346 | } 347 | } 348 | 349 | throw new ArrayIndexOutOfBoundsException(position); 350 | } 351 | 352 | @Override 353 | public long getItemId(int position) { 354 | int numHeadersAndPlaceholders = getHeadersCount() * mNumColumns; 355 | if (mAdapter != null && position >= numHeadersAndPlaceholders) { 356 | int adjPosition = position - numHeadersAndPlaceholders; 357 | int adapterCount = mAdapter.getCount(); 358 | if (adjPosition < adapterCount) { 359 | return mAdapter.getItemId(adjPosition); 360 | } 361 | } 362 | return -1; 363 | } 364 | 365 | @Override 366 | public boolean hasStableIds() { 367 | if (mAdapter != null) { 368 | return mAdapter.hasStableIds(); 369 | } 370 | return false; 371 | } 372 | 373 | @Override 374 | public View getView(int position, View convertView, ViewGroup parent) { 375 | // Header (negative positions will throw an ArrayIndexOutOfBoundsException) 376 | int numHeadersAndPlaceholders = getHeadersCount() * mNumColumns ; 377 | if (position < numHeadersAndPlaceholders) { 378 | View headerViewContainer = mHeaderViewInfos 379 | .get(position / mNumColumns).viewContainer; 380 | if (position % mNumColumns == 0) { 381 | return headerViewContainer; 382 | } else { 383 | if (convertView == null) { 384 | convertView = new View(parent.getContext()); 385 | } 386 | // We need to do this because GridView uses the height of the last item 387 | // in a row to determine the height for the entire row. 388 | convertView.setVisibility(View.INVISIBLE); 389 | convertView.setMinimumHeight(headerViewContainer.getHeight()); 390 | return convertView; 391 | } 392 | } 393 | 394 | // Adapter 395 | final int adjPosition = position - numHeadersAndPlaceholders; 396 | int adapterCount = 0; 397 | if (mAdapter != null) { 398 | adapterCount = mAdapter.getCount(); 399 | if (adjPosition < adapterCount) { 400 | return mAdapter.getView(adjPosition, convertView, parent); 401 | } 402 | } 403 | 404 | throw new ArrayIndexOutOfBoundsException(position); 405 | } 406 | 407 | @Override 408 | public int getItemViewType(int position) { 409 | int numHeadersAndPlaceholders = getHeadersCount() * mNumColumns; 410 | if (position < numHeadersAndPlaceholders && (position % mNumColumns != 0)) { 411 | // Placeholders get the last view type number 412 | return mAdapter != null ? mAdapter.getViewTypeCount() : 1; 413 | } 414 | if (mAdapter != null && position >= numHeadersAndPlaceholders) { 415 | int adjPosition = position - numHeadersAndPlaceholders; 416 | int adapterCount = mAdapter.getCount(); 417 | if (adjPosition < adapterCount) { 418 | return mAdapter.getItemViewType(adjPosition); 419 | } 420 | } 421 | 422 | return AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER; 423 | } 424 | 425 | @Override 426 | public int getViewTypeCount() { 427 | if (mAdapter != null) { 428 | return mAdapter.getViewTypeCount() + 1; 429 | } 430 | return 2; 431 | } 432 | 433 | @Override 434 | public void registerDataSetObserver(DataSetObserver observer) { 435 | mDataSetObservable.registerObserver(observer); 436 | if (mAdapter != null) { 437 | mAdapter.registerDataSetObserver(observer); 438 | } 439 | } 440 | 441 | @Override 442 | public void unregisterDataSetObserver(DataSetObserver observer) { 443 | mDataSetObservable.unregisterObserver(observer); 444 | if (mAdapter != null) { 445 | mAdapter.unregisterDataSetObserver(observer); 446 | } 447 | } 448 | 449 | @Override 450 | public Filter getFilter() { 451 | if (mIsFilterable) { 452 | return ((Filterable) mAdapter).getFilter(); 453 | } 454 | return null; 455 | } 456 | 457 | @Override 458 | public ListAdapter getWrappedAdapter() { 459 | return mAdapter; 460 | } 461 | 462 | public void notifyDataSetChanged() { 463 | mDataSetObservable.notifyChanged(); 464 | } 465 | } 466 | } 467 | --------------------------------------------------------------------------------