nayana bhoj
nayana bhoj

Reputation: 115

Use RecyclerView inside ScrollView with fixed Recycler item height

i am implementing recyclerview inside scrollview

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusableInTouchMode="true"
            android:orientation="vertical">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:divider="@android:color/transparent"
                android:dividerHeight="0dp"
                android:listSelector="@android:color/transparent" />
        </LinearLayout>

set recyclerview to fixed height like this

    mRecyclerView_other.setHasFixedSize(true);
    RecyclerView.LayoutManager layoutManager_other = new LinearLayoutManager(context);
    mRecyclerView_other.setLayoutManager(layoutManager_other);
    int adapterItemSize = 64;
    int viewHeight = adapterItemSize * list.size();
    mRecyclerView_other.getLayoutParams().height = viewHeight;
    mRecyclerView_other.setAdapter(adapter_other);

as holder height will be fixed to 64dp i have put adapterItemSize = 64, but the issue i am facing is only two rows from the list are visible.

Upvotes: 1

Views: 1585

Answers (1)

Simon
Simon

Reputation: 19938

I think you have not setLayoutParams. When you change the layout params of a view, you need to set it, eg, this is how you set it for your recyclerview:

 LinearLayout.LayoutParams layoutParams  = mRecyclerView_other.getLayoutParams();
layoutParams.width = 64;
layoutParams.height = 64;
 mRecyclerView_other.setLayoutParams(layoutParams);

You are also setting your width and height using integer values instead of pulling them from the dimens.xml - try this:

In your dimens.xml file:

<dimen name="test">64dp</dimen>

Then extract the int value like this:

int valueInPixels = (int) getResources().getDimension(R.dimen.test)

Upvotes: 2

Related Questions