为什么我的回收站视图在向上滚动时会断断续续/略微卡顿?

问题描述

这个活动有一些问题,这很奇怪,因为我在另一个片段中运行相同的代码,它工作得很好,但由于某种原因,在这个活动上向上滚动时它真的断断续续。此时我不确定该怎么做,然后尝试重新编码活动以找到问题的根源。扩展布局管理器以提前加载 15 个视图是一种帮助,因为我不必加载它,但它与适配器有关以及它回收视图的方式

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_profile_posts);
    mHostId = getIntent().getStringExtra("hostId");
    mHostName = getIntent().getStringExtra("hostName");
    mGroupId = getIntent().getExtras().getString("groupId");

    mFirebaseStoragePosts = FirebaseStorage.getInstance("gs://enchanted-files");
    mFirebaseStorageUserImg = FirebaseStorage.getInstance();
    mFirebaseDatabase = FirebaseDatabase.getInstance();
    mFirebaseFireStore = FirebaseFirestore.getInstance();

    mTitle = (TextView) findViewById(R.id.ID_home_following_seePost_Title);
    mTitle.setText("Announcements");
    mGoBack = (ImageButton) findViewById(R.id.ID_home_following_seePost_goBack);
    mGoBack.setonClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

    mloadingLayout = (RelativeLayout) findViewById(R.id.ID_home_following_seePost_loadingView);
    mnothingToShowLayout = (RelativeLayout) findViewById(R.id.ID_home_following_seePost_nothingToShowLayout);
    mRecyclerView = (RecyclerView) findViewById(R.id.ID_seePost_recyclerView);
    linearlayoutmanager linearlayoutmanager = new linearlayoutmanager(Group_Announcements_Activity.this);
    linearlayoutmanager.setItemPrefetchEnabled(true);
    linearlayoutmanager.setinitialPrefetchItemCount(15);
    mRecyclerView.setLayoutManager(linearlayoutmanager);

    getGroupAnnouncements();
}

`

    public class AnnouncementAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context mContext;
    List<Post> listofPost;
    LayoutInflater mInflater;

    public static final int withPic = 1;
    public static final int withnopic = 2;
    public static final int withnopic_byHost = 3;
    public static final int withPic_byHost = 4;
    private static final int withVideo = 5;
    private static final int withVideoByHost = 6;

    public AnnouncementAdapter(Context context,List<Post> list) {
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        listofPost = new ArrayList<>(list);
    }

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

        switch (viewType) {

            case withPic:
                View viewWithPic = mInflater.inflate(R.layout.recyclerview_item_post_with_pic,parent,false);
                return new PostWithPicViewHolder(viewWithPic);

            case withnopic:
                View viewWithnopic = mInflater.inflate(R.layout.recyclerview_item_post_with_no_pic,false);
                return new PlainTextViewHolder(viewWithnopic);

            case withPic_byHost:
                View viewWithPicByHost = mInflater.inflate(R.layout.recyclerview_item_post_with_pic_by_host,false);
                return new PostWithPicByHostViewHolder(viewWithPicByHost);

            case withnopic_byHost:
                View viewWithnopicByHost = mInflater.inflate(R.layout.recyclerview_item_post_with_no_pic_by_host,false);
                return new PlainTextByHostViewHolder(viewWithnopicByHost);

        }

        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder,int position) {
        holder.setIsRecyclable(false);
        switch (holder.getItemViewType()) {

            case withPic:
                PostWithPicViewHolder holderWithPic = (PostWithPicViewHolder) holder;
                holderWithPic.onBind(listofPost.get(position));
                break;

            case withnopic:
                PlainTextViewHolder holderWithnopic = (PlainTextViewHolder) holder;
                holderWithnopic.onBind(listofPost.get(position));
                break;

            case withPic_byHost:
                PostWithPicByHostViewHolder holderWithPicByHost = (PostWithPicByHostViewHolder) holder;
                holderWithPicByHost.onBind(listofPost.get(position));
                break;

            case withnopic_byHost:
                PlainTextByHostViewHolder holderWithnopicByHost = (PlainTextByHostViewHolder) holder;
                holderWithnopicByHost.onBind(listofPost.get(position));
                break;

        }

    }

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

    public void removePost(int pos) {
        listofPost.remove(pos);
        notifyItemRemoved(pos);
    }

    @Override
    public int getItemViewType(int position) {
        //if (position % 10 = 0 ) show AD
        String type = listofPost.get(position).getType();

        switch (type) {

            case "withPic":
                return withPic;

            case "withnopic":
                return withnopic;

            case "withnopic_byHost":
                return withnopic_byHost;

            case "withPic_byHost":
                return withPic_byHost;

        }

        return 0;

    }

}


 public class PostWithPicByHostViewHolder extends RecyclerView.ViewHolder {
    //user info
    private String postedBy;
    private ImageView profileImg;
    private TextView profileName;
    //post
    private String postId;
    private TextView postedOn;
    private String postText;
    private TextView postTextView;
    private String postImgPath;
    private ImageView postImg;
    private TextView likedCount;
    private ImageButton commentImg;
    private TextView commentCount;
    private ImageView optionsBtn;
    private long CommentsCount;
    private Options_Group_Announcement_Post_ByHost_Dialog dialog;


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

        postedOn = (TextView) itemView.findViewById(R.id.ID_recyclerView_postWithPic_timeStamp_by_host);

        profileImg = (ImageView) itemView.findViewById(R.id.ID_recyclerView_postWithPic_hostimage_by_host);
        profileImg.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserProfile_Dialog dialog = UserProfile_Dialog.getDialogFragment(mHostId,mHostName,postedBy);
                dialog.show(getSupportFragmentManager(),"");
            }
        });

        profileName = (TextView) itemView.findViewById(R.id.ID_recyclerView_postWithPic_hostName_by_host);
        profileName.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserProfile_Dialog dialog = UserProfile_Dialog.getDialogFragment(mHostId,"");
            }
        });

        postTextView = (TextView) itemView.findViewById(R.id.ID_recyclerView_postWithPic_postText_by_host);
        postImg = (ImageView) itemView.findViewById(R.id.ID_recyclerView_postWithPic_postimage_by_host);

        optionsBtn = (ImageView) itemView.findViewById(R.id.ID_recyclerView_postWitPic_postOptions_by_host);
        optionsBtn.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog.show(getSupportFragmentManager(),"");
            }
        });

        likedCount = (TextView) itemView.findViewById(R.id.ID_recyclerView_postWithPic_likeCount_TextView_by_host);

        commentCount = (TextView) itemView.findViewById(R.id.ID_recyclerView_postWithPic_commentCount_textView_by_host);

        commentImg = (ImageButton) itemView.findViewById(R.id.ID_recyclerView_postWithPic_commentImgView_by_host);
        commentImg.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Group_Announcements_Activity.this,Group_Announcement_Comments_Activity.class);
                intent.putExtra("hostId",mHostId);
                intent.putExtra("hostName",mHostName);
                intent.putExtra("groupId",mGroupId);
                intent.putExtra("postId",postId);
                startActivity(intent);
            }
        });

    }

    public void onBind(Post post) {
        String nameStr = post.getPostProfileName();
        String name = nameStr.substring(0,1).toupperCase() + nameStr.substring(1);
        profileName.setText(name);

        postId = post.getPostId();
        postedOn.setText(post.getPostedDate());
        postedBy = post.getPostedBy();
        postTextView.setText(post.getPostText());
        postImgPath = post.getPostimagePath();
        likedCount.setText(Integer.toString(post.getLikedCount()));

        String postString = post.getPostText();
        if (postString != null) {
            postText = postString;
        } else {
            postText = "";
        }

        getPostLikes(new FireBaseCallback() {
            @Override
            public void callBack(Map map) {
                likedCount.setText(CompactNumberConverter.formatValue((long) map.get("likesCount")));

            }
        });

        getCommentsCount(new FireBaseCallback() {
            @Override
            public void callBack(Map map) {
                commentCount.setText(CompactNumberConverter.formatValue((long) map.get("totalComments")));
            }
        });

        getimages();

        dialog = Options_Group_Announcement_Post_ByHost_Dialog.getDialogFragment(mHostId,mGroupId,postId,postImgPath,postText,"imagePost");
        dialog.setdismissListener(new Dialog_dismiss_Listener() {
            @Override
            public void simpleClose() {

            }

            @Override
            public void updateList_and_Close() {
                mAdapter.removePost(getAdapterPosition());
                Toast.makeText(Group_Announcements_Activity.this,"Post was removed",Toast.LENGTH_LONG).show();
            }

            @Override
            public void updateText_and_Close(String newText) {
                if (newText != null && !newText.isEmpty()) {
                    Toast.makeText(Group_Announcements_Activity.this,"Post Updated",Toast.LENGTH_LONG).show();
                    postTextView.setVisibility(View.VISIBLE);
                    postTextView.setText(newText);
                    reConfigureEditDialog(newText);
                } else {
                    Toast.makeText(Group_Announcements_Activity.this,Toast.LENGTH_LONG).show();
                    postTextView.setVisibility(View.GONE);
                    reConfigureEditDialog(newText);
                }
            }
        });
    }

    private void reConfigureEditDialog(String newText2) {
        dialog = null;
        dialog = Options_Group_Announcement_Post_ByHost_Dialog.getDialogFragment(mHostId,newText2,Toast.LENGTH_LONG).show();
            }

            @Override
            public void updateText_and_Close(String newText) {
                if (!newText.isEmpty()) {
                    Toast.makeText(Group_Announcements_Activity.this,Toast.LENGTH_LONG).show();
                    postTextView.setVisibility(View.GONE);
                    reConfigureEditDialog(newText);
                }

            }

        });
    }

    public void getimages() {
        mFirebaseDatabase.getReference().child("Profile_main_picture").child(postedBy).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    StorageReference imgRef = mFirebaseStorageUserImg.getReference().child((String) dataSnapshot.child("imagePath").getValue());
                    Glide.with(Group_Announcements_Activity.this)
                            .load(imgRef)
                            .apply(new RequestOptions()
                                    .placeholder(R.drawable.ic_hour_glass)
                                    .error(R.drawable.ic_person)
                                    .dontAnimate())
                            .into(profileImg);

                } else {
                    // user has no profile image
                    Glide.with(Group_Announcements_Activity.this)
                            .load(R.drawable.ic_person)
                            .apply(new RequestOptions()
                                    .placeholder(R.drawable.ic_hour_glass)
                                    .error(R.drawable.ic_person)
                                    .dontAnimate())
                            .into(profileImg);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        StorageReference imageRef = mFirebaseStoragePosts.getReference().child(postImgPath);
        Glide.with(Group_Announcements_Activity.this)
                .load(imageRef)
                .apply(new RequestOptions()
                        .dontAnimate()
                        .skipMemoryCache(true)
                        .diskCacheStrategy(diskCacheStrategy.RESOURCE)
                        .placeholder(R.drawable.ic_hour_glass))
                .into(postImg);

    }

    private void getPostLikes(final FireBaseCallback fireBaseCallback) {
        DatabaseReference postLikesRef = mFirebaseDatabase.getReference().child("group_announcement_likes").child(mGroupId).child(postId);
        postLikesRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String likedBy;
                long countLong = dataSnapshot.getChildrenCount();
                Map<String,Object> likesMap = new HashMap<>();
                likesMap.put("likesCount",countLong);
                fireBaseCallback.callBack(likesMap);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }

    private void getCommentsCount(final FireBaseCallback firebaseCallback) {
        DatabaseReference commentsRef = mFirebaseDatabase.getReference().child("group_announcement_comments").child(mGroupId).child(postId);
        commentsRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                CommentsCount = dataSnapshot.getChildrenCount();
                Map<String,Object> commentsMap = new HashMap<>();
                commentsMap.put("totalComments",CommentsCount);
                firebaseCallback.callBack(commentsMap);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }

}

public class PlainTextByHostViewHolder extends RecyclerView.ViewHolder {
    //userInfo
    private String postedBy;
    private ImageView profileImg;
    private TextView profileName;
    //post
    private String postId;
    private TextView postedOn;
    private String postText;
    private TextView postTextView;
    private TextView likedCount;
    private TextView commentCount;
    private ImageButton commentImg;
    private ImageView optionsBtn;
    private long CommentsCount;
    private Options_Group_Announcement_Post_ByHost_Dialog dialog;

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

        postedOn = (TextView) itemView.findViewById(R.id.ID_recyclerView_postWithnopic_timeStamp_by_host);

        profileImg = (ImageView) itemView.findViewById(R.id.ID_recyclerView_postWithnopic_profileImage_by_host);
        profileImg.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserProfile_Dialog dialog = UserProfile_Dialog.getDialogFragment(mHostId,"");

            }
        });

        profileName = (TextView) itemView.findViewById(R.id.ID_recyclerView_postWithnopic_profileName_by_host);
        profileName.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserProfile_Dialog dialog = UserProfile_Dialog.getDialogFragment(mHostId,"");
            }
        });

        postTextView = (TextView) itemView.findViewById(R.id.ID_recyclerView_postWithnopic_postText_by_host);

        optionsBtn = (ImageView) itemView.findViewById(R.id.ID_recyclerView_postWithnopic_postOptions_by_host);
        optionsBtn.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.show(getSupportFragmentManager(),"");
            }
        });

        likedCount = (TextView) itemView.findViewById(R.id.ID_recyclerView_postWithnopic_likeCount_TextView_by_host);
        commentCount = (TextView) itemView.findViewById(R.id.ID_recyclerView_postWithnopic_commentCount_textView_by_host);

        commentImg = (ImageButton) itemView.findViewById(R.id.ID_recyclerView_postWithnopic_commentImgView_by_host);
        commentImg.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Group_Announcements_Activity.this,1).toupperCase() + nameStr.substring(1);
        profileName.setText(name);

        postId = post.getPostId();
        postedOn.setText(post.getPostedDate());
        postedBy = post.getPostedBy();
        postTextView.setText(post.getPostText());
        postTextView.setTextSize(21f);
        likedCount.setText(Integer.toString(post.getLikedCount()));

        String postString = post.getPostText();
        if (postString != null) {
            postText = postString;
        } else {
            postText = "";
        }

        getCommentsCount(new FireBaseCallback() {
            @Override
            public void callBack(Map map) {
                commentCount.setText(CompactNumberConverter.formatValue((long) map.get("totalComments")));
            }
        });

        getPostLikes(new FireBaseCallback() {
            @Override
            public void callBack(Map map) {
                likedCount.setText(CompactNumberConverter.formatValue((long) map.get("likesCount")));
            }
        });

        getimages();

        dialog = Options_Group_Announcement_Post_ByHost_Dialog.getDialogFragment(mHostId,null,"plainText");
        dialog.setdismissListener(new Dialog_dismiss_Listener() {
            @Override
            public void simpleClose() {

            }

            @Override
            public void updateList_and_Close() {
                mAdapter.removePost(getAdapterPosition());
                Toast.makeText(Group_Announcements_Activity.this,Toast.LENGTH_LONG).show();
                    postTextView.setVisibility(View.VISIBLE);
                    postTextView.setText(newText);
                    reConfigureEditDialog(newText);
                }
            }
        });

    }


    private void reConfigureEditDialog(String newText2) {
        dialog = null;
        dialog = Options_Group_Announcement_Post_ByHost_Dialog.getDialogFragment(mHostId,Toast.LENGTH_LONG).show();
                    postTextView.setVisibility(View.VISIBLE);
                    postTextView.setText(newText);
                    reConfigureEditDialog(newText);
                }

            }

        });
    }

    public void getimages() {
        mFirebaseDatabase.getReference().child("Profile_main_picture").child(postedBy).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    StorageReference imgRef = mFirebaseStorageUserImg.getReference().child((String) dataSnapshot.child("imagePath").getValue());
                    Glide.with(Group_Announcements_Activity.this)
                            .load(imgRef)
                            .apply(new RequestOptions()
                                    .placeholder(R.drawable.ic_hour_glass)
                                    .error(R.drawable.ic_person)
                                    .dontAnimate())
                            .into(profileImg);

                } else {
                    // user has no profile image
                    Glide.with(Group_Announcements_Activity.this)
                            .load(R.drawable.ic_person)
                            .apply(new RequestOptions()
                                    .placeholder(R.drawable.ic_hour_glass)
                                    .error(R.drawable.ic_person)
                                    .dontAnimate())
                            .into(profileImg);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });


    }

    private void getPostLikes(final FireBaseCallback fireBaseCallback) {

        DatabaseReference postLikesRef = mFirebaseDatabase.getReference().child("group_announcement_likes").child(mGroupId).child(postId);
        postLikesRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                long countLong = dataSnapshot.getChildrenCount();

                Map<String,countLong);

                fireBaseCallback.callBack(likesMap);

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }


    private void getCommentsCount(final FireBaseCallback firebaseCallback) {

        DatabaseReference commentsRef = mFirebaseDatabase.getReference().child("group_announcement_comments").child(mGroupId).child(postId);
        commentsRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                CommentsCount = dataSnapshot.getChildrenCount();

                Map<String,CommentsCount);
                firebaseCallback.callBack(commentsMap);

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)