界面做的比较丑,但是基本上实现了功能。
为了学习最近比较火的 kotlin 语言,就试着写了个 demo。
参考上篇文章仿掌上英雄联盟的效果。
一、开始前的准备
- buildscript {
- ext.kotlin_version = '1.1.2-4'.... //省略部分代码
- }
- }
- dependencies {
- classpath 'com.android.tools.build:gradle:2.3.0'classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
- }
2、model - build.gradle
- apply plugin: 'kotlin-android'apply plugin: 'kotlin-android-extensions'
- dependencies {
- compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"..... //省略部分代码
- }
__需要注意的是如果是自动集成的默认没有
该方法需要手动添加,关于用处后面再说。__
- apply plugin: 'kotlin-android-extensions'
二、加入控件,创建布局
前面说的一个控件就是指 scrollablelayout,在 GitHub 已经开源很久了。
已经提供好了折叠所需要的布局。
- compile 'com.github.cpoopc:scrollablelayoutlib:1.0.1'
Scrollable 需要三个子 view,来分别展示:折叠的部分、标题部分、滚动的部分。
所以布局中大体就是一个标题栏 + 一个 ScrollacleLayout
- <?xml version="1.0" encoding="utf-8" ?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
- android:layout_height="match_parent" android:orientation="vertical" tools:context="com.wapchief.kotlin.MainActivity">
- <!--标题栏-->
- <include layout="@layout/action_bar" />
- <!--包裹三个View-->
- <com.cpoopc.scrollablelayoutlib.ScrollableLayout android:layout_width="match_parent"
- android:layout_height="match_parent" android:background="@android:color/white"
- android:orientation="vertical">
- <!--header-->
- <include layout="@layout/main_header" />
- <!--tab-->
- <include layout="@layout/main_tablayout" />
- <!--footer-->
- <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </com.cpoopc.scrollablelayoutlib.ScrollableLayout>
- </LinearLayout>
这里的布局跟 java 的一样,按正常的方式写就好。主要是在 activity 中的变化。
三、在 activity 中实例化
- import kotlinx.android.synthetic.main.activity_main. * import kotlinx.android.synthetic.main.action_bar. * import kotlinx.android.synthetic.main.main_tablayout. * import kotlinx.android.synthetic.main.main_header. *
这样就直接拿到了所有用到的 view 中的所有控件,
就是实例化 View 中所有的 id, 如果想实例化单个 id,就是
- .*
。 * 在通过
- import kotlinx.android.synthetic.main.main_header.id
实例化的时候,刚开始遇到一个问题就是找不到该方法,后来查到是因为 androidStudio 自动创建的 kotiln 没有
- kotlinx
这一行代码。
- apply plugin: 'kotlin-android-extensions'
- var fragments: MutableList < Fragment > =ArrayList < Fragment > () fragments.add(Fragment1()) fragments.add(Fragment1());
- fragments.add(Fragment1());
和 java 不同的是,kotlin 使用的是 MutableList,并且变量名是在前的,
并且添加 Fragment 的时候也不需要 new Fragment 对象,是不是简洁了很多。
- header_1.visibility=View.GONE
- vp.adapter = viewPagerAdapter(supportFragmentManager, fragments)
viewpager 需要一个适配器,使用内部类创建一个 viewPagerAdapter,
和
- fm
分别为参数,
- list
和
- FragmentManager
为类型,
- List<Fragment>
相当于
- FragmentPagerAdapter
继承了该类。
- extend FragmentPagerAdapter
- //继承 FragmentPagerAdapter 创建适配器
- class viewPagerAdapter(fm: FragmentManager ? ,
- var list: List < Fragment > ) : FragmentPagerAdapter(fm) {
- override fun getItem(position: Int) : Fragment {
- return list.get(position)
- }
- override fun getCount() : Int {
- return list.size
- }
- }
- override fun onPageSelected(position: Int) {
- //判断滑动后选择的页面设置相应的标签被选中
- when(position) {
- 0 - >{
- initTabLayout(tab1_tv, tab1_v) clearTabLayout(tab2_tv, tab2_v) clearTabLayout(tab3_tv, tab3_v)
- }
- 1 - >{
- initTabLayout(tab2_tv, tab2_v) clearTabLayout(tab1_tv, tab1_v) clearTabLayout(tab3_tv, tab3_v)
- }
- 2 - >{
- initTabLayout(tab3_tv, tab3_v) clearTabLayout(tab2_tv, tab2_v) clearTabLayout(tab1_tv, tab1_v)
- }
- }
控件不需要实例化,在方法中定义好控件的类型,需要调用的时候直接传递控件 id,在引用资源 id, 同样的例子:resources 替代了 java 中的 getResources
- /*初始化tab标签*/
- private fun initTabLayout(tv: TextView, tv2: TextView) {
- tv.setTextColor(resources.getColor(R.color.tab_select)) tv2.setBackgroundColor(resources.getColor(R.color.tab_select))
- }
- /*重置tab标签颜色*/
- private fun clearTabLayout(tv: TextView, tv2: TextView) {
- tv.setTextColor(resources.getColor(R.color.tab_clear)) tv2.setBackgroundColor(Color.WHITE)
- }
- tab1.setOnClickListener {
- vp.setCurrentItem(0) initTabLayout(tab1_tv, tab1_v) clearTabLayout(tab2_tv, tab2_v) clearTabLayout(tab3_tv, tab3_v)
- }
四、Fragment 设置假数据
在写 demo 用的大多都是假数据,方便查看效果,都是通过 for 循环实现数据的递增添加
- private fun getData() : List < String > {
- val data = ArrayList < String > () for (i in 0..30) {
- data.add(i, "测试数据:" + i)
- }
- return data
- }
试着用 Kotlin 写了一个小 demo,对比 java 感觉精简是 kotlin 的一大特点,能让开发者节省更多的时间,并且语法也非常好理解。有点与 php 类似。对于 android 开发者来说上手很容易。
相关资源:
- Kotlin 中文文档
- ScrollableLayout 开源库地址
- demo 源码
来源: http://blog.csdn.net/wapchief/article/details/75004577