1
我有一個嵌套在NestedScrollView中的RecyclerView,並且適配器使用SortedList來保存內容。與SortedList嵌套的RecyclerView不顯示
這是我的佈局:
<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);
}
這是一個錯誤還是我做錯了什麼?