RecyclerView不会在Android

RecyclerView不会在Android

本文介绍了RecyclerView不会在Android 5.1.1上更新UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,仅在Android 5.1.1和5.1上的设备很少.在其余版本上都可以正常工作.

I have a issue that reprodusing only on Android 5.1.1 and few devices on 5.1. On the rest of versions works fine.

Recycler视图具有onClick侦听器,我尝试将View Visibility从GONE更新为VISIBLE或相反.单击项目后,UI上没有任何更改.注意:如果我关闭(BackButton)片段并再次打开-UI将被更新.即使按下后退按钮,我仍然看到UI在销毁前会更新几毫秒.

Recycler view have onClick listener whereI try to update View Visibility from GONE to VISIBLE or conversely. After clicking on item nothing changes on UI.Note: If I close (BackButton) Fragment and open once again - UI will be updated. Even after pressing back button I see that UI updating for few milliseconds before destroing.

我试图通知所有适配器,当前项,使itemView无效,swap()并再次设置适配器,但均未成功.所有方法setVisibility(),onClick,Callbacks,BindView均已成功调用(调试器).

I tryed to notify all adapter, current item, invalidate itemView, swap() and set adapter again without success.All methods setVisibility(), onClick, Callbacks, BindView are successfully called (debugger).

public class PhotoGalleryAdapter extends RecyclerView.Adapter<PhotoGalleryAdapter.PhotoHolder> {

    private Context context;
    private List<String> photos;
    private View.OnClickListener listener;

    public PhotoGalleryAdapter(Context context, List<String> photos, View.OnClickListener listener) {
        this.context = context;
        this.photos = photos;
        this.listener = listener;
    }

    @Override
    public PhotoGalleryAdapter.PhotoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_photo, parent, false);
        return new PhotoHolder(v, listener);
    }

    @Override
    public void onBindViewHolder(final PhotoGalleryAdapter.PhotoHolder holder, int position) {
        String photoPath = photos.get(position);
        Glide.with(context)
                .load(photoPath)
                .thumbnail(0.1f)
                .into(holder.image);

        if (Utils.getSelectedPhotos().contains(photoPath)) {
            DrawableCompat.setTint(holder.selection.getDrawable(), ContextCompat.getColor(context, R.color.white));
            holder.selection.setVisibility(View.VISIBLE);
        } else {
            holder.selection.setVisibility(View.GONE);
        }
    }

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

    public void updateDataSet(List<String> photos) {
        if (!photos.equals(this.photos)) {
            this.photos.clear();
            this.photos.addAll(photos);
            notifyDataSetChanged();
        }
    }

    class PhotoHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.imageView) AppCompatImageView image;
        @BindView(R.id.selectionState) AppCompatImageView selection;

        private View.OnClickListener listener;

        PhotoHolder(View view, View.OnClickListener listener) {
            super(view);
            ButterKnife.bind(this, view);
            view.setOnClickListener(this);
            this.listener = listener;
        }

        @Override
        public void onClick(View view) {
           if (Utils.getSelectedPhotos().contains(photos.get(getAdapterPosition()))) {
                selection.setVisibility(View.GONE);
                Utils.getSelectedPhotos().remove(photos.get(getAdapterPosition()));
            } else {
                Utils.getSelectedPhotos().add(photos.get(getAdapterPosition()));
                DrawableCompat.setTint(selection.getDrawable(), ContextCompat.getColor(context, R.color.white));
                selection.setVisibility(View.VISIBLE);
            }
            listener.onClick(view);
        }
    }
}

然后回调:

 @Override public void onClick(View view) {
     callbacks.onPhotoSelected(Utils.getSelectedPhotos().size());
     adapter.notifyDataSetChanged(); }

推荐答案

更新解决方案:将onClick更改为:

Update Solution: after changing onClick to:

     @Override
            public void onClick(View view) {
               if (Utils.getSelectedPhotos().contains(photos.get(getAdapterPosition()))) {
                    Utils.getSelectedPhotos().remove(photos.get(getAdapterPosition()));
                } else {
                    Utils.getSelectedPhotos().add(photos.get(getAdapterPosition()));
                }
                notifyDataSetChanged();
                listener.onClick(view);
            }

效果很好,但是仅适用于LinearLayoutManager.仍然不适用于GridLayoutManager.因此,在两次调用setVisibility()之后,Android无法正常工作.

Works good, BUT only with LinearLayoutManager. Still doesn't works with GridLayoutManager.So after calling setVisibility() twice Android works unpredictably.

这篇关于RecyclerView不会在Android 5.1.1上更新UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 07:40