滚动网格视图时淡入/淡出动画选择器的问题

时间:2016-10-18 23:42:38

标签: android animation gridview

滚动我的gridview时遇到问题,因为在滚动过程中突出显示所选单元格的选择器也会出现在其他单元格中(暂时)。在向此选择器添加fadeInfadeOut动画后,这个问题就开始了,但我真的想让动画,以便更平滑的过渡。

此网格的适配器代码为:

public class GridAdapter extends BaseAdapter {

    private Context ctx;
    private List<Model> modelList;

    public GridAdapter(Context ctx, List<Model> modelList) {
        this.ctx = ctx;
        this.modelList = modelList;
    }

    @Override
    public int getCount() {
        return modelList != null ? modelList.size() : 0;
    }

    @Override
    public Object getItem(int pos) {
        return modelList.get(pos);
    }

    @Override
    public long getItemId(int pos) {
        return pos;
    }

    @Override
    public View getView(int pos, View view, ViewGroup viewGroup) {

        final ViewHolder holder;

        if (view == null) {
            holder = new ViewHolder();

            view = LayoutInflater.from(ctx).inflate(R.layout.grid_cell, viewGroup, false);

            holder.gridCell = (TextView) view.findViewById(R.id.grid_cell);
            holder.cellSelected = (TextView) view.findViewById(R.id.cell_selected);

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        Model model = modelList.get(pos);

        holder.gridCell.setBackgroundResource(model.getBackId());
        holder.gridCell.setText(model.getLabel());

        if (model.isSelected()) {
            //fade In
            holder.cellSelected.setBackgroundResource(R.drawable.grid_selector);
            holder.cellSelected.setVisibility(View.VISIBLE);
            holder.cellSelected.animate().setDuration(1000).alpha(1.0f).setListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animator) {

                }

                @Override
                public void onAnimationEnd(Animator animator) {
                    holder.cellSelected.setAlpha(1.0f);
                    holder.cellSelected.setVisibility(View.VISIBLE);
                }

                @Override
                public void onAnimationCancel(Animator animator) {

                }

                @Override
                public void onAnimationRepeat(Animator animator) {

                }
            }).start();
        } else {
            //fade out
            holder.cellSelected.animate().setDuration(1000).alpha(0.0f).setListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animator) {

                }

                @Override
                public void onAnimationEnd(Animator animator) {
                    holder.cellSelected.setBackgroundResource(0);
                    holder.cellSelected.setAlpha(0.0f);
                    holder.cellSelected.setVisibility(View.INVISIBLE);
                }

                @Override
                public void onAnimationCancel(Animator animator) {

                }

                @Override
                public void onAnimationRepeat(Animator animator) {

                }
            }).start();
        }

        return view;
    }

    private class ViewHolder {
        TextView gridCell;
        TextView cellSelected;
    }
}

任何人都知道我要改变什么来阻止选择器在其他单元格中的错误显示(不选择一个。)?

任何消化?

注意 - fadeIn和fadeOut都适用于必须选择的单元格,唯一的问题是在滚动期间,一些应该取消选择的单元格也会被动画化。

已编辑 - 实际上我认为问题出在回收网格视图时,但我不知道如何解决它。拜托,这是项目中唯一缺少的东西。

由于

0 个答案:

没有答案