删除Gridlayout Recyclerview中的一些项目会留下空白空间

时间:2017-10-12 03:57:22

标签: java android

我正在尝试从我的Recyclerview中的Json Response中删除一些项目,但是当我这样做时,正在创建一个空项目空间来代替已删除的项目。只有当我使用带有recyclerview的gridlayout manager时才会发生这种情况,如果我使用Linearlayout manager,则没有空格。

以下是我的代码:

 activityRecylerviewBinding.recyclerview1.setHasFixedSize(true);
    //activityRecylerviewBinding.recyclerview1.setLayoutManager(new LinearLayoutManager(getActivity()));

    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);
    activityRecylerviewBinding.recyclerview1.setLayoutManager(mLayoutManager);
    activityRecylerviewBinding.recyclerview1.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(5), true));
    activityRecylerviewBinding.recyclerview1.setItemAnimator(new DefaultItemAnimator());

删除项目:

 if (!user.emailId.equals(clickedemail)){
                 viewHolder.setIsRecyclable(false);
                 viewHolder.mAdapterItemChatsBinding.gooot.removeAllViews();
                 viewHolder.mAdapterItemChatsBinding.gooot.setPadding(0, 0, 0, 0);
                 //viewHolder.mAdapterItemChatsBinding.gooot.getLayoutParams().height = 0;
             }else {
                 viewHolder.bindUser(user, RecycFragment.this);
             }

当我使用吹码时,它可以正常工作。

        activityRecylerviewBinding.recyclerview1.setLayoutManager(new LinearLayoutManager(getActivity()));

JsonParsing代码:

 private void initializeFirebase() {

    final Query refUsers = FirebaseDatabase.getInstance()
            .getReference(ConstantsFirebase.FIREBASE_LOCATION_GARMENTS);
    refUsers.keepSynced(true);

    final List<UploadImage> uploadImageList = new ArrayList<>();
    refUsers.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()) {

                UploadImage uploadImage = messageSnapshot.getValue(UploadImage.class);

                uploadImageList.add(uploadImage);

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    mAdapter = new FirebaseRecyclerAdapter<UploadImage, ChatsItemHolder>(UploadImage.class, R.layout.recycitemfirebase,
            ChatsItemHolder.class, refUsers) {
        @Override
        public ChatsItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            adapterItemChatsBinding = DataBindingUtil.inflate(LayoutInflater
                    .from(parent.getContext()), viewType, parent, false);
            //adapterItemChatsBinding.tvUserName.setTypeface(Typefaces.get(getActivity(), Constants.FONT_ROBOT_REGULAR));

            return new ChatsItemHolder(adapterItemChatsBinding);
        }

        @Override
        protected void populateViewHolder(ChatsItemHolder viewHolder, final UploadImage user, int position) {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
            clickedemail = prefs.getString(Constants.KEY_CLICKED_EMAIL, "");

             if (!user.emailId.equals(clickedemail)){
                 viewHolder.setIsRecyclable(false);
                 viewHolder.mAdapterItemChatsBinding.gooot.removeAllViews();
                 viewHolder.mAdapterItemChatsBinding.gooot.setPadding(0, 0, 0, 0);

              //viewHolder.mAdapterItemChatsBinding.gooot.getLayoutParams().height = 0;
             }else {
                 viewHolder.bindUser(user, RecycFragment.this);
             }

          viewHolder.mAdapterItemChatsBinding.title.setText(user.getGarment_name());

            viewHolder.mAdapterItemChatsBinding.count.setText(user.getGarment_color());
            Glide.with(viewHolder.mAdapterItemChatsBinding.thumbnail.getContext())
                    .load(user.getImage())
                    .placeholder(R.drawable.ic_person)
                    .fitCenter()
                    .dontAnimate()
                    .into(viewHolder.mAdapterItemChatsBinding.thumbnail);
        }
    };

    activityRecylerviewBinding.recyclerview1.setAdapter(mAdapter);

}

有人能告诉我我做错了什么吗?

2 个答案:

答案 0 :(得分:0)

您从数据集中删除项目在哪里?

如果没有,删除项目的操作应该包含数据集中的项目索引?如果是这样的话应该有效:

public void removeItem(final int index) {
    list.remove(index);
    adapter.notifyItemRemoved(index);
}

答案 1 :(得分:0)

似乎,用于recyclerview的适配器是使用firebase查询作为项目的源构建的。

您需要更改Firebase查询,以仅获取要包含在列表中的项目。