2014-12-23 65 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