设置适配器后 RecyclerView 的大小不会改变
RecyclerView's size doesn't change after setting adapter
在片段中,我在 onActivityCreated
中的 RecyclerView
上设置了布局管理器:-
galleryRecView.setLayoutManager(new LinearLayoutManager(getActivity()));
然后我执行一个AsyncTask
,下载要在RecyclerView中设置的数据。
现在我在我的 RecyclerView 上设置适配器:-
public void onPhotosURLsReceived(boolean success) {
if (success && isAdded())
galleryRecView.setAdapter(new GalleryAdapter(getActivity(), Constants.photosList));
}
问题:-
RecyclerView
的 layout_height
设置为 wrap_content
。所以一开始,当 RecyclerView 上没有设置适配器时,它的高度为 0dp。当我在 AsyncTask 结束后设置适配器时,它仍然保持在 0dp,什么也看不到。 ListView
.
从来都不是问题
注:-
在我得到这个列表后,我计划通过扩展 RecyclerView
和覆盖 onMeasure
像这样:-
public class NonScrollRecyclerView extends RecyclerView {
public NonScrollRecyclerView(Context context) {
super(context);
}
public NonScrollRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/*
* Non-scroll listview to use with SwipeRefreshLayout and scrollview
*/
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}
原来 RecyclerView
在 ScrollView
里面是个问题。
我通过重写 getItemViewType(int position)
.
删除了 ScrollView 并将 RecyclerView 之外的内容放入 RecyclerView 作为另一种视图类型
这不仅解决了问题,还摆脱了对非滚动 RecyclerView 的需要。非滚动的 RecyclerView 不会回收视图,它的目的将被打败。
在片段中,我在 onActivityCreated
中的 RecyclerView
上设置了布局管理器:-
galleryRecView.setLayoutManager(new LinearLayoutManager(getActivity()));
然后我执行一个AsyncTask
,下载要在RecyclerView中设置的数据。
现在我在我的 RecyclerView 上设置适配器:-
public void onPhotosURLsReceived(boolean success) {
if (success && isAdded())
galleryRecView.setAdapter(new GalleryAdapter(getActivity(), Constants.photosList));
}
问题:-
RecyclerView
的 layout_height
设置为 wrap_content
。所以一开始,当 RecyclerView 上没有设置适配器时,它的高度为 0dp。当我在 AsyncTask 结束后设置适配器时,它仍然保持在 0dp,什么也看不到。 ListView
.
注:-
在我得到这个列表后,我计划通过扩展 RecyclerView
和覆盖 onMeasure
像这样:-
public class NonScrollRecyclerView extends RecyclerView {
public NonScrollRecyclerView(Context context) {
super(context);
}
public NonScrollRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/*
* Non-scroll listview to use with SwipeRefreshLayout and scrollview
*/
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}
原来 RecyclerView
在 ScrollView
里面是个问题。
我通过重写 getItemViewType(int position)
.
这不仅解决了问题,还摆脱了对非滚动 RecyclerView 的需要。非滚动的 RecyclerView 不会回收视图,它的目的将被打败。