问题描述:

I successfully implemented a sliding down panel in my app. with animations from an answer i found on stackoverflow.

But there are some features that i am trying to implement. And i don't know how to. I have sat down for hours trying to figure out how to do them but i still haven't able to solve my problems.

I have a layout that contains 2 buttons and an empty framelayout. OnClick of the buttons, a fragment transaction loads a fragment into the empty framelayout and the layout slides down. All these parts work fine.

The problems i am having now is:

1. How to check if the sliding panel is already active.

2. How to stop the panel from sliding up onClick of the same button if it is already active.

3. Also putting the fragment transaction inside the onClick method of the button, reloads the fragment even when it is active. how to i stop this.

Please i am still relatively new to app development. I have tried to solve this problem but havent had any success so far. So any help would be useful.

Also i would like to know if there is a better way of implementing this sliding down panel?

Thank you.

Here is my code:

  1. ExpandCollapseAnimation class that holds the animation

    public class ExpandCollapseAnimation extends Animation {

    private FrameLayout mAnimatedView;

    private int mEndHeight;

    private int mType;

    /**

    * Initializes expand collapse animation, has two types, collapse (1) and expand (0).

    * @param view The view to animate

    * @param duration

    * @param type The type of animation: 0 will expand from gone and 0 size to visible and layout size defined in xml.

    * 1 will collapse view and set to gone

    */

    public ExpandCollapseAnimation(FrameLayout view, int duration, int type) {

    setDuration(duration);

    mAnimatedView = view;

    mEndHeight = mAnimatedView.getLayoutParams().height;

    mType = type;

    if(mType == 0) {

    mAnimatedView.getLayoutParams().height = 0;

    mAnimatedView.setVisibility(View.VISIBLE);

    }

    }

    @Override

    protected void applyTransformation(float interpolatedTime, Transformation t) {

    super.applyTransformation(interpolatedTime, t);

    if (interpolatedTime < 1.0f) {

    if(mType == 0) {

    mAnimatedView.getLayoutParams().height = (int) (mEndHeight * interpolatedTime);

    } else {

    mAnimatedView.getLayoutParams().height = mEndHeight - (int) (mEndHeight * interpolatedTime);

    }

    mAnimatedView.requestLayout();

    } else {

    if(mType == 0) {

    mAnimatedView.getLayoutParams().height = mEndHeight;

    mAnimatedView.requestLayout();

    } else {

    mAnimatedView.getLayoutParams().height = 0;

    mAnimatedView.setVisibility(View.GONE);

    mAnimatedView.requestLayout();

    mAnimatedView.getLayoutParams().height = mEndHeight;

    }

    }

    }

    }

MainActivity:

 public class MainActivity extends ActionBarActivity {

private boolean mActive = false;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btntap();

btnTwoTap();

}

public void btnTwoTap() {

Button button2 = (Button) findViewById(R.id.openAnimTwo);

FrameLayout frameLayout= (FrameLayout) findViewById(R.id.slidin);

button2.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

FragmentManager fm = getSupportFragmentManager();

FragmentTransaction ft = fm.beginTransaction();

Fragment fragmen = new TestFragTwo();

ft.replace(R.id.slidin, fragmen);

ft.commit();

}

});

}

private void btntap() {

final FrameLayout animatedFrameLayout = (FrameLayout) findViewById(R.id.slidin);

final Button button = (Button) findViewById(R.id.openAnim);

button.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

ExpandCollapseAnimation animation = null;

if(mActive) {

animation = new ExpandCollapseAnimation(animatedFrameLayout, 1500, 1);

mActive = false;

}

else if (mActive = false){

animation = new ExpandCollapseAnimation(animatedFrameLayout, 0, 1);

}

else {

animation = new ExpandCollapseAnimation(animatedFrameLayout, 1000, 0);

mActive = true;

}

animatedFrameLayout.startAnimation(animation);

//button.setVisibility(View.GONE);

}

});

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

相关阅读:
Top