2014-12-23 109 views
3

我試圖用Flip animation顯示我的每個listview項目的正面和背面。動畫效果很好,但動畫的結果也被應用於其他項目。而且,當我上下滾動時,我的物品的位置會改變。我的代碼如下:Android中的翻轉動畫Listview項目

public View getView(final int position, final View convertView, ViewGroup parent) { 
    ArtItemHolder holder; 
    View view = convertView; 

    if (view == null) { 
     LayoutInflater inflater = ((Activity) this.context).getLayoutInflater(); 
     holder = new ArtItemHolder(); 
     view = inflater.inflate(R.layout.feed_list_item, parent, false); 

     holder.image = (ImageView) view.findViewById(R.id.img_Image); 
     holder.pubDate = (TextView) view.findViewById(R.id.txt_puslishDate); 
     holder.arTitle = (TextView) view.findViewById(R.id.txt_arTitle); 
     holder.commentCount = (TextView) view.findViewById(R.id.txt_commentCount); 
     holder.rotator = (ImageView) view.findViewById(R.id.card_roretor); 
     holder.cardFace = view.findViewById(R.id.card_face);// first of 2 child parent layout of feed_list_item.xml 
     holder.cardBack = view.findViewById(R.id.card_back);// second of 2 child parent layout of feed_list_item.xml 
     view.setTag(holder); 
    } else { 
     holder = (AdvItemHolder) view.getTag(); 
    } 

    holder.rotator.setOnClickListener(new MyFlipperListener(view, holder.cardFace, holder.cardBack)); 

    return view; 
} 


private class MyFlipperListener implements OnClickListener{ 
    View parent, frontFace, backFace; 

    public MyFlipperListener(View parent, View frontFace, View backFace) { 
     this.parent = parent; 
     this.frontFace = frontFace; 
     this.backFace = backFace; 
    } 

    public void onClick(View v) { 
     FlipAnimation flipAnimation = new FlipAnimation(frontFace, backFace); 
     if (frontFace.getVisibility() == View.GONE) 
     { 
      flipAnimation.reverse(); 
     } 

     parent.startAnimation(flipAnimation); 
    } 

} 

private static class ArtItemHolder{ 
    ImageView image; 
    TextView pubDate; 
    TextView arTitle; 
    TextView commentCount; 
    ImageView rotator; 
    View cardFace, cardBack; 
} 

我在列表視圖項目佈局XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:gravity="center"> 

    <LinearLayout 
     android:id="@+id/card_face" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:background="@drawable/feed_item_selector" 
     android:layout_margin="8dip" 
     android:padding="2dip"> 

    ########## MAIN CONTENT HERE ########### 

    </LinearLayout> 
    <LinearLayout 
     android:id="@+id/card_back" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:background="@drawable/feed_item_selector" 
     android:layout_margin="8dip" 
     android:padding="2dip" 
     android:visibility="gone"> 

    ########## SOME INFO ABOUT MAIN CONTENT HERE ########### 

    </LinearLayout> 
</LinearLayout> 

UPDATE

FlipAnimation flipAnimation = new FlipAnimation(holder.cardFace, holder.cardBack); 
holder.rotator.setOnClickListener(new MyFlipperListener(view, holder.cardFace, flipAnimation)); 

private class MyFlipperListener implements OnClickListener{ 
     View parent, frontFace; 
     FlipAnimation flipAnimation; 
     public MyFlipperListener(View parent, View frontFace, FlipAnimation flip) { 
      this.parent = parent; 
      this.frontFace = frontFace; 
      this.flipAnimation = flip; 
     } 

     public void onClick(View v) { 
      if (frontFace.getVisibility() == View.GONE){ 
       flipAnimation.reverse(); 
      } 
      parent.startAnimation(flipAnimation); 
     } 
    } 
+0

試試這個改變你的構造函數'公共MyFlipperListener(查看父,查看frontFace,FlipAnimation flipAnimation)'所以你知道該怎麼做還是怎麼做呢,如果這是你的構造的樣子..並instatiate你的flipanimation中,如果視圖是空的子句..意思把flipanimation在artitemholder ..我的建議可能是偉大的或狗屎..只是嘗試它..讓我知道 – Elltz

+0

@Elltz結果是一樣的。更新我的問題只是爲了向你展示我做了什麼。 – eskimoo

+0

好的,讓我問你幾個問題,然後我發佈一個答案,你是翻轉整個項目視圖?或只是holder.rotator這是imageview?其次,我按了你正在捕捉的ImageView的onclicklistener權利? – Elltz

回答

0

你的問題是這些線路:

FlipAnimation flipAnimation = new 
FlipAnimation(holder.cardFace, holder.cardBack); 
holder.rotator.setOnClickListener(new MyFlipperListener(view, holder.cardFace, flipAnimation)); 

您r動畫也適用於其他項目,因爲適配器重新使用視圖!您也可以通過在getView中設置onClickListener來解決問題。更好的方法是將其設置在例如yourListView.setOnItemClickListener之外。希望它能幫助:)

+0

如何處理myListView.setOnItemClickListener上的每個項目中的按鈕單擊? – eskimoo

0

AYT檢查我的解決方案...

public View getView(final int position, final View convertView, ViewGroup parent) { 
ArtItemHolder holder; 
View view = convertView; 

if (view == null) { 
    LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
    holder = new ArtItemHolder(); 
    view = inflater.inflate(R.layout.feed_list_item, parent, false); 

    holder.image = (ImageView) view.findViewById(R.id.img_Image); 
    holder.pubDate = (TextView) view.findViewById(R.id.txt_puslishDate); 
    holder.arTitle = (TextView) view.findViewById(R.id.txt_arTitle); 
    holder.commentCount = (TextView) view.findViewById(R.id.txt_commentCount); 
    holder.rotator = (ImageView) view.findViewById(R.id.card_roretor); 
    holder.rotator.setTag(false); // put a boolean here.. 
    holder.cardFace = view.findViewById(R.id.card_face);// first of 2 child parent layout of feed_list_item.xml 
    holder.cardBack = view.findViewById(R.id.card_back);// second of 2 child parent layout of feed_list_item.xml 
    holder.flipAnimation = new FlipAnimation(holder.cardFace, holder.cardBack); 
    view.setTag(holder); 
} else { 
    holder = (AdvItemHolder) view.getTag(); 
} 

holder.rotator.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
    // TODO Auto-generated method stub 
    if (!((Boolean) v.getTag()).booleanValue()){ 
    v.setTag(true); // switch boolean 
    // you can either call notifydatasetchanged to show changes by the change function or put the change function here 
    }else{v.setTag(false); // switch boolean again if it has already been clicked} 
     } 
})); 
// i am putting the changes here-(change function) so intend i call notifydatasetchanged in the onclick 
// but it depends on your preference 
// check the boolean instead of view being visible.. 
     if (((Boolean) view.findViewById(R.id.card_roretor).getTag()).booleanValue()){ 
      flipAnimation.reverse(); 
      view.findViewById(R.id.card_roretor).startAnimation(holder.flipAnimation); 
     } 
// end of the change function 
return view; 
} 

........................... ....

private static class ArtItemHolder{ 
ImageView image; 
TextView pubDate; 
TextView arTitle; 
TextView commentCount; 
ImageView rotator; 
View cardFace, cardBack; 
FlipAnimation flipAnimation; 
} 
+0

我剛看到你的答案。它沒有幫助。我找不到爲什麼你將動畫應用到rotator imageview本身的原因。你做的東西給出了結果作爲rotator imageview在listview項目開始動畫和cardback顯示,而cardFace被隱藏沒有動畫 – eskimoo

+0

它不幫助什麼意義?像第一個一樣的錯誤?以及我問你是否試圖翻轉整個項目視圖或只是圖像視圖?但你沒有給出一個有效的答案..如果我不能幫助你希望有人可能,但我只是編輯代碼來修復問題的最後部分@eskimoo – Elltz

+0

我的意思是它沒有工作。好吧,讓我們忘記翻轉動畫。因爲我的問題是關於項目中的子元素中的點擊事件影響其他項目。例如,我只是試圖改變其子圖像視圖(旋轉器)被點擊的項目的背景顏色,並且我看到一些其他的itesm背景也被改變了 – eskimoo