【问题标题】:Flip Animation in Android Listview itemAndroid Listview 项中的翻转动画
【发布时间】:2014-12-25 07:05:59
【问题描述】:

我正在尝试用Flip animation 显示我的每个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>

更新

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

【问题讨论】:

  • 尝试将您的构造函数更改为public MyFlipperListener(View parent, View frontFace,FlipAnimation flipAnimation),这样您就知道该做什么或如何去做,如果您的构造函数看起来如此......并在 if view is null 子句中实例化您的翻转动画......意思是把翻转动画放在artitemholder中..我的建议可能很棒或很糟糕..试试吧..让我知道
  • @Elltz 结果相同。更新我的问题只是为了告诉你我做了什么。
  • 好的,让我在发布答案之前问你几个问题,你是在翻转整个项目视图吗?还是只是图像视图的holder.rotator?其次,我认为您正在为 imageview 捕获 onclicklistener 对吗?
  • 我插入了我的布局 xml。请看一下。
  • @eskimoo:嗨,你找到任何解决方案了吗?我正在尝试同样的问题并遇到同样的问题,但找不到任何解决方案。请帮忙。

标签: android android-listview android-animation


【解决方案1】:

您的问题是以下几行:

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

您的动画也适用于其他项目,因为适配器重用了视图!您还可以通过在getView 中设置onClickListener 来解决问题。更好的方法是将其设置在外部,例如使用yourListView.setOnItemClickListener。希望对你有帮助:)

【讨论】:

  • 如何在 myListView.setOnItemClickListener 上的每个项目中单击按钮?
【解决方案2】:

请检查我的解决方案...

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

【讨论】:

  • 我刚刚看到你的回答。它没有帮助。而且我找不到将动画应用于旋转器图像视图本身的原因。你所做的突出显示结果为列表视图项目中的旋转器图像视图启动动画和卡片背面显示,而 cardFace 在没有动画的情况下隐藏
  • 它在什么意义上没有帮助?和第一个一样的错误?好吧,我问您是要翻转整个项目视图还是仅翻转图像视图?但你没有给出一个有效的答案..如果我不能帮助你希望有人可以,但我只是编辑了代码来解决问题的最后一部分@eskimoo
  • 我的意思是它不起作用。好的,让我们忘记翻转动画。因为我的问题是关于 item 中子元素中的 click 事件会影响其他项目。例如,我刚刚尝试更改单击子图像视图(旋转器)的项目的背景颜色,我看到其他一些项目背景也发生了变化
  • 我已经编辑了答案副本并粘贴了它,它应该可以工作..lol..更正格式或代码名称..
  • 查看您的答案时,它看起来确实不错。但是它并没有真正起作用。动画仍在应用于与我单击的项目完全不相关的其他项目。只是一个问题;为什么要对 view.findViewById(R.id.card_roretor) 应用动画?
猜你喜欢
  • 2011-03-22
  • 2017-07-06
  • 1970-01-01
  • 1970-01-01
  • 2013-12-21
  • 2012-01-24
  • 1970-01-01
  • 2018-02-17
  • 2016-06-25
相关资源
最近更新 更多