【问题标题】:RecyclerView Strange scrolling when dataset changed数据集更改时RecyclerView奇怪的滚动
【发布时间】:2015-04-06 10:22:13
【问题描述】:

我正在使用 recyclerView (TwowayView),当数据发生变化时,它的行为很奇怪(在滚动中)。我正在使用以下代码为其设置适配器(每次数据更改时)。

// sites = list , rvlinks = twowayview

    adapter = new SitesAdapter(getActivity(), rvLinks, LinksFragment.this, sites);
    rvLinks.setAdapter(adapter);

我制作了错误视频以便更好地理解。

https://youtu.be/xPvHper3gpc

如所见,recyclerview 的顶部和底部区域很大(见图),尽管列表中只有 2 个项目。

仅使用notifyDatasetChanged() 会产生相同的结果。卡在这了。

Recyclerview 设置了固定大小。

rvLinks.setHasFixedSize(true);

任何帮助都会很棒。

这是我的 xml 布局:

 <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/dummyview"
    android:layout_below="@+id/sp_category"
    android:layout_marginTop="15dp"
    android:background="@drawable/white_round">

    <Widget.EmptyRecyclerView
    android:id="@+id/rv_links"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:drawSelectorOnTop="false"

    android:orientation="vertical"
    app:twowayview_layoutManager="GridLayoutManager"
    app:twowayview_numColumns="2"

    tools:context=".MainActivity" />



    <TextView
        android:id="@+id/tv_empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="No Sites Found" />

</RelativeLayout>

这是我的 RecyclerView 和 emptyView 的实现。

public class EmptyRecyclerView extends TwoWayView {
    private View emptyView;
    final private AdapterDataObserver observer = new AdapterDataObserver() {
        @Override
        public void onChanged() {
            checkIfEmpty();
        }

        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            checkIfEmpty();
        }

        @Override
        public void onItemRangeRemoved(int positionStart, int itemCount) {
            checkIfEmpty();
        }
    };

    public EmptyRecyclerView(Context context) {
        super(context);
    }

    public EmptyRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public EmptyRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    void checkIfEmpty() {
        if (emptyView != null && getAdapter() != null) {
            final boolean emptyViewVisible = getAdapter().getItemCount() == 0;
            emptyView.setVisibility(emptyViewVisible ? VISIBLE : GONE);
            setVisibility(emptyViewVisible ? GONE : VISIBLE);
        }
    }

    @Override
    public void setAdapter(Adapter adapter) {
        final Adapter oldAdapter = getAdapter();
        if (oldAdapter != null) {
            oldAdapter.unregisterAdapterDataObserver(observer);
        }
        super.setAdapter(adapter);
        if (adapter != null) {
            adapter.registerAdapterDataObserver(observer);
        }

        checkIfEmpty();
    }

    public void setEmptyView(View emptyView) {
        this.emptyView = emptyView;
        checkIfEmpty();
    }
}

【问题讨论】:

  • 您是否为 recyclerview 指定了任何尺寸?有一个错误,您不能使用“wrap_content”为 recyclerview 根据其内容code.google.com/p/android/issues/detail?id=74772 调整视图大小。所以只要假设你指定的高度可能是 match_parent 或一些固定的 dp 值
  • 我已将我的 xml 添加到问题中。见已编辑。
  • 不要将 EmptyRecyclerView 高度设置为 match_parent,而是首先尝试将 dp 值设置为接近预期高度
  • 我尝试了 300 dp 但结果相同。可能是早期数据的顶部空白区域,你怎么看?
  • 您设备的屏幕分辨率是多少?

标签: android scroll android-recyclerview


【解决方案1】:

尝试为您的 recyclerview 设置一个确切的高度,因为它不尊重 wrap_content 并且设置 match_parent 将占用您的列表视图下方整个空间的高度

一旦您开始看到结果,您就可以为您的 recyclerview 使用自定义布局管理器,覆盖 onMeasure()。

查看示例

Nested Recycler view height doesn't wrap its content

【讨论】:

  • 我将其更改为 recyclerview,它按预期工作。谢谢。 :)
【解决方案2】:

正如@random 所建议的,我尝试了 match_parent 但与TwoWayView 相同的问题。所以我决定在support-v7 lib中把它改成RecyclerView

令人惊讶的是,它按预期工作。可能是它在自己的GridLayoutManager 中做一些花哨的工作导致了问题。 GridlayoutManager 的 recyclerview 效果很好。

感谢您的帮助。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多