我在我的应用中成功实现了一个向下滑动的面板。从我在stackoverflow上找到的答案动画。
但是我正在努力实现一些功能。我不知道该怎么做。我已经坐了几个小时试图找出怎么做但我仍然无法解决我的问题。
我的布局包含2个按钮和一个空的framelayout。单击按钮,片段事务将片段加载到空的framelayout中,布局向下滑动。所有这些部件都运行良好。
我现在遇到的问题是: 1.如何检查滑动面板是否已处于活动状态。 2.如何停止面板向上滑动单击同一按钮(如果它已经处于活动状态)。 3.还将片段事务放在按钮的onClick方法内,即使片段处于活动状态,也会重新加载片段。如何阻止这一点。
请问我还是比较新的应用程序开发。我试图解决这个问题但到目前为止还没有取得任何成功。所以任何帮助都会有用。
另外我想知道是否有更好的方法来实现这个滑动面板? 谢谢。
这是我的代码:
包含动画的ExpandCollapseAnimation类
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;
}
}