在Android Listview项目中翻转动画

在Android Listview项目中翻转动画,android,android-listview,android-animation,Android,Android Listview,Android Animation,我正试图用翻转动画显示我的列表视图项的正面和背面。动画效果很好,但动画的结果也被应用到其他项目中。而且,当我上下滚动时,物品的位置也会改变。我的代码如下: public View getView(final int position, final View convertView, ViewGroup parent) { ArtItemHolder holder; View view = convertView; if (view == null) {

我正试图用
翻转动画
显示我的
列表视图
项的正面和背面。动画效果很好,但动画的结果也被应用到其他项目中。而且,当我上下滚动时,物品的位置也会改变。我的代码如下:

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;
}
listview中项目的我的布局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>

您的问题是以下几行:

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

您的动画也应用于其他项目,因为适配器重用视图!在
getView
中设置
onClickListener
,也可能会出现问题。更好的方法是将其设置在外部,例如使用
yourListView.setOnItemClickListener
。希望有帮助:)

请检查我的解决方案

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

尝试将您的构造函数更改为
公共MyFlipperListener(查看父对象、查看正面、FlipAnimation FlipAnimation)
,这样您就知道该做什么,或者如果您的构造函数是这样的话,该如何做。。并在if view is null子句中恢复flipanimation。。意思是将flipanimation放入artitemholder。。我的建议可能很好,也可能是狗屎。。试试看,让我来know@Elltz结果是一样的。更新我的问题只是为了告诉你我做了什么。好吧,在我发布答案之前,让我问你几个问题,你是否正在翻转整个项目视图?或者只是支架旋转器,哪个是imageview?第二,我按下按钮,您正在捕获imageview的onclicklistener,对吗?我插入了布局xml。爱斯基摩人:嗨,你找到解决办法了吗?我也在尝试同样的方法,也遇到同样的问题,但找不到任何解决办法。请提供帮助。我如何在myListView.setOnItemClickListener上的每个项目中单击鼠标按钮?我刚刚看到了您的答案。这没有帮助。我找不到将动画应用于旋转器imageview本身的原因。当cardFace在没有动画的情况下隐藏时,在listview项开始动画和cardback中显示rotator imageview时,您所做的突出显示结果在什么意义上没有帮助?和第一个一样的错误?我问你是想翻转整个项目视图还是只翻转图像视图?但是你没有给出一个有效的答案。如果我不能帮助你,希望有人能,但我只是编辑了代码来修复问题的最后一部分@eskimooi意味着它不起作用。好吧,让我们忘记翻转动画。因为我的问题是关于item中的子元素中的click事件会影响其他项目。例如,我刚刚尝试更改其子imageview(旋转器)被单击的项目的背景颜色,我看到一些其他itesm背景也被更改。我已经编辑了答案副本并粘贴了它,它应该可以工作..哈哈。。更正格式或代码名。当查看您的答案时,它看起来确实是一个很好的方法。然而,它并没有真正起作用。动画仍然应用于其他与我单击的项目完全无关的项目。这是一个问题;为什么要将动画应用于view.findViewById(R.id.card\u roretor)?
private static class ArtItemHolder{
 ImageView image;
 TextView pubDate;
 TextView arTitle;
 TextView commentCount;
 ImageView rotator;
 View cardFace, cardBack;
 FlipAnimation flipAnimation;
}