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>
16,은 지금 몇 시간에 대한 코드를 파고있다 그리고 나는 내가 바보입니다
a) 코드를 표시해야합니다. b) RecyclerView에서'notifyDatasetChanged()'를 호출하는 것이 효율적이지 않으므로 멋지지 않습니다. – reVerse
데이터를 받으면 어댑터의 notifyDataSetChanged가 호출됩니다. – DariusL
그건 기본적으로 내가 뭘 - 인용 [Docs] (https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#notifyDataSetChanged%28%29) :'만약 당신이 가능하면 어댑터를 작성하여보다 구체적인 변경 이벤트를 사용하는 것이 더 효율적입니다. 최후의 수단으로 notifyDataSetChanged()를 의지하십시오. ' – reVerse