在一个片段中,我在 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));
}
The problem : -
RecyclerView
的 layout_height
设置为 wrap_content
. 所以在开始时,当RecyclerView上没有设置适配器时,它需要高度为0dp . 当我在AsyncTask结束后设置适配器时,它仍然保持在0dp并且看不到任何内容 . 这对 ListView
来说永远不是问题 .
Note : -
在我获得此列表后,我计划将其转换为非滚动的RecyclerView,以便在 scrollView
和 swipeLayout
内部使用,通过扩展 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();
}
}
1 回答
原来
ScrollView
里面的RecyclerView
是个问题 .我删除了ScrollView,并通过覆盖
getItemViewType(int position)
将RecyclerView外部的内容作为另一种视图类型放入RecyclerView中 .这不仅解决了问题,还摆脱了对非滚动RecyclerView的需求 . 非滚动RecyclerView不会回收视图,它的目的将被打败 .