美文网首页
React Native 调用原生RecyclerView 无法

React Native 调用原生RecyclerView 无法

作者: liy_lmn | 来源:发表于2019-11-28 15:24 被阅读0次
  • React Native 调用原生RecyclerView 无法刷新,需要手动下拉或者上拉一下才能成功刷新,要解决这个问题,需要我们自定义RecyclerView,代码如下:
package com.netease.nim.uikit.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

public class RnRecyclerView extends RecyclerView {
    private boolean mRequestedLayout = false;

    public RnRecyclerView(@NonNull Context context) {
        super(context);
    }

    public RnRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public RnRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }


    @Override
    public void requestLayout() {
        super.requestLayout();
        // We need to intercept this method because if we don't our children will never update
        // Check https://stackoverflow.com/questions/49371866/recyclerview-wont-update-child-until-i-scroll
        mRequestedLayout = true;
        this.post(new Runnable() {
            @SuppressLint("WrongCall")
            @Override
            public void run() {
                mRequestedLayout = false;
                layout(getLeft(), getTop(), getRight(), getBottom());
                onLayout(false, getLeft(), getTop(), getRight(), getBottom());
            }
        });
    }

}
  • 注意:布局中也需要替换一下引用的RecyclerView,如
<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

修改成

<自己包名.RecyclerView(自己定义的名字)
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
  • 好了,就分享到这,谢谢大家观看!

相关文章

网友评论

      本文标题:React Native 调用原生RecyclerView 无法

      本文链接:https://www.haomeiwen.com/subject/cllgwctx.html