在显示新添加的项目之前,将RecyclerView可见性从Gone更改为Visible,错误地显示以前删除的项目

时间:2017-03-17 12:27:07

标签: android android-recyclerview visibility

背景/上下文:

在我的主要活动中,我有一个标签布局有两个标签。每个选项卡包含两个片段,SearchVehicleFragment和VehicleListFragment。

以下是VehicleListFragment的布局文件。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".ui.fragments.VehicleListFragment"
    android:orientation="vertical">


    <com.boulevardclan.vvp.ui.recyclerviews.VehicleRecyclerView
        android:id="@+id/rvVehicleList"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/tvNoSearchedVehicles"
        style="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="You haven't searched for any vehicles yet." />
</LinearLayout>

我的VehicleRecyclerView的创建方式与PlacesRecyclerView可用here完全相同。这是因为我想为我的VehicleRecyclerView实现空状态机制。此外,在初始化期间,我的VehicleRecyclerView具有setHasFixedSize(true)

public class VehicleRecyclerView extends RecyclerView {
    private View mEmptyView;

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

    private AdapterDataObserver mDataObserver = new AdapterDataObserver() {
        @Override
        public void onChanged() {
            super.onChanged();
            updateEmptyView();
        }

        @Override
        public void onItemRangeRemoved(int positionStart, int itemCount) {
            super.onItemRangeRemoved(positionStart, itemCount);
            try{
                updateEmptyView();
            }catch(Exception e){
            //                TODO
            }
        }

        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            super.onItemRangeInserted(positionStart, itemCount);
            updateEmptyView();
        }

        @Override
        public void onItemRangeChanged(int positionStart, int itemCount) {
            super.onItemRangeChanged(positionStart, itemCount);
            updateEmptyView();
        }

        @Override
        public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
            super.onItemRangeMoved(fromPosition, toPosition, itemCount);
            updateEmptyView();
        }
    };

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

    public VehicleRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * Designate a view as the empty view. When the backing adapter has no
     * data this view will be made visible and the recycler view hidden.
     *
     */
    public void setEmptyView(View emptyView) {
        mEmptyView = emptyView;
    }

    @Override
    public void setAdapter(RecyclerView.Adapter adapter) {
        if (getAdapter() != null) {
            getAdapter().unregisterAdapterDataObserver(mDataObserver);
        }
        if (adapter != null) {
            adapter.registerAdapterDataObserver(mDataObserver);
        }
        super.setAdapter(adapter);
        updateEmptyView();
    }

    private void updateEmptyView() {
        if (mEmptyView != null && getAdapter() != null) {
            boolean showEmptyView = getAdapter().getItemCount() == 0;
            if(showEmptyView){
                mEmptyView.setVisibility(VISIBLE);
                setVisibility(GONE);
            }else {
                mEmptyView.setVisibility(GONE);
                setVisibility(VISIBLE);
            }
        }
    }
}

我的VehicleAdapter有setHasStableIds(true)并覆盖getItemId(position)

public class VehicleAdapter extends RecyclerView.Adapter<VehicleAdapter.VehicleViewHolder> {

    private List<Vehicle> vehicleList = new ArrayList<>();
    private Context mContext;

    public VehicleAdapter(Context context, List<Vehicle> vehicles) {
        vehicleList.addAll(vehicles);
        setHasStableIds(true);
        mContext = context;
    }

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

    @Override
    public void onBindViewHolder(VehicleViewHolder holder, int position) {
        holder.bind(vehicleList.get(position));
    }

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

    @Override
    public long getItemId(int position) {
        return vehicleList.get(position).getId();
    }

    public void addVehicle(Vehicle vehicle){
        if(vehicleList != null){
            vehicleList.add(0, vehicle);
            notifyItemInserted(0);
        }
    }

    public void removeVehicle(int position){
        if(vehicleList.get(position).delete()){
            vehicleList.remove(position);
            notifyItemRemoved(position);
        }else {
//            TODO
        }
    }

    public void updateVehicle(Vehicle vehicle, int position){
        vehicleList.set(position, vehicle);
        notifyItemChanged(position);
    }

    class VehicleViewHolder extends RecyclerView.ViewHolder{

        TextView tvRegistrationNumber;
        TextView tvOwnerName;
        TextView tvColor;
        TextView tvOwnerCity;
        TextView tvLookupDate;
        TextView tvManufacturer;
        TextView tvModel;
        TextView tvMakeYear;

        ImageView ivDetail;
        ImageView ivBookmark;
        ImageView ivDelete;

        public VehicleViewHolder(View itemView) {
            super(itemView);

            tvRegistrationNumber = (TextView) itemView.findViewById(R.id.tvRegistrationNumber);
            tvOwnerName = (TextView) itemView.findViewById(R.id.tvOwnerName);
            tvColor = (TextView) itemView.findViewById(R.id.tvColor);
            tvOwnerCity = (TextView) itemView.findViewById(R.id.tvOwnerCity);
            tvLookupDate = (TextView) itemView.findViewById(R.id.tvLookupDate);
            tvManufacturer = (TextView) itemView.findViewById(R.id.tvManufacturer);
            tvModel = (TextView) itemView.findViewById(R.id.tvModel);
            tvMakeYear = (TextView) itemView.findViewById(R.id.tvMakeYear);

            ivDetail = (ImageView) itemView.findViewById(R.id.ivDetail);
            ivBookmark = (ImageView) itemView.findViewById(R.id.ivBookmark);
            ivDelete = (ImageView) itemView.findViewById(R.id.ivDelete);
        }

        void setOwnerName(String ownerName){
            tvOwnerName.setText(ownerName);
        }
        void setColor(String color){
            tvColor.setText(color);
        }
        void setOwnerCity(String ownerCity){
            tvOwnerCity.setText(ownerCity);
        }
        void setLookupDate(String lookupDate){
            tvLookupDate.setText(lookupDate);
        }
        void setManufacturer(String manufacturer){
            tvManufacturer.setText(manufacturer);
        }
        void setMakeYear(int makeYear){
            tvMakeYear.setText(String.format(Locale.getDefault(),"%s",makeYear));
        }
        void setModel(String model){
            tvModel.setText(model);
        }
        void setBookmarked(boolean isBookmarked){
            ivBookmark.setImageDrawable(ViewUtils.getDrawable(mContext, (isBookmarked ? R.drawable.ic_favorite_black_24dp: R.drawable.ic_favorite_border_black_24dp)));
        }

        void bind(final Vehicle vehicle){
            tvRegistrationNumber.setText(vehicle.getRegistrationNumber());
            setOwnerName(vehicle.getOwnerName());
            setColor(vehicle.getColor());
            setOwnerCity(vehicle.getOwnerCity());
            setLookupDate(DateTimeUtils.getPKTDateTime(vehicle.getModifiedAt()));
            setManufacturer(vehicle.getManufacturer());
            setModel(vehicle.getMakeType());
            setMakeYear(vehicle.getMakeYear());
            setBookmarked(vehicle.isBookmarked());
            setupClickListeners(vehicle);
        }

        private void setupClickListeners(final Vehicle vehicle) {
            ivDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new MaterialDialog.Builder(mContext)
                            .title(R.string.confirm_delete_vehicle_heading)
                            .content(R.string.confirm_delete_vehicle_label)
                            .positiveText(R.string.confirm_response_yes)
                            .negativeText(R.string.confirm_response_cancel)
                            .stackingBehavior(StackingBehavior.ALWAYS)
                            .onPositive(new MaterialDialog.SingleButtonCallback() {
                                @Override
                                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                    if(getAdapterPosition() != RecyclerView.NO_POSITION){
                                        removeVehicle(getAdapterPosition());
                                    }
                                }
                            })
                            .show();
                }
            });
        }
    }
}

问题:

当recyclerview中没有项目并且我通过notifyItemInserted(0)向Recyclerview添加了一个或多个新项目时,一切正常。 但是当recyclerview中只有一个项目时,我会通过notifyItemRemoved(position)删除该项目,现在通过notifyItemInserted(0)向recyclerview添加一个新项目,有一个过渡/动画效果,其中隐藏了emptyView后, recyclelerview与之前删除的项目一起显示,只需几分之一秒,然后新添加的项目淡入视口。

所以,这是应该遵循的'理想'事件序列:

  1. [正常工作] 我删除了recyclerview中的最后一项。 recyclerview的可见性更改为GONE,emptyView(TextView)可见性更改为可见。
  2. [正常工作] 然后,我向recyclerview添加了一个新项目。 emptyView的可见性已更改为GONE。
  3. [未按预期工作] Recyclerview的可见性应更改为VISIBLE且它应仅包含一个项目,即新添加的项目[不按预期工作] 相反,有一个flickr / blink,其中只有先前存在的项目的recyclelerview会在几分之一秒内显示,然后通过淡入(?)过渡将该项目替换为新添加的项目。
  4. 更新:您可以在此处查看此问题:https://media.giphy.com/media/l0IydcuJDAqPNlmla/giphy.gif

    我期待一种摆脱这种flickr / blink效果的方法,该效果显示旧项目/ recyclerview状态非常简单。

    我尝试了什么:

    RecyclerView.ItemAnimator animator = mVehicleListRV.getItemAnimator();
    if (animator instanceof SimpleItemAnimator) {
        ((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
    }
    

    免责声明:

    我是Android开发的新手,并且在这个问题上坚持了好几个小时。寻找SO的伟大思想家的帮助/指针以获得畅通无阻。请承担我有限的知识。

2 个答案:

答案 0 :(得分:0)

您可以使用可视性INVISIBLE而不是GONE,在这种情况下,即使适配器不可见,也会调用onBindViewHolder。

答案 1 :(得分:0)

我遇到了这个问题 - 事实上,当 Android 系统认为 RecyclerView 不可见时,它不会重新布局它的孩子。这将在重新显示 RecyclerView 时产生闪烁,因为在可以为新数据更新视图之前,旧的孩子仍然会看到一小段时间。

我尝试了几种不同的方法来欺骗 RecyclerView 使其对用户不可见,同时仍然更新其子项。

一些不起作用的方法:

  • 将 RecyclerView 可见性设置为 GONE
  • 将 RecyclerView 可见性设置为 INVISIBLE
  • 将 RecyclerView alpha 设置为 0

所有这些方法导致 RecyclerView 消失,但也没有重新布局它的子项,导致闪烁。

确实有效的方法:

  • 将 RecyclerView 高度设置为 1

我在 ConstraintLayout 中有我的 RecyclerView,所以我写了一个小实用函数。它看起来像这样:

/**
 * Toggle whether [recyclerView] is visible.
 * It does not use [View.setVisibility] or [View.setAlpha],
 * because doing so will prevent recyclerView children from being updated,
 * and this creates a flicker.
 *
 * Instead, this workaround makes the recyclerView too small to be seen,
 * while 'tricking' the Android system into thinking the view is still visible,
 * so allows View updates to continue as necessary.
 */
fun toggleRecyclerView(shouldShow: Boolean) {
    recyclerView.updateLayoutParams {
        // Use 1 as the 'invisible' height - it's the smallest height available to us
        height = if(shouldShow) MATCH_CONSTRAINT else 1
    }
}

我将此与 submitList 提供的 ListAdapter 回调相结合 - 这只会在新旧数据之间计算异步差异后重新显示 RecyclerView,然后是 {{1} } - 等待 RecyclerView 子项更新:

View.post