android - 设置适配器后 RecyclerView 的大小不会改变

标签 android android-recyclerview

在 fragment 中,我在 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));
}

问题:-

RecyclerViewlayout_height 设置为wrap_content。所以一开始,当 RecyclerView 上没有设置适配器时,它的高度为 0dp。当我在 AsyncTask 结束后设置适配器时,它仍然保持在 0dp,什么也看不到。对于 ListView,这从来都不是问题。

注意:-

在我得到这个列表后,我计划通过扩展 RecyclerView< 将它转换为非滚动 RecyclerView 以在 scrollViewswipeLayout 中使用 并像这样覆盖 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();
}
}

最佳答案

事实证明,ScrollView 中的 RecyclerView 是个问题。

我删除了 ScrollView,并通过覆盖 getItemViewType(int position) 将 RecyclerView 之外的内容放入 RecyclerView 作为另一种 View 类型。

这不仅解决了问题,还摆脱了对非滚动 RecyclerView 的需要。非滚动的 RecyclerView 不会回收 View ,它的目的将被打败。

关于android - 设置适配器后 RecyclerView 的大小不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29742332/

相关文章:

java - 键盘无法抬起编辑文本,因为scrollview

安卓 : To shift the common code of all activities to a base class. 。应用程序崩溃

android - 为什么 DBFlow 馈送的 RecyclerView 在 Marshmallow 中得到更新并且在 M 之前没有这样做?

android - addOnScrollListener 的 RecyclerView onScrolled 方法只调用一次

android - 单元测试 AndroidViewModel 类

安卓udp丢包

android - 使用代码启用/禁用移动数据 (GPRS)

android - 扩展 CardView 的最后一项

java - 从 ormlite 数据库表中选择不重复的内容

android - fragment 上的更改列表正在其他 fragment 中更改