RecyclerView not displaying

北慕城南 提交于 2019-12-21 04:11:20

问题


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();
}

The gist of it. The service holds a list of albums and notifies the adapter of changes by calling dataUpdated() and endReached().

More code:

@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>

Even more code:

The activity 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>

List item layout:

<?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>

List layout:

<?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


回答1:


I am an idiot.

<?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>

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.



来源:https://stackoverflow.com/questions/27066340/recyclerview-not-displaying

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!