我正在scrollview中实施recyclerview
<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>
将recyclelerview设置为固定高度,如此
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);
由于持有者高度将固定为64dp我已将adapterItemSize = 64,但我面临的问题是列表中只有两行可见。
答案 0 :(得分:0)
我认为你没有setLayoutParams。当您更改视图的布局参数时,您需要设置它,例如,这是您为recyclerview设置它的方式:
LinearLayout.LayoutParams layoutParams = mRecyclerView_other.getLayoutParams();
layoutParams.width = 64;
layoutParams.height = 64;
mRecyclerView_other.setLayoutParams(layoutParams);
您还使用整数值设置宽度和高度,而不是从dimens.xml中提取它们 - 试试这个:
在dimens.xml文件中:
<dimen name="test">64dp</dimen>
然后像这样提取int值:
int valueInPixels = (int) getResources().getDimension(R.dimen.test)