0

我有一個類,其中包含按鈕單擊另一個佈局。這包括佈局中有一些按鈕和點擊這些按鈕時執行的代碼。我已經使用了一個指示按鈕被點擊的次數的計數器。第一次點擊按鈕包含佈局,第二次點擊刪除視圖等。下面的代碼removeAllViews上的動畫Android

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); 

消除了看法,但無法處理動畫。我曾嘗試使用removeView,但在這種情況下,if語句中的buttonclicks無法執行第二次。我在這裏錯過了什麼?我怎樣才能實現它?

回答

1

答案很簡單。動畫完成後,您必須刪除該視圖。這可以非常簡單地實現,首先必須爲您的動畫設置動畫偵聽器,並在回調中動畫 - 當動畫完成時調用 - 您將刪除視圖。

編輯:

替換此:

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); 

如果有任何進一步的問題,讓我知道。

+0

我想刪除按鈕點擊而不是動畫結束的意見。我嘗試使用代碼:flContainer.removeView(layoutInflater.inflate(R.layout.test1,con,false));它使用動畫刪除視圖,但再次單擊該按鈕以播放動畫時,包含的佈局中的按鈕不響應點擊。 – user1377504

+0

我不知道你在說什麼,但是這段代碼:'flContainer.removeView(layoutInflater.inflate(R.layout.test1,con,false));'簡直是錯的。您正在爲新視圖充氣,然後立即嘗試將其從容器中取出。請編輯你的問題,準確解釋你想要做什麼。 –

+0

在我的home.xml文件中有一個按鈕。點擊此按鈕後,將包含新的佈局。這包括佈局依次有一些按鈕,其中的點擊操作是在我包含佈局後定義的。該代碼所建議的佈局包含在動畫中(向上滑動)。再次點擊我的home.xml文件中的按鈕,佈局將被刪除。代碼'flContainer.removeAllViews()'刪除視圖,但它不會按照我希望的方式滑動。我怎樣才能實現它? – user1377504