gojic
gojic

Reputation: 363

How to prevent RecyclerView adding empty space in rows

I have CheckBox and when is it clicked ,my text view should be placed on the left side of RecyclerView aka first row, and if it left unchecked it should be placed on the right side of RecyclerView aka second row. I tried with GridLayoutManager but nothing happened. Basically I am trying to get something like this:enter image description here

and this is what I got: enter image description here I got empty space on left side and number 25 is not on top, basically getting space on the right side also

my code:`

if (modelClass.isContingent()) {
                binding.benefiiaryContigentTV.setText(modelClass.getPercentage());
               // binding.benefiiaryPercentageTV.setVisibility(View.GONE);
            } else {

                binding.benefiiaryPercentageTV.setText(modelClass.getPercentage());
            }

my LIneaarLayoutManager:

  // Create layout manager with initial prefetch item count
        LinearLayoutManager layoutManager = new LinearLayoutManager(
                binding.rvNonregisteredList.getContext(),
                LinearLayoutManager.VERTICAL,
                true
        );
        layoutManager.setInitialPrefetchItemCount(modelClass.getNonRegBeneficiarisList().size());
        // Create sub item view adapter
        NonRegBeneficiarisAdapter subItemAdapter = new NonRegBeneficiarisAdapter(modelClass.getNonRegBeneficiarisList(), context);

        binding.rvNonregisteredList.setLayoutManager(layoutManager);
        binding.rvNonregisteredList.setAdapter(subItemAdapter);
        binding.rvNonregisteredList.setRecycledViewPool(viewPool);

` and my fragment:

private void buildSubItemList(List<NonRegBeneficiaris> subItemList) {
        NonRegBeneficiaris subItem = new NonRegBeneficiaris();
        subItemList.add(subItem);
}

and here i am calling this method:

for (int i = 0; i<assetsModel.getNonRegisteredModelList().size()-1;i++){
                buildSubItemList(assetsModel.getNonRegisteredModelList().get(i).getNonRegBeneficiarisList());
            }

my xml:

`<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/bela">

        <TextView
            android:id="@+id/benefiiary_percentage_TV"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textColor="#8fa2b0"
            android:textSize="10sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/vertical_view3"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <View
            android:id="@+id/vertical_view3"
            android:layout_width="1dp"
            android:layout_height="12dp"
            android:layout_marginEnd="6dp"
            android:background="@color/background"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/benefiiary_contigent_TV"
            app:layout_constraintStart_toEndOf="@+id/benefiiary_percentage_TV"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/benefiiary_contigent_TV"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textColor="#8fa2b0"
            android:textSize="10sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/vertical_view3"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>`

Upvotes: 1

Views: 599

Answers (2)

MehranB
MehranB

Reputation: 1525

here is an idea:

use a cardview layout like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/panelLeft"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">


        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        ...

    </LinearLayout>

    <LinearLayout
        android:id="@+id/panelRight"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        ...

    </LinearLayout>
    
</LinearLayout>

inflate it in your RecyclerView adapter onCreateViewHolder method and whenever a checkBox is unchecked, you can remove the clicked checkbox view from panelLeft and add it to panelRight and vice versa.

final result before and after clicking some of the CheckBoxes:

before: enter image description here

After: enter image description here

Upvotes: 1

ismail alaoui
ismail alaoui

Reputation: 6073

If you want to show items as wanted , you better use two RecylcerView , because while using one the view will always be considered as a row , so you will never avoid space since you are just changing the gravity .

So then anytime you check the box your remove/add item to the list and then call notifyDataSetChanged() on your two adapter

Edit

enter image description here

Upvotes: 0

Related Questions