嵌套的RecyclerView与SortedList没有任何关系

时间:2016-08-17 16:05:14

标签: android android-recyclerview recycler-adapter sortedlist

我在RecyclerView中嵌套了NestedScrollView,并且适配器使用SortedList来保存内容。

这是我的布局:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.NestedScrollView
        marginTop="@{StatusbarUtils.getTopMargin(context)}"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

            <!-- some more ui elements -->

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_settings_cameras"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

            <!-- some more ui elements -->

    </android.support.v4.widget.NestedScrollView>

</FrameLayout>

我的适配器:

public class SettingsRecyclerViewAdapter extends RecyclerView.Adapter<SettingsRecyclerViewAdapter.ViewHolder> {

    @NonNull private final SortedList<Camera> cameras;

    public SettingsRecyclerViewAdapter(@NonNull final OnSettingsInteractionListener listener) {
        cameras = new SortedList<>(Camera.class, new CameraSortedListCallback(this));
    }

    public void addCamera(@NonNull final Camera camera) {
        cameras.add(camera);
    }

    private static class CameraSortedListCallback extends SortedListAdapterCallback<Camera> {

        private final RecyclerView.Adapter adapter;

        CameraSortedListCallback(final RecyclerView.Adapter adapter) {
            super(adapter);
            this.adapter = adapter;
        }

        @Override
        public int compare(final Camera o1, final Camera o2) {
            return 0;
        }

        @Override
        public boolean areContentsTheSame(final Camera oldItem, final Camera newItem) {
            return false;
        }

        @Override
        public boolean areItemsTheSame(final Camera item1, final Camera item2) {
            return false;
        }
    }
}

我是如何使用它的:

    final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    linearLayoutManager.setAutoMeasureEnabled(true);

    adapter = new SettingsRecyclerViewAdapter(this);

    recyclerView.setLayoutManager(linearLayoutManager);
    recyclerView.setHasFixedSize(true);
    recyclerView.setNestedScrollingEnabled(false);
    recyclerView.setAdapter(adapter);

当我使用它并致电addCamera(...)时,没有任何反应: - (

SortedListAdapterCallback有一个onInserted方法,可以调用mAdapter.notifyItemRangeInserted(position, count)。当我覆盖它并添加adapter.notifyDataSetChanged()时,RecyclerView会显示项目。

    @Override
    public void onInserted(int position, int count) {
        Timber.i("onInserted() called with: " + "position = " + position + ", count = " + count);
        adapter.notifyDataSetChanged();
        super.onInserted(position, count);
    }

这是一个错误还是我做错了什么?

1 个答案:

答案 0 :(得分:2)

您需要删除setHasFixedSize(true)调用(默认为false)。如果这是真的,那么Recyclerview认为当你通知他时它的大小没有变化,但这不是这里的情况;)

希望这有帮助