记录一下: https://blog.csdn.net/qq_26075861/article/details/86685293 在RecyclerView外层套上一层RelativeLayout即可
https://blog.csdn.net/Moonlighting_/article/details/52385267 @Override protected void onMeasure(int widthSpec, int heightSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthSpec, expandSpec); }
https://blog.csdn.net/the_spring/article/details/54236996 1.getItemCount()返回值<=0 ; 2.设置LayoutManager ; 3.被ScrollView嵌套 ok重点说一下第三点 recyclerview 25可以被scrollview嵌套,可以正常显示 recyclerview 23被scrollview嵌套时,无法正常显示
getItemCount()返回值<=0 ;肯定是对的。其他几个没有验证。 我再来补充一种情况: 由于我们经常会使用开源的第三方的RecycleView,并且我们可能还会把他的代码抄下来: 于是:
@Override
public int getItemViewType(int position) {
if (isRefreshHeader(position)) {
return TYPE_REFRESH_HEADER;
}
if (isLoadMoreFooter(position)) {
return TYPE_LOAD_MORE_FOOTER;
}
int adjPosition = position - 1;
int adapterCount;
if (adapter != null) {
adapterCount = adapter.getItemCount();
if (adjPosition < adapterCount) {
int type = adapter.getItemViewType(adjPosition);
if (isReservedItemViewType(type)) {
throw new IllegalStateException("PullToRefreshRecyclerView require itemViewType in adapter should be less than 10000 ");
}
return type;
}
}
return 0;
}
比如PullRecyclerView的代码中,你的Adpater是被他wrap了一层的。 由于他默认认为有header,所以position固定-1。 这个问题,如果你遇到了类似的问题也可以往别人的源码中查一下。
https://my.oschina.net/caomenglong/blog/747197 https://github.com/amardeshbd/android-recycler-view-wrap-content
<android.support.v7.widget.RecyclerView android:id="@+id/my_recycler_view" android:layout_width=“match_parent” android:layout_height=“wrap_content” tools:listitem="@layout/row_list_item">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(getContext());
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);
// Disabled nested scrolling since Parent scrollview will scroll the content.
mRecyclerView.setNestedScrollingEnabled(false);
// specify an adapter (see also next example)
mAdapter = new SimpleListAdapter(DataSetProvider.generateDataset());
mRecyclerView.setAdapter(mAdapter);
|