Possiedo una visualizzazione RecyclerView che non visualizza alcun elemento. Inizialmente è vuoto, ma successivamente aggiungo elementi e richiama notifyDatasetChanged(). getItemCount() viene chiamato e restituisce 25, onBindViewHolder viene chiamato 3 volte e imposta correttamente la vista.RecyclerView not displaying
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();
}
L'essenza di esso. Il servizio contiene un elenco di album e notifica l'adattatore delle modifiche chiamando dataUpdated() e endReached().
Più codice:
@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>
Ancora più code:
L'attività di layout
<?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>
Lista layout di voce:
<?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>
layout della lista:
<?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>
Sono stato frugando il codice per un paio d'ore la società e non ho idea del perché non funziona
a) Devi mostrarci il tuo codice; b) Chiamare 'notifyDatasetChanged()' su RecyclerView non è efficiente e quindi non è bello – reVerse
L'adapter notifyDataSetChanged viene chiamato quando ricevo i dati. – DariusL
Fondamentalmente quello che sto dicendo - Citando [Docs] (https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#notifyDataSetChanged%28%29): 'Se sei scrivendo un adattatore sarà sempre più efficiente usare gli eventi di cambiamento più specifici, se possibile. Affidati a notifyDataSetChanged() come ultima risorsa. – reVerse