Skip to content Skip to sidebar Skip to footer

Recyclerview Adapter Show Only First Item

I know that this question has been asked many times, but the answers doesn't work for me. I am trying to use Horizontal placement. Here is my code: public class CalendarDaysAdapter

Solution 1:

Change your fragment_calendar_days_item height and width to android:layout_height="wrap_content" and android:layout_width="wrap_content"

it will solve your issue

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="wrap_content"android:layout_height="wrap_content"><RelativeLayoutandroid:layout_width="wrap_content"android:layout_height="50dp"android:gravity="center"><TextViewandroid:id="@+id/daystxt"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="MON"android:textSize="20sp"/></RelativeLayout></LinearLayout>

Solution 2:

Try this for your

fragment_calendar_days_item xml:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="wrap_content"><RelativeLayoutandroid:layout_width="wrap_content"android:layout_height="50dp"android:gravity="center"><TextViewandroid:id="@+id/daystxt"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="MON"android:textSize="20sp"/></RelativeLayout></LinearLayout>

Solution 3:

I think what you want to do is show that "n" elements and the rest must be scrollable.

The solution of setting the hard value is not the best. it's not generic. I think that my solution will fit very well in any case. ;-)

For that I propose you a solution

First determine and create a MAX_ITEMS variable

Kotlin

recycler.addOnLayoutChangeListener { v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom ->
    if (recycler.layoutManager.itemCount > maxItemVisible) {
        valheight= (0 until maxItemVisible).sumBy { recycler.layoutManager.findViewByPosition(it).height }
        recycler.layoutParams = LayoutParams(recycler.width, height)
    }
}

Java

mRecycler.addOnLayoutChangeListener(newView.OnLayoutChangeListener() {
    @OverridepublicvoidonLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
        if (mRecycler.getLayoutManager().getItemCount() > maxItemVisible) {
            intheight=0;
            for (inti=0; i < maxItemVisible; i++){
                height += mRecycler.getLayoutManager().findViewByPosition(i).getHeight()
            }
            mRecycler.setLayoutParams(newLinearLayout.LayoutParams(mRecycler.getWidth(), height));
        }
    }
});

Hoping to have answered your need

Post a Comment for "Recyclerview Adapter Show Only First Item"