我有一个类,其中我在单击按钮时包含另一个布局。这个包含的布局有一些按钮和点击这些按钮时执行的代码。我使用了一个计数器来指示单击按钮的次数。第一次单击按钮包括布局,第二次单击删除 View 等。这是代码
public class Home extends Fragment implements OnClickListener {
int c = 0;
Button bmain, bnew, bolder;
RelativeLayout r1;
View rootView;
Animation slidedown, slideup;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.home, container, false);
bmain = (Button) rootView.findViewById(R.id.btn2);
bmain.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View arg0) {
ViewGroup con = null;
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
FrameLayout flContainer = (FrameLayout)rootView.findViewById(R.id.flContainer);
//Loading animation
slidedown = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_down);
slideup = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_up);
//The counter indicates the number of clicks.
//Needs to be replaced for a better solution.
//If it's even add view
if(c%2==0)
{
//Adding layout here
flContainer.addView(layoutInflater.inflate(R.layout.test1,con,false ));
//Starting Animation
flContainer.startAnimation(slidedown);
//After adding layout we can find the Id of the included layout and proceed from there
bnew = (Button) rootView.findViewById(R.id.btntest);
bnew.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
Toast.makeText(getActivity(), "You Clicked New", Toast.LENGTH_LONG).show();
}
});
bolder = (Button) rootView.findViewById(R.id.btntest1);
bolder.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
Intent form = new Intent(getActivity(),FeedbackForm.class);
startActivity(form);
}
});
c++;
} //If ends here
//If it's odd remove view
else
{
flContainer.removeAllViews();
flContainer.startAnimation(slideup);
//flContainer.removeView(flContainer);
//flContainer.removeView(layoutInflater.inflate(R.layout.test1, con, false));
c++;
}
}
}
最后的代码
flContainer.removeAllViews();
flContainer.startAnimation(slideup);
移除 View 但无法处理动画。我曾尝试使用 removeView,但在那种情况下,if 语句中的 buttonclicks 无法第二次执行。我在这里错过了什么?我怎样才能实现它?
最佳答案
答案很简单。您必须在动画完成后删除 View 。这可以非常简单地实现,首先您必须为您的动画设置一个动画监听器,然后在 onAnimationEnd
回调(动画完成时调用)中删除 View 。
编辑:
替换这个:
flContainer.removeAllViews();
flContainer.startAnimation(slideup);
有了这个:
slideup.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
flContainer.removeAllViews();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
flContainer.startAnimation(slideup);
如果还有问题请告诉我。
关于android - removeAllViews Android 上的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22615720/