Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow dynamic views add/remove #10

Merged
merged 3 commits into from
Nov 7, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

/**
Expand Down Expand Up @@ -78,8 +79,9 @@ private void init(Context context, AttributeSet attrs)
} finally {
attrArray.recycle();
}

}

loadAnimations();
}

@Override
Expand All @@ -90,21 +92,66 @@ protected void onFinishInflate ()
if (getChildCount() > 2) {
throw new IllegalStateException("EasyFlipView can host only two direct children!");
}


findViews();
changeCameraDistance();
}

@Override
public void addView(View v, int pos, ViewGroup.LayoutParams params) {
if (getChildCount() == 2) {
throw new IllegalStateException("EasyFlipView can host only two direct children!");
}

super.addView(v, pos, params);

findViews();
loadAnimations();
changeCameraDistance();
}

private void findViews() {
mCardFrontLayout = getChildAt(1);
mCardBackLayout = getChildAt(0);
@Override
public void removeView(View v) {
super.removeView(v);

findViews();
}

@Override
public void removeAllViewsInLayout() {
super.removeAllViewsInLayout();

// Reset the state
mFlipState = FlipState.FRONT_SIDE;
if (!isFlipOnTouch())
{

findViews();
}

private void findViews() {
// Invalidation since we use this also on removeView
mCardBackLayout = null;
mCardFrontLayout = null;

int childs = getChildCount();
if (childs < 1) {
return;
}

if (childs < 2) {
// Only invalidate flip state if we have a single child
mFlipState = FlipState.FRONT_SIDE;

mCardFrontLayout = getChildAt(0);
} else if (childs == 2) {
mCardFrontLayout = getChildAt(1);
mCardBackLayout = getChildAt(0);
}

if (!isFlipOnTouch()) {
mCardFrontLayout.setVisibility(VISIBLE);
mCardBackLayout.setVisibility(GONE);

if (mCardBackLayout != null) {
mCardBackLayout.setVisibility(GONE);
}
}
}

Expand Down Expand Up @@ -148,21 +195,25 @@ public void onAnimationRepeat(Animator animator) {
private void changeCameraDistance() {
int distance = 8000;
float scale = getResources().getDisplayMetrics().density * distance;
mCardFrontLayout.setCameraDistance(scale);
mCardBackLayout.setCameraDistance(scale);

if (mCardFrontLayout != null) {
mCardFrontLayout.setCameraDistance(scale);
}
if (mCardBackLayout != null) {
mCardBackLayout.setCameraDistance(scale);
}
}

/**
* Play the animation of flipping and flip the view for one side!
*/
public void flipTheView() {

if (!flipEnabled)
if (!flipEnabled || getChildCount() < 2)
return;

if (mSetRightOut.isRunning() || mSetLeftIn.isRunning())
return;

mCardBackLayout.setVisibility(VISIBLE);
mCardFrontLayout.setVisibility(VISIBLE);

Expand Down Expand Up @@ -191,6 +242,9 @@ public void flipTheView() {
*/
public void flipTheView(boolean withAnimation)
{
if (getChildCount() < 2)
return;

if (!withAnimation)
{
mSetLeftIn.setDuration(0);
Expand Down