问题
I am using a Recycler view
that hold a Grid Layout
to present the items in a 2 by 2 way.
The problem is that the first two items are not stretching accordingly with the content inside. To easy the explanation here is one example of my problem:
Regarding the code, in my item.xml
I have
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/dialog_background_color"
app:cardCornerRadius="10dp"
app:cardElevation="3dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/background_light">
<TextView
style="@style/SubtitleStyle"
android:id="@+id/vExpenseType"
android:textSize="15sp"
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
app:layout_constraintTop_toTopOf="@+id/vExpenseImage"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="@+id/vExpenseImage"
app:layout_constraintStart_toEndOf="@+id/vExpenseImage"
android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginBottom="8dp"/>
<ImageView
android:id="@+id/vExpenseImage"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/ic_debt"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
This is my main.xml
that hold the recycler view
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CC000000"
tools:context=".ui.flows.main.fragments.overview.OverviewMainInputExpenseDialog">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="@dimen/dialog_margin"
android:layout_marginStart="@dimen/dialog_margin"
android:background="@drawable/dialog_background"
android:transitionName="@string/transition_dialog"
android:orientation="horizontal">
<Button
android:id="@+id/close"
style="@style/ButtonLabelStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:background="@drawable/btn_background"
android:text="Choose"
android:textColor="#fff"
android:textAppearance="?android:textAppearanceMedium"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="32dp"
app:layout_constraintTop_toBottomOf="@+id/vMovementTypeRv"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/vMovementTypeRv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
tools:spanCount="2"
tools:listitem="@layout/item_expense_type"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
Then in my respective class
I have :
vMovementTypeRv.layoutManager = GridLayoutManager(applicationContext,2)
Final conclusions
I tried to change the layout but I am not able to make this work. Can someone show me some input on this?
回答1:
You can use something like this for showing 2 by 2 items. And you need to show the RecyclerView
for items showing and delete the spanCount
.
vMovementTypeRv.setLayoutManager(new GridLayoutManager(mContext, 4));
vMovementTypeRv.setHasFixedSize(true);
来源:https://stackoverflow.com/questions/56411226/grid-layout-in-recycler-view-is-cutting-of-the-first-two-items