使用自定义动画时,Recyclerview列表与快速滚动重叠

时间:2016-12-30 23:00:20

标签: android

您在使用动画时遇到RecyclerView列表重叠问题。

我的RecyclerView适配器:

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import java.util.Collections;
import java.util.List;


public class RecyclerViewAdapter extends RecyclerView.Adapter<ViewHolder> {

        List<Data> list = Collections.emptyList();
        Context context;

        public RecyclerViewAdapter(List<Data> list, Context context) {
            this.list = list;
            this.context = context;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            //Inflate the layout, initialize the View Holder
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.apps_layout, parent, false);
            ViewHolder holder = new ViewHolder(v);
            return holder;

        }

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

            //Use the provided View Holder on the onCreateViewHolder method to populate the current row on the RecyclerView
            holder.title.setText(list.get(position).title);
            holder.description.setText(list.get(position).description);
            holder.imageView.setImageResource(list.get(position).imageId);

            animate(holder); ///////////////// i am animating here

        }

    public void animate(RecyclerView.ViewHolder viewHolder) {

        Animation animAnticipateOvershoot = AnimationUtils.loadAnimation(context, R.anim.slidein);
        viewHolder.itemView.setAnimation(animAnticipateOvershoot);
    }

        @Override
        public int getItemCount() {
            //returns the number of elements the RecyclerView will display
            return list.size();
        }

        @Override
        public void onAttachedToRecyclerView(RecyclerView recyclerView) {
            super.onAttachedToRecyclerView(recyclerView);
        }

        // Insert a new item to the RecyclerView on a predefined position
        public void insert(int position, Data data) {
            list.add(position, data);
            notifyItemInserted(position);
        }

        // Remove a RecyclerView item containing a specified Data object
        public void remove(Data data) {
            int position = list.indexOf(data);
            list.remove(position);
            notifyItemRemoved(position);
        }

    }

viewholder.java:

import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


public class ViewHolder  extends RecyclerView.ViewHolder {

        CardView cv;
        TextView title;
        TextView description;
        ImageView imageView;
        TextView inststatus;

        ViewHolder(View itemView) {
            super(itemView);
            cv = (CardView) itemView.findViewById(R.id.cardViewapp);
            title = (TextView) itemView.findViewById(R.id.titleapp);
            description = (TextView) itemView.findViewById(R.id.descriptionapp);
            imageView = (ImageView) itemView.findViewById(R.id.imageViewapp);
        }
    }

使用这种方式

 RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        RecyclerViewAdapter adapter = new RecyclerViewAdapter(data, getApplication());
        recyclerView.setAdapter(adapter);
        recyclerView.setNestedScrollingEnabled(false);

如何修复滚动速度非常快时发生的重叠?没有动画时,不会发生重叠。

1 个答案:

答案 0 :(得分:1)

只需添加绑定视图持有者。在整个搜索之后,我们这个解决方案对我有用

holder.setIsRecyclable(false)