我需要在RecyclerView中嵌入一个React Native视图。我创建了一个简单的包装器布局,其中包含我在RecyclerView中充气的ReactRootView
:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.facebook.react.ReactRootView
android:id="@+id/rootView"
android:layout_height="40dp"
android:layout_width="match_parent"/>
</FrameLayout>
以下是我如何充气视图:
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.react_native_wrapper, null);
return new ViewHolder(view);
}
渲染ReactRootView
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Bundle launchOptions = new Bundle();
launchOptions.putInt("index", position);
holder.reactRootView.startReactApplication(mReactInstanceManager, "HelloWorld", launchOptions);
}
重新使用ViewHolder并再次调用onBindViewHolder
时,我会在此处获得例外:holder.reactRootView.startReactApplication(mReactInstanceManager, "HelloWorld", launchOptions)
。
错误消息:java.lang.AssertionError: This root view has already been attached to a catalyst instance manager
如何重用ReactRootView实例?当我为每个项目创建一个新的ViewHolder时,我没有遇到这个错误,但是这违背了RecyclerView的目的。