回收网格中的错误。无需随机点击即可更改UI

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了回收网格中的错误。无需随机点击即可更改UI相关的知识,希望对你有一定的参考价值。

我在视图寻呼机中有一个回收器内部片段。

它把问题放在用户帐户的UI中随机但在DB中一切都很好。在日志中,我看到随机对BD没有影响。所以这个bug只在UI部分。刷新列表后,它可以再次出现,然后消失。我想分享代码,但我已经不知道问题出在哪里了。它可能位于适配器/刷新列表侦听器/ XML或任何其他位置。请告诉我您需要的代码部分,我会提供。也许这是一个回收者本身的错误,我无法解决它。

适配器类代码:

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.UserViewHolder>

    private List<FsUser> fsUserList = new ArrayList<>();
    private OnItemClickListener.OnItemClickCallback onItemClickCallback;
    private OnItemClickListener.OnItemClickCallback onChatClickCallback;
    private OnItemClickListener.OnItemClickCallback onLikeClickCallback;
    private Context context;



    public SearchAdapter(OnItemClickListener.OnItemClickCallback onItemClickCallback,
                         OnItemClickListener.OnItemClickCallback onChatClickCallback,
                         OnItemClickListener.OnItemClickCallback onLikeClickCallback) 
        this.onItemClickCallback = onItemClickCallback;
        this.onChatClickCallback = onChatClickCallback;
        this.onLikeClickCallback = onLikeClickCallback;
    

    public void addUsers(List<FsUser> userList) 
        fsUserList.addAll(userList);
        notifyItemRangeInserted(fsUserList.size() - userList.size(), fsUserList.size());
    

    public void clearData()
        fsUserList.clear();
        notifyDataSetChanged();
    

    @NonNull
    @Override
    public UserViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
        context = parent.getContext();
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user, parent, false);
        return new UserViewHolder(v);
    

    @Override
    public void onBindViewHolder(@NonNull UserViewHolder holder, int position) 
        FsUser fsUser = fsUserList.get(position);
        holder.bind(fsUser, position);
    

    @Override
    public int getItemCount() 
        return fsUserList.size();
    

    public String getLastItemId()
        return fsUserList.get(fsUserList.size() - 1).getUid();
    

    class UserViewHolder extends RecyclerView.ViewHolder 
        RelativeLayout container;
        ImageView imageView, like, chat;
        TextView name, country;

        private LottieAnimationView animationView;

        UserViewHolder(View itemView) 
            super(itemView);
            context = itemView.getContext();
            container = itemView.findViewById(R.id.item_user_container);
            imageView = itemView.findViewById(R.id.user_img);
            like = itemView.findViewById(R.id.search_btn_like);
            chat = itemView.findViewById(R.id.search_btn_chat);
            name = itemView.findViewById(R.id.user_name);
            country = itemView.findViewById(R.id.user_country);
            animationView = itemView.findViewById(R.id.lottieAnimationView);
        

        void bind(FsUser fsUser, int position)
            ViewCompat.setTransitionName(imageView, fsUser.getName());

            if (FirebaseUtils.isUserExist() && fsUser.getUid() != null) 
                new FriendRepository().isLiked(fsUser.getUid(), flag -> 
                    if (flag) 
                        like.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_favorite));
                        animationView.setVisibility(View.VISIBLE);
                    
                );
            

            if(fsUser.getUid() != null) 
                chat.setOnClickListener(new OnItemClickListener(position, onChatClickCallback));
                like.setOnClickListener(new OnItemClickListener(position, onLikeClickCallback));
            

            imageView.setOnClickListener(new OnItemClickListener(position, onItemClickCallback));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);

            if(fsUser.getImage().equals("default"))
                Glide.with(context).load(context.getResources().getDrawable(R.drawable.default_avatar)).into(imageView);
             else 
                Glide.with(context).load(fsUser.getImage()).thumbnail(0.5f).into(imageView);
            

            name.setText(fsUser.getName());
            country.setText(fsUser.getCountry());

            ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f).setDuration(500);
            animator.addUpdateListener(valueAnimator ->
                    animationView.setProgress((Float) valueAnimator.getAnimatedValue()));

            if (animationView.getProgress() == 0f) 
                animator.start();
             else 
                animationView.setProgress(0f);
            
        
    


和RecyclerView项目的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/item_user_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:layout_width="@dimen/user_cv_width"
        android:layout_height="@dimen/user_cv_height"
        android:layout_margin="@dimen/dp4"
        android:elevation="@dimen/dp4">

        <RelativeLayout
            android:id="@+id/item_user_main_relative_container"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:id="@+id/item_user_top_relative_container"
                android:layout_width="@dimen/user_rl_width"
                android:layout_height="@dimen/user_rl_height">

                <ImageView
                    android:id="@+id/user_img"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:scaleType="centerCrop"
                    android:src="@drawable/default_avatar" />


                <RelativeLayout
                    android:id="@+id/item_user_top_relative"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/user_item_bg"
                        android:orientation="vertical">
                        <TextView
                            android:id="@+id/user_name"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="@dimen/dp4"
                            android:textColor="@android:color/white"
                            android:textSize="@dimen/medium_text_size" />

                        <TextView
                            android:id="@+id/user_country"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@android:color/white"
                            android:layout_marginStart="@dimen/dp4"
                            android:textSize="@dimen/medium_text_size" />
                    </LinearLayout>
                </RelativeLayout>
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/item_user_bottom_relative_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:orientation="horizontal">
                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/dp12">
                        <ImageView
                            android:id="@+id/search_btn_like"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/heart_outline"
                            android:contentDescription="@string/search_btn_like_desc"/>
                        <com.airbnb.lottie.LottieAnimationView
                            android:id="@+id/lottieAnimationView"
                            android:visibility="gone"
                            android:layout_width="@dimen/lottie_animation_view_size"
                            android:layout_height="@dimen/lottie_animation_view_size"
                            app:lottie_loop="true"
                            app:lottie_autoPlay="true"
                            app:lottie_fileName="like.json"/>
                    </RelativeLayout>

                    <ImageView
                        android:id="@+id/search_btn_chat"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/dp12"
                        android:layout_weight="1"
                        android:src="@drawable/message_outline" />
                </LinearLayout>
            </RelativeLayout>
        </RelativeLayout>
    </android.support.v7.widget.CardView>

</RelativeLayout>

enter image description here

答案

在适配器中覆盖这两种方法

@Override
public long getItemId(int position) 
            return position;


@Override
public int getItemViewType(int position) 
       return position;

以上是关于回收网格中的错误。无需随机点击即可更改UI的主要内容,如果未能解决你的问题,请参考以下文章

无需等待 API 响应即可更改图标状态。 (标记为收藏功能)

Kendo UI Grid:如果有任何未决更改,则无法拦截和取消排序事件

无需轮询即可检测 PHP 中的文件更改

更改点击事件的网格布局

UI 测试失败:发现多个匹配错误

Prisma 无需等待即可创建/更新