Android 4.4上的RecycleView中的AnimationDrawable自动播放(API 19)

时间:2017-06-30 02:56:12

标签: android animationdrawable

Android 4.4(API 19)上的RecycleView中的AnimationDrawable自动播放

活性

    public class RecycleViewFrameAnimationActivity extends AppCompatActivity implements View.OnClickListener {

    private RecyclerView recyclerView;
    private LinearLayoutManager manager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cardview);
        recyclerView = (RecyclerView) findViewById(R.id.rv);
        manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        recyclerView.addItemDecoration(new MyDecration());
        recyclerView.setAdapter(new RecyclerView.Adapter() {
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(RecycleViewFrameAnimationActivity.this).inflate(R.layout.frame_animation_activity, parent, false);
                MyViewHolder holder = new MyViewHolder(view);
                return holder;
            }

            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                MyViewHolder viewHolder = (MyViewHolder) holder;
            }


            @Override
            public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
                super.onViewDetachedFromWindow(holder);
                MyViewHolder viewHolder = (MyViewHolder) holder;
                ImageView imageView = viewHolder.view;
                AnimationDrawable drawable = (AnimationDrawable) imageView.getDrawable();
                if (drawable.isRunning()) {
                    drawable.stop();
                }
            }

            @Override
            public int getItemCount() {
                return 10;
            }

            class MyViewHolder extends RecyclerView.ViewHolder {

                ImageView view;

                public MyViewHolder(View itemView) {
                    super(itemView);
                    view = (ImageView) itemView.findViewById(R.id.frame_iv);
                    view.setOnClickListener(RecycleViewFrameAnimationActivity.this);
                }
            }

        });
    }

    @Override
    public void onClick(View v) {
        ImageView imageView = (ImageView) v;
        AnimationDrawable drawable = (AnimationDrawable) imageView.getDrawable();
        drawable.start();
    }
}

动画列表

    <?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item
        android:drawable="@drawable/sequences_10_02"
        android:duration="100"></item>
    <item
        android:drawable="@drawable/sequences_10_03"
        android:duration="100"></item>
    <item
        android:drawable="@drawable/sequences_10_04"
        android:duration="100"></item>
    <item
        android:drawable="@drawable/sequences_10_05"
        android:duration="100"></item>
    <item
        android:drawable="@drawable/sequences_10_06"
        android:duration="100"></item>
    <item
        android:drawable="@drawable/sequences_10_07"
        android:duration="100"></item>
    <item
        android:drawable="@drawable/sequences_10_08"
        android:duration="100"></item>
    <item
        android:drawable="@drawable/sequences_10_09"
        android:duration="100"></item>
    <item
        android:drawable="@drawable/sequences_10_10"
        android:duration="100"></item>
    <item
        android:drawable="@drawable/sequences_10_11"
        android:duration="100"></item>
    <item
        android:drawable="@drawable/sequences_10_12"
        android:duration="100"></item>
    <item
        android:drawable="@drawable/sequences_10_13"
        android:duration="100"></item>
</animation-list>

下面显示了问题 enter image description here

Android 7.0上没有出现此问题(其他版本的android未测试)

0 个答案:

没有答案