2013-10-11 47 views
0

我有我的應用程序中的自定義列表視圖。每個列表項左側有一個圖標,右側有一些文本。 我希望在點擊圖標時顯示一些動畫。我已經使用了ontouch監聽器,在MotionEvent.ACTION_DOWN事件和其他一些動作發生在MotionEvent.ACTION_UP事件上時,動畫開始於此處。Last ListItem總是收到ontouch事件

問題是,當我在列表視圖中單擊特定項目時,該圖標會爲該特定項目以及列表視圖中的最後一項獲取動畫..每次..不確定問題可能是什麼。請幫助。

代碼的相關部分粘貼如下:

public class Accounts implements OnItemClickListener, OnClickListener, AnimationListener{ 

Animation animation1; 
ImageButton folderBTN; 

//oncreate method 
    animation1 = AnimationUtils.loadAnimation(this, R.anim.to_middle); 
    animation1.setAnimationListener(this); 



    //Adapter getview method 
    .. 
    . 
    . 
    . 
    getView(){ 

     folderBTN.setOnTouchListener(new OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 

       if (event.getAction() == MotionEvent.ACTION_DOWN) { 
        folderBTN.clearAnimation(); 
        folderBTN.setAnimation(animation1); 
        folderBTN.startAnimation(animation1); 
        isIconShowing=true; 

       } else if (event.getAction() == MotionEvent.ACTION_UP) { 
        FolderList.actionHandleAccount(Accounts.this, 
          (Account) account); 
       } 
       return false; 
     } 

    } 


} 

回答

0

我終於明白了。上面代碼中的imagebutton folderbtn包含對生成的最後一個視圖的引用。使用不同的變量爲我解決了這個問題。這是代碼,以防有人可能需要它。

public boolean onTouch(View v, MotionEvent event) { 

        if (event.getAction() == MotionEvent.ACTION_DOWN) { 
         toAnimateBTN=(ImageButton) v.findViewById(R.id.folders); 
         toAnimateBTN.clearAnimation(); 
         toAnimateBTN.setAnimation(animation2); 
         toAnimateBTN.startAnimation(animation2); 
        } 
1

您的問題是要重複使用相同的動畫實例,試圖聲明onTouch方法裏面的動畫實例。嘗試下面的代碼片段。

public class Accounts implements OnItemClickListener, OnClickListener, AnimationListener{ 


    ImageButton folderBTN; 

    //oncreate method 


     //Adapter getview method 
     .. 
     . 
     . 
     . 
     getView(){ 

      folderBTN.setOnTouchListener(new OnTouchListener() { 

       @Override 
       public boolean onTouch(View v, MotionEvent event) { 

          Animation animation; 
          animation = AnimationUtils.loadAnimation(this, R.anim.to_middle); 
          animation.setAnimationListener(Accounts.this); 


        if (event.getAction() == MotionEvent.ACTION_DOWN) { 
         folderBTN.clearAnimation(); 
         folderBTN.setAnimation(animation); 
         folderBTN.startAnimation(animation); 
         isIconShowing=true; 

        } else if (event.getAction() == MotionEvent.ACTION_UP) { 
         FolderList.actionHandleAccount(Accounts.this, 
           (Account) account); 
        } 
        return false; 
      } 

     } 


    } 
+0

謝謝..我會嘗試這..但我想知道爲什麼這是發生在列表視圖中的最後一個項目.. – ambit