当前位置: 首页 > 知识库问答 >
问题:

如何让Android片段显示在react native中?

羊舌赞
2023-03-14

我尝试过构建本机用户界面组件,但通常是成功或失败的。该组件似乎没有以react-本机呈现。

不过,对于这个问题,我特别尝试在react-native中渲染一个Android片段,但没有显示任何内容。示例代码:

public class PreferenceViewManager extends SimpleViewManager<FrameLayout> {
    public static final String REACT_CLASS = "RCTPreferenceView";

    private WeakReference<Activity> activityWeakReference;

    public PreferenceViewManager(WeakReference<Activity> activityWeakReference) {
        this.activityWeakReference = activityWeakReference;
    }

    @Override
    public String getName() {
        return REACT_CLASS;
    }

    @Override
    public FrameLayout createViewInstance(ThemedReactContext context) {
        Log.d(REACT_CLASS, "PreferenceView createViewInstance");
        final FrameLayout view = new FrameLayout(context);
        view.setId(View.generateViewId());
        // Testing if the view does get rendered, it should show white even if fragment is not rendered!
        view.setBackgroundColor(Color.WHITE);
        // not sure where to call fragment beginTransaction properly, the code below is just for testing
        view.postDelayed(new Runnable() {
            @Override
            public void run() {
                onAfterUpdateTransaction(view);
            }
        }, 2000);
        return view;
    }

    @Override
    public void onAfterUpdateTransaction(FrameLayout view) {
        super.onAfterUpdateTransaction(view);

        activityWeakReference.get().getFragmentManager().beginTransaction().replace(view.getId(), new PreferenceView()).commit();
        Log.d(REACT_CLASS, "PreferenceView Commit"); // for debug
    }
}

使用上面的视图管理器,视图应该在的地方甚至没有被涂成白色,这意味着框架布局本身没有被渲染。

我做错了什么?

共有2个答案

邵绪
2023-03-14

您必须在视图管理器中公开片段,然后将视图管理器桥接到反应本地javascript代码。

锺离辰沛
2023-03-14

事实上,你的方向几乎是正确的。你的FrameLayout没有被渲染的原因是因为你没有在ReactNative中明确地添加一个样式来指定它的高度和宽度。然而,即使这样,你的片段也不会被渲染。你可以执行一些更改来渲染你的片段。

在ViewManager中,执行以下更改:

@Override
public FrameLayout createViewInstance(ThemedReactContext context) {
    final FrameLayout view = new FrameLayout(context);
    MyFragment fragment = MyFragment.newInstance();
    // Add the fragment into the FrameLayout
    reactContext.getCurrentActivity().getFragmentManager()
            .beginTransaction()
            .add(fragment, "My_TAG")
            .commit();
    // Execute the commit immediately or can use commitNow() instead
    reactContext.getCurrentActivity().getFragmentManager().executePendingTransactions();
    // This step is needed to in order for ReactNative to render your view
    addView(fragment.getView(), LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    return view;
}

然后在您的ReactNative组件中

class ExampleComponent extends React.Component {
  render() {
    return (
      <View>
        <Text>Hello</Text>
        <RCTPreferenceView
          {...this.props}
          style={{ height: "80%", width: "100%" }}
        />
      </View>
    );
  }
}

希望这可以帮助那些试图在反应视图内渲染片段的人

 类似资料:
  • 我正在开发包含2个片段的应用程序,我想根据需要显示隐藏。下面的代码有一个简单的例子来说明我的问题。这个简单的Fragmentactivity包含一个按钮和一个listfragment。 这个简单的例子完美无瑕。但我不满足于展示隐藏片段。如果删除布局。设置可见性(View.GONE);然后从代码中选择ft.hide(f);不会隐藏碎片。事实上,我们不是在隐藏片段,而是在隐藏容器。 我的问题是,这是一

  • 我正在尝试学习如何在片段中显示具有3列的网格视图。值来自strings.xml文件作为数组列表。当我运行应用程序时,它会崩溃。有人能帮助我在3列中显示值,即位置、描述和时间。我无法继续在gridview中显示读取值。 错误 致命异常:主java。com上的lang.NullPointerException。ts.FlightFragment$MyAdapter。(FlightFragment.ja

  • 问题内容: 只是想知道用UML中的一些片段或更具体地说是类图来表示活动的最佳方式是什么。 问题答案: 这取决于观点。 可以通过对象图或复合结构图来对片段进行最彻底的分析。 如果要建模其外部行为,则可以使用组件图或任何行为图。

  • 我正在尝试制作一个选项卡,其中我必须在gridview中显示图像,所以我从一个库中制作了普通选项卡,并制作了一个适配器来显示我的主要活动代码中的图像。 我的片段类。 我已将适配器设置如下: 我能够在活动中创建网格视图,但是我用它来声明类中的图像,但在这里我遇到了错误。我在适配器中声明为适配器将调用图像的 Sso。它正确编译,没有任何错误,但它显示运行时错误错误为 那么如何解决这个问题,我必须声明图

  • 因此,在Android的ImageView中显示图像非常简单。我从图库中挑选了一张图片,保存了它到数据库的链接,然后尝试在ImageView中显示它。 我也尝试了这个小的图像不是由相机拍摄的,这是工作的。但每次我选择用手机相机拍摄的图像时,都会有一个错误: 那么,在超过300万像素的相机中,如何在我的ImageView中显示图像呢?

  • 我正试图在现有的应用程序中实现新的Android导航组件。我有两个片段除了名字外都是一样的。当我将startDestination设置为fragment2时,片段似乎显示正确。当startDestination设置为fragment1时,我看不到膨胀的视图,但我确实看到了“Fragment 1 created”toast。 我做错了什么? 导航图。xml 主活动布局: Fragment1布局: 依