mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
af9e309842
Deploy website version based on bb8effca0b
186 lines
44 KiB
HTML
186 lines
44 KiB
HTML
<!DOCTYPE html><html lang="en"><head><meta charSet="utf-8"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><title>Native UI Components · React Native</title><meta name="viewport" content="width=device-width"/><meta name="generator" content="Docusaurus"/><meta name="description" content="There are tons of native UI widgets out there ready to be used in the latest apps - some of them are part of the platform, others are available as third-party libraries, and still more might be in use in your very own portfolio. React Native has several of the most critical platform components already wrapped, like `ScrollView` and `TextInput`, but not all of them, and certainly not ones you might have written yourself for a previous app. Fortunately, it's quite easy to wrap up these existing components for seamless integration with your React Native application."/><meta name="docsearch:version" content="next"/><meta name="docsearch:language" content="en"/><meta property="og:title" content="Native UI Components · React Native"/><meta property="og:type" content="website"/><meta property="og:url" content="https://facebook.github.io/react-native/index.html"/><meta property="og:description" content="There are tons of native UI widgets out there ready to be used in the latest apps - some of them are part of the platform, others are available as third-party libraries, and still more might be in use in your very own portfolio. React Native has several of the most critical platform components already wrapped, like `ScrollView` and `TextInput`, but not all of them, and certainly not ones you might have written yourself for a previous app. Fortunately, it's quite easy to wrap up these existing components for seamless integration with your React Native application."/><meta name="twitter:card" content="summary"/><link rel="shortcut icon" href="/react-native/img/favicon.png"/><link rel="stylesheet" href="https://cdn.jsdelivr.net/docsearch.js/1/docsearch.min.css"/><link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/solarized-dark.min.css"/><link rel="alternate" type="application/atom+xml" href="https://facebook.github.io/react-native/blog/atom.xml" title="React Native Blog ATOM Feed"/><link rel="alternate" type="application/rss+xml" href="https://facebook.github.io/react-native/blog/feed.xml" title="React Native Blog RSS Feed"/><script>
|
||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
||
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
|
||
|
||
ga('create', 'UA-41298772-2', 'auto');
|
||
ga('send', 'pageview');
|
||
</script><script type="text/javascript" src="https://snack.expo.io/embed.js"></script><script type="text/javascript" src="/react-native/js/codeblocks.js"></script><link rel="stylesheet" href="/react-native/css/main.css"/></head><body class="sideNavVisible"><div class="fixedHeaderContainer"><div class="headerWrapper wrapper"><header><a href="/react-native/"><img class="logo" src="/react-native/img/header_logo.png" alt="React Native"/><h2 class="headerTitleWithLogo">React Native</h2></a><a href="/react-native/versions"><h3>next</h3></a><div class="navigationWrapper navigationSlider"><nav class="slidingNav"><ul class="nav-site nav-site-internal"><li class="siteNavGroupActive"><a href="/react-native/docs/next/getting-started" target="_self">Docs</a></li><li class=""><a href="/react-native/en/help" target="_self">Community</a></li><li class=""><a href="/react-native/blog" target="_self">Blog</a></li><li class="navSearchWrapper reactNavSearchWrapper"><input type="text" id="search_input_react" placeholder="Search" title="Search"/></li><li class=""><a href="https://github.com/facebook/react-native" target="_self">GitHub</a></li><li class=""><a href="https://reactjs.org/" target="_self">React</a></li></ul></nav></div></header></div></div><div class="navPusher"><div class="docMainWrapper wrapper"><div class="container docsNavContainer" id="docsNav"><nav class="toc"><div class="toggleNav"><section class="navWrapper wrapper"><div class="navBreadcrumb wrapper"><div class="navToggle" id="navToggler"><i></i></div><h2><i>›</i><span>Guides (Android)</span></h2></div><div class="navGroups"><div class="navGroup"><h3 class="navGroupCategoryTitle">The Basics</h3><ul><li class="navListItem"><a class="navItem" href="/react-native/docs/next/getting-started">Getting Started</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/tutorial">Learn the Basics</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/props">Props</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/state">State</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/style">Style</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/height-and-width">Height and Width</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/flexbox">Layout with Flexbox</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/handling-text-input">Handling Text Input</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/handling-touches">Handling Touches</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/using-a-scrollview">Using a ScrollView</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/using-a-listview">Using List Views</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/network">Networking</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/more-resources">More Resources</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Guides</h3><ul><li class="navListItem"><a class="navItem" href="/react-native/docs/next/components-and-apis">Components and APIs</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/platform-specific-code">Platform Specific Code</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/navigation">Navigating Between Screens</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/images">Images</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/animations">Animations</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/accessibility">Accessibility</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/improvingux">Improving User Experience</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/timers">Timers</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/debugging">Debugging</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/performance">Performance</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/gesture-responder-system">Gesture Responder System</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/javascript-environment">JavaScript Environment</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/direct-manipulation">Direct Manipulation</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/colors">Color Reference</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/integration-with-existing-apps">Integration with Existing Apps</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/building-for-apple-tv">Building For TV Devices</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/running-on-device">Running On Device</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/upgrading">Upgrading to new React Native versions</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/troubleshooting">Troubleshooting</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/native-modules-setup">Native Modules Setup</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Guides (iOS)</h3><ul><li class="navListItem"><a class="navItem" href="/react-native/docs/next/native-modules-ios">Native Modules</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/native-components-ios">Native UI Components</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/linking-libraries-ios">Linking Libraries</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/running-on-simulator-ios">Running On Simulator</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/communication-ios">Communication between native and React Native</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/app-extensions">App Extensions</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Guides (Android)</h3><ul><li class="navListItem"><a class="navItem" href="/react-native/docs/next/native-modules-android">Native Modules</a></li><li class="navListItem navListItemActive"><a class="navItem" href="/react-native/docs/next/native-components-android">Native UI Components</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/headless-js-android">Headless JS</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/signed-apk-android">Generating Signed APK</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/removing-default-permissions">Removing Default Permissions</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Contributing</h3><ul><li class="navListItem"><a class="navItem" href="/react-native/docs/next/contributing">How to Contribute</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/maintainers">What to Expect from Maintainers</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/building-from-source">Building React Native from source</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/testing">Testing your Changes</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/understanding-cli">Understanding the CLI</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Components</h3><ul><li class="navListItem"><a class="navItem" href="/react-native/docs/next/activityindicator">ActivityIndicator</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/button">Button</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/datepickerios">DatePickerIOS</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/drawerlayoutandroid">DrawerLayoutAndroid</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/flatlist">FlatList</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/image">Image</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/inputaccessoryview">InputAccessoryView</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/keyboardavoidingview">KeyboardAvoidingView</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/listview">ListView</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/maskedviewios">MaskedViewIOS</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/modal">Modal</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/navigatorios">NavigatorIOS</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/picker">Picker</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/pickerios">PickerIOS</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/progressbarandroid">ProgressBarAndroid</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/progressviewios">ProgressViewIOS</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/refreshcontrol">RefreshControl</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/safeareaview">SafeAreaView</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/scrollview">ScrollView</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/sectionlist">SectionList</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/segmentedcontrolios">SegmentedControlIOS</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/slider">Slider</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/snapshotviewios">SnapshotViewIOS</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/statusbar">StatusBar</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/switch">Switch</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/tabbarios">TabBarIOS</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/tabbarios-item">TabBarIOS.Item</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/text">Text</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/textinput">TextInput</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/toolbarandroid">ToolbarAndroid</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/touchablehighlight">TouchableHighlight</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/touchablenativefeedback">TouchableNativeFeedback</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/touchableopacity">TouchableOpacity</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/touchablewithoutfeedback">TouchableWithoutFeedback</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/view">View</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/viewpagerandroid">ViewPagerAndroid</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/virtualizedlist">VirtualizedList</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/webview">WebView</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">APIs</h3><ul><li class="navListItem"><a class="navItem" href="/react-native/docs/next/accessibilityinfo">AccessibilityInfo</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/actionsheetios">ActionSheetIOS</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/alert">Alert</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/alertios">AlertIOS</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/animated">Animated</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/appregistry">AppRegistry</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/appstate">AppState</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/asyncstorage">AsyncStorage</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/backandroid">BackAndroid</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/backhandler">BackHandler</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/cameraroll">CameraRoll</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/clipboard">Clipboard</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/datepickerandroid">DatePickerAndroid</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/dimensions">Dimensions</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/easing">Easing</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/geolocation">Geolocation</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/imageeditor">ImageEditor</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/imagepickerios">ImagePickerIOS</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/imagestore">ImageStore</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/image-style-props">Image Style Props</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/interactionmanager">InteractionManager</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/keyboard">Keyboard</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/layout-props">Layout Props</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/layoutanimation">LayoutAnimation</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/linking">Linking</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/listviewdatasource">ListViewDataSource</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/netinfo">NetInfo</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/panresponder">PanResponder</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/permissionsandroid">PermissionsAndroid</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/pixelratio">PixelRatio</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/pushnotificationios">PushNotificationIOS</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/settings">Settings</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/shadow-props">Shadow Props</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/share">Share</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/statusbarios">StatusBarIOS</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/stylesheet">StyleSheet</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/systrace">Systrace</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/text-style-props">Text Style Props</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/timepickerandroid">TimePickerAndroid</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/toastandroid">ToastAndroid</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/transforms">Transforms</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/vibration">Vibration</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/vibrationios">VibrationIOS</a></li><li class="navListItem"><a class="navItem" href="/react-native/docs/next/view-style-props">View Style Props</a></li></ul></div></div></section></div><script>
|
||
document.addEventListener('DOMContentLoaded', function() {
|
||
createToggler('#navToggler', '#docsNav', 'docsSliderActive');
|
||
createToggler('#tocToggler', 'body', 'tocActive');
|
||
|
||
const headings = document.querySelector('.toc-headings');
|
||
headings && headings.addEventListener('click', function(event) {
|
||
if (event.target.tagName === 'A') {
|
||
document.body.classList.remove('tocActive');
|
||
}
|
||
}, false);
|
||
|
||
function createToggler(togglerSelector, targetSelector, className) {
|
||
var toggler = document.querySelector(togglerSelector);
|
||
var target = document.querySelector(targetSelector);
|
||
|
||
toggler.onclick = function(event) {
|
||
event.preventDefault();
|
||
|
||
target.classList.toggle(className);
|
||
};
|
||
}
|
||
});
|
||
</script></nav></div><div class="container mainContainer"><div class="wrapper"><div class="post"><header class="postHeader"><a class="edit-page-link button" href="https://github.com/facebook/react-native-website/blob/master/docs/native-components-android.md" target="_blank" rel="noreferrer noopener">Edit</a><h1 class="postHeaderTitle">Native UI Components</h1></header><article><div><span><p>There are tons of native UI widgets out there ready to be used in the latest apps - some of them are part of the platform, others are available as third-party libraries, and still more might be in use in your very own portfolio. React Native has several of the most critical platform components already wrapped, like <code>ScrollView</code> and <code>TextInput</code>, but not all of them, and certainly not ones you might have written yourself for a previous app. Fortunately, it's quite easy to wrap up these existing components for seamless integration with your React Native application.</p>
|
||
<p>Like the native module guide, this too is a more advanced guide that assumes you are somewhat familiar with Android SDK programming. This guide will show you how to build a native UI component, walking you through the implementation of a subset of the existing <code>ImageView</code> component available in the core React Native library.</p>
|
||
<h2><a class="anchor" aria-hidden="true" id="imageview-example"></a><a href="#imageview-example" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>ImageView example</h2>
|
||
<p>For this example we are going to walk through the implementation requirements to allow the use of ImageViews in JavaScript.</p>
|
||
<p>Native views are created and manipulated by extending <code>ViewManager</code> or more commonly <code>SimpleViewManager</code> . A <code>SimpleViewManager</code> is convenient in this case because it applies common properties such as background color, opacity, and Flexbox layout.</p>
|
||
<p>These subclasses are essentially singletons - only one instance of each is created by the bridge. They vend native views to the <code>NativeViewHierarchyManager</code>, which delegates back to them to set and update the properties of the views as necessary. The <code>ViewManagers</code> are also typically the delegates for the views, sending events back to JavaScript via the bridge.</p>
|
||
<p>Vending a view is simple:</p>
|
||
<ol>
|
||
<li>Create the ViewManager subclass.</li>
|
||
<li>Implement the <code>createViewInstance</code> method</li>
|
||
<li>Expose view property setters using <code>@ReactProp</code> (or <code>@ReactPropGroup</code>) annotation</li>
|
||
<li>Register the manager in <code>createViewManagers</code> of the applications package.</li>
|
||
<li>Implement the JavaScript module</li>
|
||
</ol>
|
||
<h2><a class="anchor" aria-hidden="true" id="1-create-the-viewmanager-subclass"></a><a href="#1-create-the-viewmanager-subclass" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>1. Create the <code>ViewManager</code> subclass</h2>
|
||
<p>In this example we create view manager class <code>ReactImageManager</code> that extends <code>SimpleViewManager</code> of type <code>ReactImageView</code>. <code>ReactImageView</code> is the type of object managed by the manager, this will be the custom native view. Name returned by <code>getName</code> is used to reference the native view type from JavaScript.</p>
|
||
<pre><code class="hljs css languages- java">...
|
||
|
||
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ReactImageManager</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">SimpleViewManager</span><<span class="hljs-title">ReactImageView</span>> </span>{
|
||
|
||
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> String REACT_CLASS = <span class="hljs-string">"RCTImageView"</span>;
|
||
|
||
<span class="hljs-meta">@Override</span>
|
||
<span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getName</span><span class="hljs-params">()</span> </span>{
|
||
<span class="hljs-keyword">return</span> REACT_CLASS;
|
||
}
|
||
</code></pre>
|
||
<h2><a class="anchor" aria-hidden="true" id="2-implement-method-createviewinstance"></a><a href="#2-implement-method-createviewinstance" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>2. Implement method <code>createViewInstance</code></h2>
|
||
<p>Views are created in the <code>createViewInstance</code> method, the view should initialize itself in its default state, any properties will be set via a follow up call to <code>updateView.</code></p>
|
||
<pre><code class="hljs css languages- java"> <span class="hljs-meta">@Override</span>
|
||
<span class="hljs-function"><span class="hljs-keyword">public</span> ReactImageView <span class="hljs-title">createViewInstance</span><span class="hljs-params">(ThemedReactContext context)</span> </span>{
|
||
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> ReactImageView(context, Fresco.newDraweeControllerBuilder(), mCallerContext);
|
||
}
|
||
</code></pre>
|
||
<h2><a class="anchor" aria-hidden="true" id="3-expose-view-property-setters-using-reactprop-or-reactpropgroup-annotation"></a><a href="#3-expose-view-property-setters-using-reactprop-or-reactpropgroup-annotation" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>3. Expose view property setters using <code>@ReactProp</code> (or <code>@ReactPropGroup</code>) annotation</h2>
|
||
<p>Properties that are to be reflected in JavaScript needs to be exposed as setter method annotated with <code>@ReactProp</code> (or <code>@ReactPropGroup</code>). Setter method should take view to be updated (of the current view type) as a first argument and property value as a second argument. Setter should be declared as a <code>void</code> method and should be <code>public</code>. Property type sent to JS is determined automatically based on the type of value argument of the setter. The following type of values are currently supported: <code>boolean</code>, <code>int</code>, <code>float</code>, <code>double</code>, <code>String</code>, <code>Boolean</code>, <code>Integer</code>, <code>ReadableArray</code>, <code>ReadableMap</code>.</p>
|
||
<p>Annotation <code>@ReactProp</code> has one obligatory argument <code>name</code> of type <code>String</code>. Name assigned to the <code>@ReactProp</code> annotation linked to the setter method is used to reference the property on JS side.</p>
|
||
<p>Except from <code>name</code>, <code>@ReactProp</code> annotation may take following optional arguments: <code>defaultBoolean</code>, <code>defaultInt</code>, <code>defaultFloat</code>. Those arguments should be of the corresponding primitive type (accordingly <code>boolean</code>, <code>int</code>, <code>float</code>) and the value provided will be passed to the setter method in case when the property that the setter is referencing has been removed from the component. Note that "default" values are only provided for primitive types, in case when setter is of some complex type, <code>null</code> will be provided as a default value in case when corresponding property gets removed.</p>
|
||
<p>Setter declaration requirements for methods annotated with <code>@ReactPropGroup</code> are different than for <code>@ReactProp</code>, please refer to the <code>@ReactPropGroup</code> annotation class docs for more information about it.</p>
|
||
<p><strong>IMPORTANT!</strong> in ReactJS updating the property value will result in setter method call. Note that one of the ways we can update component is by removing properties that have been set before. In that case setter method will be called as well to notify view manager that property has changed. In that case "default" value will be provided (for primitive types "default" can value can be specified using <code>defaultBoolean</code>, <code>defaultFloat</code>, etc. arguments of <code>@ReactProp</code> annotation, for complex types setter will be called with value set to <code>null</code>).</p>
|
||
<pre><code class="hljs css languages- java"> <span class="hljs-meta">@ReactProp</span>(name = <span class="hljs-string">"src"</span>)
|
||
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setSrc</span><span class="hljs-params">(ReactImageView view, @Nullable ReadableArray sources)</span> </span>{
|
||
view.setSource(sources);
|
||
}
|
||
|
||
<span class="hljs-meta">@ReactProp</span>(name = <span class="hljs-string">"borderRadius"</span>, defaultFloat = <span class="hljs-number">0f</span>)
|
||
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setBorderRadius</span><span class="hljs-params">(ReactImageView view, <span class="hljs-keyword">float</span> borderRadius)</span> </span>{
|
||
view.setBorderRadius(borderRadius);
|
||
}
|
||
|
||
<span class="hljs-meta">@ReactProp</span>(name = ViewProps.RESIZE_MODE)
|
||
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setResizeMode</span><span class="hljs-params">(ReactImageView view, @Nullable String resizeMode)</span> </span>{
|
||
view.setScaleType(ImageResizeMode.toScaleType(resizeMode));
|
||
}
|
||
</code></pre>
|
||
<h2><a class="anchor" aria-hidden="true" id="4-register-the-viewmanager"></a><a href="#4-register-the-viewmanager" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>4. Register the <code>ViewManager</code></h2>
|
||
<p>The final Java step is to register the ViewManager to the application, this happens in a similar way to <a href="/react-native/docs/next/native-modules-android">Native Modules</a>, via the applications package member function <code>createViewManagers.</code></p>
|
||
<pre><code class="hljs css languages- java"> <span class="hljs-meta">@Override</span>
|
||
<span class="hljs-function"><span class="hljs-keyword">public</span> List<ViewManager> <span class="hljs-title">createViewManagers</span><span class="hljs-params">(
|
||
ReactApplicationContext reactContext)</span> </span>{
|
||
<span class="hljs-keyword">return</span> Arrays.<ViewManager>asList(
|
||
<span class="hljs-keyword">new</span> ReactImageManager()
|
||
);
|
||
}
|
||
</code></pre>
|
||
<h2><a class="anchor" aria-hidden="true" id="5-implement-the-javascript-module"></a><a href="#5-implement-the-javascript-module" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>5. Implement the JavaScript module</h2>
|
||
<p>The very final step is to create the JavaScript module that defines the interface layer between Java and JavaScript for the users of your new view. Much of the effort is handled by internal React code in Java and JavaScript and all that is left for you is to describe the <code>propTypes</code>.</p>
|
||
<pre><code class="hljs css languages- javascript"><span class="hljs-comment">// ImageView.js</span>
|
||
|
||
<span class="hljs-keyword">import</span> PropTypes <span class="hljs-keyword">from</span> <span class="hljs-string">'prop-types'</span>;
|
||
<span class="hljs-keyword">import</span> {requireNativeComponent, ViewPropTypes} <span class="hljs-keyword">from</span> <span class="hljs-string">'react-native'</span>;
|
||
|
||
<span class="hljs-keyword">var</span> iface = {
|
||
<span class="hljs-attr">name</span>: <span class="hljs-string">'ImageView'</span>,
|
||
<span class="hljs-attr">propTypes</span>: {
|
||
<span class="hljs-attr">src</span>: PropTypes.string,
|
||
<span class="hljs-attr">borderRadius</span>: PropTypes.number,
|
||
<span class="hljs-attr">resizeMode</span>: PropTypes.oneOf([<span class="hljs-string">'cover'</span>, <span class="hljs-string">'contain'</span>, <span class="hljs-string">'stretch'</span>]),
|
||
...ViewPropTypes, <span class="hljs-comment">// include the default view properties</span>
|
||
},
|
||
};
|
||
|
||
<span class="hljs-built_in">module</span>.exports = requireNativeComponent(<span class="hljs-string">'RCTImageView'</span>, iface);
|
||
</code></pre>
|
||
<p><code>requireNativeComponent</code> commonly takes two parameters, the first is the name of the native view and the second is an object that describes the component interface. The component interface should declare a friendly <code>name</code> for use in debug messages and must declare the <code>propTypes</code> reflected by the Native View. The <code>propTypes</code> are used for checking the validity of a user's use of the native view. Note that if you need your JavaScript component to do more than just specify a name and propTypes, like do custom event handling, you can wrap the native component in a normal react component. In that case, you want to pass in the wrapper component instead of <code>iface</code> to <code>requireNativeComponent</code>. This is illustrated in the <code>MyCustomView</code> example below.</p>
|
||
<h1><a class="anchor" aria-hidden="true" id="events"></a><a href="#events" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Events</h1>
|
||
<p>So now we know how to expose native view components that we can control easily from JS, but how do we deal with events from the user, like pinch-zooms or panning? When a native event occurs the native code should issue an event to the JavaScript representation of the View, and the two views are linked with the value returned from the <code>getId()</code> method.</p>
|
||
<pre><code class="hljs css languages- java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyCustomView</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">View</span> </span>{
|
||
...
|
||
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onReceiveNativeEvent</span><span class="hljs-params">()</span> </span>{
|
||
WritableMap event = Arguments.createMap();
|
||
event.putString(<span class="hljs-string">"message"</span>, <span class="hljs-string">"MyMessage"</span>);
|
||
ReactContext reactContext = (ReactContext)getContext();
|
||
reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
|
||
getId(),
|
||
<span class="hljs-string">"topChange"</span>,
|
||
event);
|
||
}
|
||
}
|
||
</code></pre>
|
||
<p>To map the <code>topChange</code> event name to the <code>onChange</code> callback prop in JavaScript, register it by overriding the <code>getExportedCustomBubblingEventTypeConstants</code> method in your <code>ViewManager</code>:</p>
|
||
<pre><code class="hljs css languages- java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ReactImageManager</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">SimpleViewManager</span><<span class="hljs-title">MyCustomView</span>> </span>{
|
||
...
|
||
<span class="hljs-function"><span class="hljs-keyword">public</span> Map <span class="hljs-title">getExportedCustomBubblingEventTypeConstants</span><span class="hljs-params">()</span> </span>{
|
||
<span class="hljs-keyword">return</span> MapBuilder.builder()
|
||
.put(
|
||
<span class="hljs-string">"topChange"</span>,
|
||
MapBuilder.of(
|
||
<span class="hljs-string">"phasedRegistrationNames"</span>,
|
||
MapBuilder.of(<span class="hljs-string">"bubbled"</span>, <span class="hljs-string">"onChange"</span>)))
|
||
.build();
|
||
}
|
||
}
|
||
</code></pre>
|
||
<p>This callback is invoked with the raw event, which we typically process in the wrapper component to make a simpler API:</p>
|
||
<pre><code class="hljs css languages- javascript"><span class="hljs-comment">// MyCustomView.js</span>
|
||
|
||
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyCustomView</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
|
||
<span class="hljs-keyword">constructor</span>(props) {
|
||
<span class="hljs-keyword">super</span>(props);
|
||
<span class="hljs-keyword">this</span>._onChange = <span class="hljs-keyword">this</span>._onChange.bind(<span class="hljs-keyword">this</span>);
|
||
}
|
||
_onChange(event: Event) {
|
||
<span class="hljs-keyword">if</span> (!<span class="hljs-keyword">this</span>.props.onChangeMessage) {
|
||
<span class="hljs-keyword">return</span>;
|
||
}
|
||
<span class="hljs-keyword">this</span>.props.onChangeMessage(event.nativeEvent.message);
|
||
}
|
||
render() {
|
||
<span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag"><<span class="hljs-name">RCTMyCustomView</span> {<span class="hljs-attr">...this.props</span>} <span class="hljs-attr">onChange</span>=<span class="hljs-string">{this._onChange}</span> /></span>;
|
||
}
|
||
}
|
||
MyCustomView.propTypes = {
|
||
/**
|
||
* Callback that is called continuously when the user is dragging the map.
|
||
*/
|
||
onChangeMessage: PropTypes.func,
|
||
...
|
||
};
|
||
|
||
var RCTMyCustomView = requireNativeComponent(`RCTMyCustomView`, MyCustomView, {
|
||
nativeOnly: {onChange: true}
|
||
});
|
||
</span></code></pre>
|
||
<p>Note the use of <code>nativeOnly</code> above. Sometimes you'll have some special properties that you need to expose for the native component, but don't actually want them as part of the API for the associated React component. For example, <code>Switch</code> has a custom <code>onChange</code> handler for the raw native event, and exposes an <code>onValueChange</code> handler property that is invoked with just the boolean value rather than the raw event (similar to <code>onChangeMessage</code> in the example above). Since you don't want these native only properties to be part of the API, you don't want to put them in <code>propTypes</code>, but if you don't you'll get an error. The solution is simply to call them out via the <code>nativeOnly</code> option.</p>
|
||
</span></div></article></div><div class="docs-prevnext"><a class="docs-prev button" href="/react-native/docs/next/native-modules-android">← Native Modules</a><a class="docs-next button" href="/react-native/docs/next/headless-js-android">Headless JS →</a></div></div></div></div><footer class="nav-footer" id="footer"><section class="sitemap"><a href="/react-native/" class="nav-home"><img src="/react-native/img/header_logo.png" alt="React Native" width="66" height="58"/></a><div><h5><a href="/react-native/docs/getting-started.html">Docs</a></h5><a href="/react-native/docs/getting-started.html">Getting Started</a><a href="/react-native/docs/tutorial.html">Tutorial</a><a href="/react-native/docs/components-and-apis.html">Components and APIs</a><a href="/react-native/docs/more-resources.html">More Resources</a></div><div><h5><a href="/react-native/help.html">Community</a></h5><a href="/react-native/showcase.html">Who's using React Native?</a><a href="http://stackoverflow.com/questions/tagged/react-native" target="_blank">Stack Overflow</a><a href="https://discuss.reactjs.org" target="_blank">Discussion Forum</a><a href="https://discord.gg/0ZcbPKXt5bZjGY5n">Reactiflux Chat</a></div><div><h5>More</h5><a href="/react-native/blog">Blog</a><a href="https://twitter.com/reactnative" target="_blank">Twitter</a><a href="https://github.com/facebook/react-native" target="_blank">GitHub</a><a href="http://reactjs.org" target="_blank">React</a></div></section><a href="https://code.facebook.com/projects/" target="_blank" class="fbOpenSource"><img src="/react-native/img/oss_logo.png" alt="Facebook Open Source" width="170" height="45"/></a><section class="copyright">Copyright © 2018 Facebook Inc.</section></footer></div><script type="text/javascript" src="https://cdn.jsdelivr.net/docsearch.js/1/docsearch.min.js"></script><script>window.fbAsyncInit = function() {FB.init({appId:'1677033832619985',xfbml:true,version:'v2.7'});};(function(d, s, id){var js, fjs = d.getElementsByTagName(s)[0];if (d.getElementById(id)) {return;}js = d.createElement(s); js.id = id;js.src = '//connect.facebook.net/en_US/sdk.js';fjs.parentNode.insertBefore(js, fjs);}(document, 'script','facebook-jssdk'));
|
||
</script><script>window.twttr=(function(d,s, id){var js,fjs=d.getElementsByTagName(s)[0],t=window.twttr||{};if(d.getElementById(id))return t;js=d.createElement(s);js.id=id;js.src='https://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js, fjs);t._e = [];t.ready = function(f) {t._e.push(f);};return t;}(document, 'script', 'twitter-wjs'));</script><script>
|
||
var search = docsearch({
|
||
|
||
apiKey: '2c98749b4a1e588efec53b2acec13025',
|
||
indexName: 'react-native-versions',
|
||
inputSelector: '#search_input_react',
|
||
algoliaOptions: {"facetFilters":["tags:next"],"hitsPerPage":5}
|
||
});
|
||
</script></body></html> |