我有一個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>
我一直在戳了幾個小時的代碼,我不知道爲什麼它不工作
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