简体   繁体   English

调用notifydatasetchanged()之后,在Recyclerview中保持视图状态不变

[英]Keeping state of a view constant in Recyclerview after calling notifydatasetchanged()

I have a recycler adapter which has 3 itemviewtypes. 我有一个具有3个itemviewtypes的回收站适配器。 No.1 type of view has an initial animation to show the view which I've written under the holder instance check in the OnBindView . 第一种视图类型具有初始动画,以显示我在OnBindView的holder实例检查下编写的OnBindView it works fine. 它工作正常。 but when there is any change in the other view types, notifydatasetchanged() is called and the adapter is set again which makes the animation start again. 但是当其他视图类型发生任何变化时,将调用notifydatasetchanged()并再次设置适配器,从而使动画再次开始。 i tried putting a flag for the animation where it would only animate the first time. 我尝试为动画设置标志,使其仅在第一时间进行动画处理。 is there a smarter way to do this? 有没有更聪明的方式做到这一点?

    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int 
    viewType) {

mSharedPreferencesManager.setVersionNewFeatureCheck(BuildConfig.VERSION_NAME);

    Logger.i(TAG, "onCreateViewHolder, " + viewType);

    if (viewType == TYPE_HEADER) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.converation_header_row, parent, false);
        return new HeaderViewHolder(v);
    } else if(viewType == TYPE_ANNOUNCEMENT) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_new_feature_notification, parent, false);
        return new FeatureNotificationViewHolder(v);
    }
        else{
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.conversation_row, parent, false);
        return new ConversationRowViewHolder(v);
    }
}

f (holder instanceof FeatureNotificationViewHolder) { final FeatureNotificationViewHolder featureNotificationViewHolder = (FeatureNotificationViewHolder) holder; f(FeatureNotificationViewHolder的所有者实例){最终FeatureNotificationViewHolder featureNotificationViewHolder =(FeatureNotificationViewHolder)所有者;

                featureNotificationViewHolder.chatNewFeatureNotif.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                    @Override
                    public boolean onPreDraw() {

                        final int height = featureNotificationViewHolder.chatNewFeatureNotif.getMeasuredHeight();
                        featureNotificationViewHolder.chatNewFeatureNotif.setVisibility(View.GONE);
                        featureNotificationViewHolder.chatNewFeatureNotif.getViewTreeObserver().removeOnPreDrawListener(this);
                        mOverflowAnimations.showView(featureNotificationViewHolder.chatNewFeatureNotif, height, 6);

                        return false;
                    }
                });

.notifyDataSetChanged() will notify all of your items inside the RecyclerView, if you don't want everything to reset consider using .notifyItemChanged(int position) method on your adapter instance. .notifyDataSetChanged()将通知您在RecyclerView中的所有项目,如果您不想重置所有内容,请考虑在适配器实例上使用.notifyItemChanged(int position)方法。 You'll need to know exactly the index/position of the item to be notified. 您需要确切知道要通知的项目的索引/位置。

Instead of using notifyDataSetChanged , you should use notifyItemChanged to only change the item that needs to be changed, and not the whole adapter or data set. 而不是使用notifyDataSetChanged ,应该使用notifyItemChanged仅更改需要更改的项目,而不是整个适配器或数据集。

This won't lead to the unnecessary animation because you're only changing a specific item. 这不会导致不必要的动画,因为您仅更改特定项目。 This is also much faster and takes up less processing and rendering power. 这也快得多,并且占用较少的处理和渲染功能。

You can read more about it here . 您可以在此处了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM