草庐IT

android - 在 React Native Android ViewManager 中公开 fragment

coder 2023-11-18 原文

我正在尝试将 YouTube Android API 包装为 React Native 的 UI 组件。我在 Android 配置方面取得了成功(获得 onInitializationSuccess),但是我无法弄清楚如何将 YouTubePlayerView 返回到我的 React Native 应用程序。

根据文档,如果您无法扩展 YouTubeBaseActivity,他们建议使用 YouTubePlayerFragment。由于 Android 上的 React Native 不使用基于 XML 的布局,我尝试以编程方式创建 View 。但是,当我返回包装 View 时(我尝试作为 FrameLayout,但不确定这是否是正确的选择)我创建它不会在应用程序上呈现任何内容。

我希望暂时保持它非常简单,这里是必要的代码:

YouTubeManager.java

public class YouTubeManager extends SimpleViewManager<FrameLayout>  implements YouTubePlayer.OnInitializedListener {
// ...
@Override
    protected FrameLayout createViewInstance(ThemedReactContext reactContext) {
        this.reactContext = reactContext;

        FrameLayout view = new FrameLayout(reactContext);
        view.setId(View.generateViewId());


        FragmentManager fragmentManager = activity.getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        YouTubePlayerFragment fragment = new YouTubePlayerFragment();
        fragmentTransaction.add(view.getId(), fragment);

        fragmentTransaction.commit();

        fragment.initialize("SECRET_KEY", this);

        return view;
    }
// ... 
}

YouTube.js

class YouTube extends Component {
    render () {
        return <YouTubeAndroid {...this.props}/>;
    }
};

var iface = {
    name : 'YouTube',
    propTypes : {
        ...View.propTypes
    },
};


var YouTubeAndroid = requireNativeComponent('YouTube', iface);

module.exports = YouTube;

index.android.js

var YouTube = require('./YouTube');

class YouTubePlayer extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Text>hello</Text>
        <YouTube />
      </View>
    );
  }
}

非常感谢任何帮助,谢谢!

最佳答案

我没有针对 YouTube fragment 的实际解决方案,但我使用了一些变通方法在 RN 中显示 YouTube View 。

(也许您可以先在您的 fragment 上尝试 3. 中的 hack。)

  1. 制作您自己的 ReactActivity 来扩展 YouTubeBaseActivity 并让 MainActivity 扩展它。

    public abstract class ReactActivity extends YouTubeBaseActivity implements DefaultHardwareBackBtnHandler {
      // copy & paste RN's ReactActivity.java source
    
  2. YouTubePlayerView 创建 YoutubeManager 类。

    当您实现 ReactPackage 时,它应该从 createViewManagers 获取一个 Activity 实例。

    public class YoutubeManager extends SimpleViewManager<YouTubePlayerView> implements YouTubePlayer.OnInitializedListener, YouTubePlayer.PlayerStateChangeListener,  YouTubePlayer.PlaybackEventListener {
    
      public static final String REACT_CLASS = "RCTYoutube";
      private static final String YOUTUBE_API_KEY = "<YOUR_OWN_KEY>";
    
      private YouTubeBaseActivity mActivity = null;
      private YouTubePlayerView mYouTubePlayerView = null;
    
      public YoutubeManager(Activity activity) {
        super();
        mActivity = (YouTubeBaseActivity) activity;
      }
    
      @Override
      public String getName() {
        return REACT_CLASS;
      }
    
      @Override
      public YouTubePlayerView createViewInstance(ThemedReactContext context) {
        mContext = context;
    
        mYouTubePlayerView = new YouTubePlayerView(mActivity);
        mYouTubePlayerView.initialize(YOUTUBE_API_KEY, this);
    
        return mYouTubePlayerView;
      }
    

    并为它制作一个js模块。

    module.exports = requireNativeComponent('RCTYoutube', iface);
    
  3. 现在您可以显示 YouTube View ,但它看起来像是空 View 。你需要一些技巧。

    componentDidMount() {
      this.setState({
        width: this.props.style.width,
        height: this.props.style.height - 1
      })
      this.timer = setInterval(()=>{
        this.setState({
          width: this.props.style.width,
          height: this.props.style.height + Math.floor(Math.random() * 2)
        })
      }, 500)
    }
    
    render() {
      return (<YoutubeView style={[style, { width: this.state.width, height: this.state.height }]} />)
    }
    
    componentWillUnmount() {
      if(this.timer) clearTimeout(this.timer);
    }
    

您可以从我的应用程序中看到它实际是如何工作的。

https://play.google.com/store/apps/details?id=kr.dobbit.sharehows

关于android - 在 React Native Android ViewManager 中公开 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36226702/

有关android - 在 React Native Android ViewManager 中公开 fragment的更多相关文章

  1. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  2. ruby-on-rails - 如果字段不为零,则葡萄实体有条件地公开 - 2

    在一个葡萄实体中,我只想在没有运气的情况下显示一个字段(不是零?)。我正在尝试这段代码,但根本没有按预期工作,但总是隐藏该字段。expose:winner,:using=>PlayerEntity,:unless=>{:winner=>nil}我认为代码本身解释了我真正需要的东西,但正如我所说,我没有得到预期的结果。有什么线索吗? 最佳答案 好的,检查grape-entity的代码我发现你需要将这个block作为RubyProc传递。此代码将按预期工作:expose:winner,:using=>PlayerEntity,:unle

  3. ruby-on-rails - 将配置文件模型的某些属性设置为对其他用户公开(可见)或私有(private)(不可见)的最佳方法是什么? - 2

    我有一个Profile模型,它有很多属性,比如电子邮件、图像、年龄、地址等。最终用户可以将某些属性设为私有(private),以便其他用户无法查看。我通过向表private_attr添加一列并将其序列化以存储哈希来解决这个问题:-{email:true,address:true,age:false}这里的属性作为具有值true的键被认为是私有(private)的,不会向除这些属性所属的用户以外的用户显示。我想知道这是解决这个问题的最好方法,还是有其他方法。提前致谢。 最佳答案 我认为您可以只序列化用户希望在数组中私有(private

  4. Android Studio开发之使用内容组件Content获取通讯信息讲解及实战(附源码 包括添加手机联系人和发短信) - 2

    运行有问题或需要源码请点赞关注收藏后评论区留言一、利用ContentResolver读写联系人在实际开发中,普通App很少会开放数据接口给其他应用访问。内容组件能够派上用场的情况往往是App想要访问系统应用的通讯数据,比如查看联系人,短信,通话记录等等,以及对这些通讯数据及逆行增删改查。首先要给AndroidMaifest.xml中添加响应的权限配置 下面是往手机通讯录添加联系人信息的例子效果如下分成三个步骤先查出联系人的基本信息,然后查询联系人号码,再查询联系人邮箱代码 ContactAddActivity类packagecom.example.chapter07;importandroid

  5. Android 10.0 设置默认launcher后安装另外launcher后默认Launcher失效的功能修复 - 2

    1.前言 在10.0的系统rom定制化开发中,在系统中有多个launcher的时候,会在开机进入launcher的时候弹窗launcher列表,让用户选择进入哪个launcher,这样显得特别的不方便所以产品开发中,要求用RoleManager的相关api来设置默认Launcher,但是在设置完默认Launcher以后,在安装一款Launcher的时候,默认Launcher就会失效,在系统设置的默认应用中Launcher选项就为空,点击home键的时候会弹出默认Launcher列表,让选择进入哪个默认Launcher.所以需要从安装Launcher的流程来分析相关的设置。来解决问题设置默认La

  6. AiBote 2022 新研发的自动化框架,支持 Android 和 Windows 系统。速度非常快 - 2

    Ai-Bot基于流行的Node.js和JavaScript语言的一款新自动化框架,支持Windows和Android自动化。1、Windowsxpath元素定位算法支持支持Windows应用、.NET、WPF、Qt、Java和Electron客户端程序和ie、edgechrome浏览器2、Android支持原生APP和H5界面,元素定位速度是appium十倍,无线远程自动化操作多台安卓设备3、基于opencv图色算法,支持找图和多点找色,1080*2340全分辨率找图50MS以内4、内置免费OCR人工智能技术,无限制获取图片文字和找字功能。5、框架协议开源,除官方node.jsSDK外,用户可

  7. Android Gradle 7.1+新版本依赖变化 - 2

    前一段时间由于工作需要把可爱的小雪狐舍弃了,找到了小蜜蜂。但是新版本的小蜜蜂出现了很多和旧版本不一样的位置。1.功能位置迁移,原来在工程build.gradle的buildscript和allprojects移动至setting.gradle并改名为pluginManagement和dependencyResolutionManagement。里面的东西依旧可以按照原来的copy过来。pluginManagement{repositories{gradlePluginPortal()google()mavenCentral()}}dependencyResolutionManagement{r

  8. ruby - Ruboto 的最佳教程(适用于 Android 的 ruby​​)? - 2

    关闭。这个问题不符合StackOverflowguidelines.它目前不接受答案。要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于StackOverflow来说是偏离主题的,因为它们往往会吸引自以为是的答案和垃圾邮件。相反,describetheproblem以及迄今为止为解决该问题所做的工作。关闭9年前。Improvethisquestion我几乎用完了Ruby,但现在想试试Ruboto,android上的ruby​​。谷歌未能给我足够的(几乎没有结果)。所以任何人都可以分享一些关于Ruboto的教程。

  9. ruby - 使用 NFS 和 private_network 时将 Vagrant VM 公开到网络 - 2

    我有一个多机Vagrant设置。使用NFS和private_network我可以让我需要的一切正常工作(Drupal、PHP等),除了允许大厅下面的人访问我的VM上运行的Web应用程序。我了解private_network使得无法从外部连接到VM。有没有一种方法可以同时创建私有(private)网络和公共(public)网络,以便除负载均衡器之外的所有VMS都是私有(private)的,并且可以通过主机的ip访问负载均衡器?hosts={"wloadlocal"=>"192.168.33.10","wweblocal1"=>"192.168.33.11","wweblocal2"=>"

  10. Android Studio 解决Could not resolve com.android.tools.build:gradle:7.4.2问题 - 2

    Aproblemoccurredconfiguringrootproject'MyApplication2'.>Couldnotresolveallfilesforconfiguration':classpath'.  >Couldnotresolvecom.android.tools.build:gradle:7.4.2.   Requiredby:     project:>com.android.application:com.android.application.gradle.plugin:7.4.2     project:>com.android.library:com.andr

随机推荐