gpt4 book ai didi

android - 带有 SortedList 的嵌套 RecyclerView 不显示任何内容

转载 作者:行者123 更新时间:2023-11-29 19:38:22 26 4
gpt4 key购买 nike

我有一个 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);
}

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

最佳答案

您需要删除 setHasFixedSize(true) 调用(默认为 false)。如果这是真的,recyclerview 认为当您通知他时它的大小不会改变,但这里不是这种情况;)

希望对你有帮助

关于android - 带有 SortedList 的嵌套 RecyclerView 不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39001432/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com