Kotlin编程之Kotlin Android Extensions(扩展插件) 痛定思痛。 2022-06-14 04:30 1265阅读 0赞 **Kotlin编程开发Android运用程序的相关介绍**: * [Kotlin编程之AndroidStudio(包括3.0与2.x版本)配置与使用][Kotlin_AndroidStudio_3.0_2.x] * [ Kotlin编程开发Android运用程序(Volley+Gson依赖库)][Kotlin_Android_Volley_Gson] -------------------- ## **Kotlin Android Extensions** ## -------------------- 介绍: > Kotlin Android扩展插件可以节省`findviewbyid()`,实现与Data-Binding,Dagger框架的效果,不需要添加任何额外代码,也不影响任何运行时体验。 Kotlin Android扩展是Kotlin 插件的组成之一,不需要在单独安装插件。 **在Gralde中配置**: apply plugin: 'kotlin-android-extensions' 点击syncNow,开始同步。 ![这里写图片描述][SouthEast] ## **导入合成属性** ## -------------------- 使用Kotlin Android Extensions在以下常用的情况: #### **1. 在Activity中**: #### 按照`import kotlinx.android.synthetic.main.<布局>.*`格式,可以导入布局文件中所有控件属性。 ![这里写图片描述][SouthEast 1] 接着,根据控件的Id直接引用控件对象: ![这里写图片描述][SouthEast 2] 最终,简洁的代码如下: class ScrollingActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_scrolling) //等同于findViewById(R.id.toolbar) as Toolbar var toolbarView=toolbar //为了更容易看懂,声明了一个变量。最简洁: setSupportActionBar(toolbar),一行搞定。 setSupportActionBar(toolbarView) .......... } ......... } #### **2. 在Adapter中**: #### 在Adapter中使用movielist\_item.xml布局如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp"> <ImageView android:id="@+id/movielist_item_iv" android:layout_width="100dp" android:layout_height="100dp" android:scaleType="centerCrop"/> <TextView android:id="@+id/movielist_item_tv" android:layout_width="wrap_content" android:layout_marginLeft="20dp" android:textStyle="bold" android:layout_gravity="center_vertical" android:layout_height="wrap_content" /> </LinearLayout> 在Adapter中,先按`import kotlinx.android.synthetic.main.item布局名.view.*`的方式导入。 import kotlinx.android.synthetic.main.movielist_item.view.* internal class MovieListAdapter(var context: Context, var list: List<MovieList.Movie>) : RecyclerView.Adapter<MovieListAdapter.ViewHolder>() { internal class ViewHolder(rootView: View) : RecyclerView.ViewHolder(rootView) { /** * 这里使用Kotlin Android 扩展,省略了findViewById(). * 在最上面导入了import kotlinx.android.synthetic.main.movielist_item.view.* */ var imageView = rootView.movielist_item_iv var title_Tv= rootView.movielist_item_tv } } #### **3. 在Fragment中**: #### 和Adapter中使用类似 Fragment中对应的布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:fitsSystemWindows="true" android:layout_height="match_parent"> //......... <android.support.v7.widget.RecyclerView android:id="@+id/movieList_recyclverView" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout> 在Fragment编写代码: import kotlinx.android.synthetic.main.fragment_movielist.view.* class MovieListFragment : Fragment(), MovieListConstract.View { ....... override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { rootView = inflater.inflate(R.layout.fragment_movielist, container, false) return rootView } ....... /** * 加载数据 */ override fun loadMovie(list: List<MovieList.Movie>) { var recyclerView = rootView.movieList_recyclverView //引用控件 recyclerView.layoutManager = LinearLayoutManager(activity) recyclerView.adapter = MovieListAdapter(activity, list) } } ## **Android多渠道** ## 安卓扩展插件现已支持安卓多渠道。具体详情,请阅读[Kotlin Android Extensions原理][Kotlin Android Extensions] ## **Kotlin Android Extensions的原理**: ## > Kotlin 安卓扩展作为 Kotlin 编译器的插件,主要有两大作用: > > 在每一个 KotlinActivity 中添加一个隐藏缓存函数以及一个变量。该方法非常简洁,因此不会直接对APK体积有明显增加。 > 使用函数调用替换每一个合成属性。 > 其工作原理是,当调用合成属性,在模块资源中 Kotlin Activity/Fragment 类作为接收器时,缓存函数将被调用. 具体详情,请阅读[Kotlin Android Extensions原理][Kotlin Android Extensions] [Kotlin_AndroidStudio_3.0_2.x]: http://blog.csdn.net/hexingen/article/details/72621795 [Kotlin_Android_Volley_Gson]: http://blog.csdn.net/hexingen/article/details/72625383 [SouthEast]: /images/20220614/f4c6d2955c664d9aa1e96c32e2b7fcac.png [SouthEast 1]: /images/20220614/dd35436fbb354b61b85d00a794158cfd.png [SouthEast 2]: /images/20220614/61f001d381f34c68baa977d03c72d175.png [Kotlin Android Extensions]: https://www.kotlincn.net/docs/tutorials/android-plugin.html
还没有评论,来说两句吧...