Skip to content

Commit

Permalink
Merge pull request tjerkw#26 from naddaf/master
Browse files Browse the repository at this point in the history
Scrolling hidden expanded item into the visible view
  • Loading branch information
tjerkw committed Jan 10, 2014
2 parents a99e817 + f14202e commit 8399fbc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
2 changes: 1 addition & 1 deletion library/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
# project structure.

# Project target.
target=android-4
target=android-8
android.library=true
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.tjerkw.slideexpandable.library;

import java.util.BitSet;

import android.annotation.SuppressLint;
import android.graphics.Rect;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.SparseIntArray;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.LinearLayout;
import android.widget.ListAdapter;

import java.util.BitSet;
import android.widget.ListView;

/**
* Wraps a ListAdapter to give it expandable list view functionality.
Expand Down Expand Up @@ -53,13 +57,25 @@ public abstract class AbstractSlideExpandableListAdapter extends WrapperListAdap
* The height is calculated just before the view is drawn.
*/
private final SparseIntArray viewHeights = new SparseIntArray(10);

/**
* Will point to the ListView
*/
private ViewGroup parent;

private boolean froyoOrAbove;

public AbstractSlideExpandableListAdapter(ListAdapter wrapped) {
super(wrapped);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.FROYO){
froyoOrAbove = true;
}
}

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
this.parent = viewGroup;
view = wrapped.getView(position, view, viewGroup);
enableFor(view, position);
return view;
Expand Down Expand Up @@ -241,6 +257,41 @@ private void animateView(final View target, final int type) {
type
);
anim.setDuration(getAnimationDuration());
if (froyoOrAbove) {
anim.setAnimationListener(new AnimationListener() {

@Override
public void onAnimationStart(Animation animation) {}

@Override
public void onAnimationRepeat(Animation animation) {}

@SuppressLint("NewApi")
@Override
public void onAnimationEnd(Animation animation) {
if (type == ExpandCollapseAnimation.EXPAND) {
if (parent instanceof ListView) {
ListView listView = (ListView) parent;
int movement = target.getBottom();

Rect r = new Rect();
boolean visible = target.getGlobalVisibleRect(r);
Rect r2 = new Rect();
listView.getGlobalVisibleRect(r2);

if (!visible) {
listView.smoothScrollBy(movement, getAnimationDuration());
} else {
if (r2.bottom == r.bottom) {
listView.smoothScrollBy(movement, getAnimationDuration());
}
}
}
}

}
});
}
target.startAnimation(anim);
}

Expand Down

0 comments on commit 8399fbc

Please sign in to comment.