RecyclerView无法正确回收文本视图

时间:2016-03-05 16:57:09

标签: android android-recyclerview

我对RecyclerView和自定义TextView有疑问,后者只获得rotate属性:

public class IconTextView extends TextView {

    boolean rotate = false;

    public IconTextView(Context context) {
        super(context);
    }

    public IconTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public IconTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * Start rotate animation.
     * @param rotate boolean
     */
    public void setRotate(boolean rotate) {
        this.rotate = rotate;
        if (rotate) {
            rotateInfinite();
        }
    }

    private void rotateInfinite() {
        Animation anim = new RotateAnimation(0.0f, 360.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        anim.setDuration(1000);
        anim.setRepeatMode(Animation.INFINITE);
        anim.setRepeatCount(Animation.INFINITE);
        anim.setInterpolator(new LinearInterpolator());
        startAnimation(anim);
    }
}

适配器:

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.RotatingViewHolder> {

    public ArrayList<Item> items = new ArrayList<>();

    @Override
    public int getItemCount() {
        return items.size();
    }

    @Override
    public RotatingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new RotatingViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.search_item_single, parent, false));
    }

    @Override
    public void onBindViewHolder(RotatingViewHolder holder, int position) {

        Item item = items.get(position);

        holder.mTextViewIcon.setText(item.icon);
        holder.mTextViewIcon.setRotate(true);
        holder.mTextViewTitle.setText(item.title);
    }

    public static class RotatingViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.mTextViewIcon)
        IconTextView mTextViewIcon;

        @Bind(R.id.mTextViewTitle)
        TextView mTextViewTitle;

        public RotatingViewHolder(View mView) {
            super(mView);
            ButterKnife.bind(this, mView);
        }
    }

    public static class Item {

        String icon, title;
        boolean rotating;

        public Item(String icon, String title, boolean rotating) {
            this.icon = icon;
            this.title = title;
            this.rotating = rotating;
        }
    }
}

的活动:

public class MainActivity extends AppCompatActivity {

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        ItemAdapter itemAdapter = new ItemAdapter();

        recyclerView.setAdapter(itemAdapter);
        recyclerView.requestFocus();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        for (int i = 0; i < 40; i++) {
            itemAdapter.items.add(new ItemAdapter.Item("Foo", "Bar", true));
        }

        recyclerView.getAdapter().notifyDataSetChanged();
    }
}

所以这实际上是有效的,文字正在旋转。但是,只要我向下滚动一下,然后再向上滚动文本视图,其中隐藏的位置已停止旋转。知道会发生什么吗?

我将小项目推送到github,尝试一下:https://github.com/ArtworkAD/RecyclerViewTest

1 个答案:

答案 0 :(得分:1)

当视图符号被回收并且内部调用视图onDetachedFromWindow()时,动画将被清除。如果rotate trueonAttachedToWindow() @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); if (anim != null && rotate) { startAnimation(anim); } } 上,我就重启了动画。动画似乎工作正常。

IconTextView.java

{{1}}