我试图将YouTube Android API包装为React Native的UI组件.我已成功完成Android配置(获取onInitializationSuccess),但我无法弄清楚如何将YouTubePlayerView恢复到我的React Native应用程序.
根据文档,如果您无法扩展YouTubeBaseActivity,他们建议您使用YouTubePlayerFragment.由于Android上的React Native不使用基于XML的布局,因此我尝试以编程方式创建视图.但是,当我返回包装视图(我尝试作为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;
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
YouTube.js
class YouTube extends Component {
render () {
return <YouTubeAndroid {...this.props}/>;
}
};
var iface = {
name : 'YouTube',
propTypes …Run Code Online (Sandbox Code Playgroud) 我已将动态生成的FrameLayout添加到react-native视图中.Framelayout正在出现.但是当我向视图添加一个片段时,什么也没有显示.
这是代码的一部分:
public FrameLayout createViewInstance(ThemedReactContext context) {
Fragment fragment = MapFragment.newInstance(mapKey);
FrameLayout view = new FrameLayout(context);
view.setWillNotDraw(false);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
view.setLayoutParams(lp);
view.setId(View.generateViewId());
view.setBackgroundColor(Color.parseColor("#1FFDDB"));
FragmentActivity activity = MainActivity.getFragmentActivity();
FragmentManager fragmentManager = activity.getSupportFragmentManager();
fragmentManager.beginTransaction().remove(fragment).commit();
fragmentManager.executePendingTransactions();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(0, fragment);
fragmentTransaction.show(fragment);
fragmentTransaction.attach(fragment);
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();
return view;
}
Run Code Online (Sandbox Code Playgroud)
当我向视图添加TextView时,它显示:
// add a textfield to the view
TextView textView1 = new TextView(context);
textView1.setText("Android");
textView1.setTextSize(30);
textView1.setGravity(Gravity.CENTER);
view.setVisibility(View.VISIBLE);
view.addView(textView1);
Run Code Online (Sandbox Code Playgroud)
所以Fragment存在问题.任何想法为什么片段没有出现?