【问题标题】:RecyclerView not displayingRecyclerView 不显示
【发布时间】:2014-11-21 16:49:25
【问题描述】:

我有一个不显示任何项目的 RecyclerView。最初它是空的,但后来我添加了项目并调用了 notifyDatasetChanged()。 getItemCount() 被调用并返回 25,onBindViewHolder 被调用 3 次并正确设置视图。

public AlbumListAdapter(FacebookActivity activity, long pageId, OnItemClickListener listener){
    this.listener = listener;
    this.inflater = LayoutInflater.from(activity);
    this.service = new PagedGraphService<Album>(String.format("https://graph.facebook.com/%d/albums", pageId),
            new PagedGraphService.ServiceUpdateListener() {
        @Override
        public void dataUpdated() {
            notifyDataSetChanged();
        }

        @Override
        public void endReached() {

        }
    },
    new TypeToken<PagedGraphService.GraphResponse<Album>>(){}.getType());
}

@Override
public AlbumHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    return new AlbumHolder(inflater.inflate(R.layout.photo_album_list_card, viewGroup, false));
}

@Override
public void onBindViewHolder(AlbumHolder viewHolder, int i) {
    viewHolder.setAlbum(service.get(i));
}

@Override
public int getItemCount() {
    return service.getLoadedCount();
}

它的要点。该服务保存一个专辑列表,并通过调用 dataUpdated() 和 endReached() 通知适配器更改。

更多代码:

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    RecyclerView view = (RecyclerView) inflater.inflate(R.layout.recycler, container, false);
    view.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
    view.setHasFixedSize(false);
    AlbumListAdapter adapter = new AlbumListAdapter(activity, FacebookActivity.PSM_PAGE_ID, this);
    view.setAdapter(adapter);
    return view;
}

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v7.widget.RecyclerView>

更多代码:

活动布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

<include layout="@layout/toolbar"/>

<FrameLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>

列表项布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/album_list_title"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</android.support.v7.widget.CardView>

列表布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

</android.support.v7.widget.RecyclerView>

我已经戳代码几个小时了,我不知道为什么它不起作用

【问题讨论】:

  • a) 您必须向我们展示您的代码; b) 在 RecyclerView 上调用 notifyDatasetChanged() 效率不高,因此并不酷
  • 接收数据时调用适配器的notifyDataSetChanged。
  • 这基本上就是我所说的-引用DocsIf you are writing an adapter it will always be more efficient to use the more specific change events if you can. Rely on notifyDataSetChanged() as a last resort.
  • 但它应该仍然可以工作,但没有绘制任何项目
  • 尝试从开发选项设置布局边界,也许视图在那里但其他的东西是错误的。还要检查 RV 的大小,可能是 0。我看不出它没有显示的任何原因。您可以发布更多关于您的布局的代码,这可能会有所帮助。

标签: android android-recyclerview


【解决方案1】:

我是个白痴。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

<include layout="@layout/toolbar"/>

<FrameLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>

LinearLayout 的默认方向是水平的,工具栏的宽度是match_parent,所以我的内容不在屏幕上。这可能是 android 上最常见的错误之一,这就是您收到警告的原因,但 include 一定隐藏了它。

【讨论】:

  • 我爱你。我为此花了一个小时。我什至最后给自己泡了杯茶,哈哈。
  • LinearLayout 的默认方向是水平的。很容易相信布局是垂直的,向其中添加多个孩子,并想知道为什么只有第一个孩子是可见的(当随后的孩子不在屏幕右侧时)。此 lint 规则通过在 LinearLayout 与隐式方向和多个子项一起使用时发出警告来帮助查明此问题。它还检查没有定义 id 属性的方向属性的空线性布局。这捕获了子元素将被动态添加到 LinearLayout 的场景。
  • 天哪,我简直不敢相信。我今天也在这个境界上犯了错误。你是英雄
猜你喜欢
  • 2018-06-03
  • 1970-01-01
  • 2021-03-10
  • 2019-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多