- React Native 調(diào)用原生RecyclerView 無法刷新,需要手動(dòng)下拉或者上拉一下才能成功刷新鳖宾,要解決這個(gè)問題掀亩,需要我們自定義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"
/>
- 好了虏冻,就分享到這,謝謝大家觀看弹囚!