停止动画以重新开始向后滚动

时间:2014-07-27 11:29:34

标签: android

我想要突出显示新的行项目,并且我要在所有新行项目中显示图像。 (注意:所有行项都加在一起,只有未读的行项被突出显示)。 我有一个ImageView:

<LinearLayout
    android:layout_width="30dp"
    android:layout_height="match_parent"
    android:gravity="left">

    <ImageView
        android:id="@+id/unreaddead"
        android:src="@android:color/transparent"
        android:layout_width="8dp"
        android:layout_height="90dp"
        android:background="@android:color/transparent"/>

</LinearLayout>

(我通过使用&#34; setBackgroundColor(Color.BLUE)设置适配器本身的颜色;

我一直在尝试淡化ListView中突出显示的行项目中的图像。为此我使用动画文件:

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:duration="3000" android:repeatCount="0"/></set>

我在适配器中将带有rowitems的动画附加为:

public View getView(int position, View convertView, final ViewGroup parent) {
    ObjectPerCourseDeadline currentObject = obj.get(position);

    LayoutInflater inflater = (LayoutInflater) this.supercontext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    rowView = inflater.inflate(R.layout.single_deadline_layout,
            parent, false);

    final View unreadnot = rowView.findViewById(R.id.unreaddead);
    unreadimg = (ImageView)rowView.findViewById(R.id.unreaddead);
    fadeinanimation = AnimationUtils.loadAnimation(getContext(), R.anim.fade_image);
    unreadimg.startAnimation(fadeinanimation);
    fadeinanimation.setAnimationListener(new Animation.AnimationListener() {
        public void onAnimationEnd(Animation animation) {
            unreadnot.setBackgroundColor(Color.TRANSPARENT);
        }
        public void onAnimationRepeat(Animation animation) {
            unreadnot.setBackgroundColor(Color.TRANSPARENT);
            // TODO Auto-generated method stub
        }
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub
        }
    });

    if(position < FragmentViewPager.deadnotlen){
        unreadnot.setBackgroundColor(Color.BLUE);
        Log.d("color changes to blue","of background of listview");
    }

    Typeface tf2 = Typeface.createFromAsset(this.supercontext.getAssets(), "fonts/Roboto-Regular.ttf");
    Typeface tf = Typeface.createFromAsset(this.supercontext.getAssets(), "fonts/Roboto-Light.ttf");
    TextView title = (TextView) rowView.findViewById(R.id.title);
    title.setText(removeHTML(currentObject.getTitle()));
    title.setTypeface(tf2);
    if(currentObject.getBody().length()!=0)
    {
        TextView body = (TextView) rowView.findViewById(R.id.body);
        body.setTypeface(tf);
        body.setText(removeHTML(currentObject.getBody()));}
        TextView end = (TextView) rowView.findViewById(R.id.endDateTime);
        end.setTypeface(tf);
        end.setText(removeHTML(currentObject.getEndtime().split("\\+")[0]));
        TextView duration = (TextView) rowView.findViewById(R.id.duration);
        duration.setTypeface(tf);
        duration.setText(removeHTML(currentObject.getTimestamp().split(" ")[0]));
    return rowView;
}

但即使是我不想要的滚动,这也会重新加载。我只想运行这个动画一次,特别是不要滚动

我知道这是因为行项目再次被加载但是更好的想法呢?

修改1

我已经更新了我正在使用的getView函数代码。很抱歉没有提供足够的详细信息现在的问题是即使滚动回某个行项目也会调用getView,因此动画再次发生,这是我不想要的。

1 个答案:

答案 0 :(得分:0)

好的,所以我通过维护静态变量修复了这个问题,该变量保持了屏幕上显示的最大位置,因此我只能在列表第一次向下滚动时应用动画。以下是摘录:

static Integer max_position = -1;

public View getView(int position, View convertView, final ViewGroup parent) {
    ObjectPerCourseDeadline currentObject = obj.get(position);

    LayoutInflater inflater = (LayoutInflater) this.supercontext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    rowView = inflater.inflate(R.layout.single_deadline_layout,
            parent, false);

    final View unreadnot = rowView.findViewById(R.id.unreaddead);
    if( position > max_position ) {
        unreadimg = (ImageView) rowView.findViewById(R.id.unreaddead);
        fadeinanimation = AnimationUtils.loadAnimation(getContext(), R.anim.fade_image);
        unreadimg.startAnimation(fadeinanimation);
        fadeinanimation.setAnimationListener(new Animation.AnimationListener() {
            public void onAnimationEnd(Animation animation) {
                unreadnot.setBackgroundColor(Color.TRANSPARENT);
            }

            public void onAnimationRepeat(Animation animation) {
                unreadnot.setBackgroundColor(Color.TRANSPARENT);
                // TODO Auto-generated method stub
            }

            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub
            }
        });
        max_position = position;
        unreadnot.setBackgroundColor(Color.TRANSPARENT);
    }

    if(position < FragmentViewPager.deadnotlen && position >= max_position){
        unreadnot.setBackgroundColor(Color.BLUE);
        Log.d("color changes to blue","of background of listview");
    }

    Typeface tf2 = Typeface.createFromAsset(this.supercontext.getAssets(), "fonts/Roboto-Regular.ttf");
    Typeface tf = Typeface.createFromAsset(this.supercontext.getAssets(), "fonts/Roboto-Light.ttf");
    TextView title = (TextView) rowView.findViewById(R.id.title);
    title.setText(removeHTML(currentObject.getTitle()));
    title.setTypeface(tf2);
    if(currentObject.getBody().length()!=0)
    {
        TextView body = (TextView) rowView.findViewById(R.id.body);
        body.setTypeface(tf);
        body.setText(removeHTML(currentObject.getBody()));}
        TextView end = (TextView) rowView.findViewById(R.id.endDateTime);
        end.setTypeface(tf);
        end.setText(removeHTML( currentObject.getEndtime().split("\\+")[0]));
        TextView duration = (TextView) rowView.findViewById(R.id.duration);
        duration.setTypeface(tf);
        duration.setText(removeHTML(currentObject.getTimestamp().split(" ")[0]));

    return rowView;
    }

任何更好的解决方案是欢迎!。