问题描述
我有一个RecyclerView,它不显示任何项目.最初它是空的,但是后来我添加了项并调用notifyDatasetChanged(). getItemCount()被调用并返回25,onBindViewHolder被调用3次并正确设置视图.
I have a RecyclerView that's not displaying any items. Initially it's empty, but later I add items and call notifyDatasetChanged(). getItemCount() gets called and returns 25, onBindViewHolder gets called 3 times and sets up the view correctly.
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()通知适配器更改.
The gist of it. The service holds a list of albums and notifies the adapter of changes by calling dataUpdated() and 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>
我已经把代码弄了几个小时,我不知道为什么它不起作用
I've been poking the code for a few hours now and I have no idea why it doesn't work
推荐答案
我是个白痴.
<?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
必须将其隐藏.
The default orientation for LinearLayout
is horizontal, and the toolbar's width is match_parent
, so my content was off screen. This is probably one of the most popular mistakes on android, which is why you get a warning, but the include
must have hidden it.
这篇关于RecyclerView不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!