RecyclerView不显示
我有一个RecyclerView不显示任何项目。最初它是空的,但后来我添加项目并调用notifyDatasetChanged()。 getItemCount()被调用并返回25,onBindViewHolder被调用3次并正确设置视图。RecyclerView不显示
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>
我一直在戳了几个小时的代码,我不知道为什么它不工作
我是一个白痴。
<?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
必须隐藏它。
我爱你。我在这上面花了一个小时。我甚至最终让自己喝了一杯茶,哈哈。 – 2015-12-11 05:53:04
LinearLayout的默认方向是水平的。很容易相信布局是垂直的,添加多个孩子,并想知道为什么只有第一个孩子可见(当后面的孩子在屏幕右侧)。这个lint规则通过在使用隐式方向和多个子项时使用LinearLayout进行警告来帮助查明这个问题。 它还检查没有方向属性的空LinearLayouts,该属性还定义了id属性。这捕获了儿童将动态添加到LinearLayout的场景。 – 2017-08-24 09:09:03
a)您必须向我们展示您的代码; b)在RecyclerView上调用'notifyDatasetChanged()'效率不高,因此不够酷 – reVerse 2014-11-21 16:52:36
当我接收到数据时调用适配器的notifyDataSetChanged。 – DariusL 2014-11-21 16:53:57
这基本上就是我所说的 - 引用[Docs](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#notifyDataSetChanged%28%29):'如果你是编写一个适配器,如果可以的话,使用更具体的更改事件总是更有效率。依赖于notifyDataSetChanged()作为最后的手段.' – reVerse 2014-11-21 16:56:06