鱼C论坛

 找回密码
 立即注册
查看: 1596|回复: 0

[技术交流] kotlin for android 之fragment

[复制链接]
发表于 2017-12-4 10:17:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
        如果只要求手机屏幕上一部分改变,用新建activity的方法就不行了,这时就要使用fragment。新建一个项目然后再MainActivity.kt文件旁新建2个fragment,方法是,鼠标右键点目录->new->Fragment->Fragment(Blank),系统会自动生成xml文件跟kt文件,修改BlankFragment.kt文件:
  1. package com.example.xinwei.fragmentkotlin

  2. import android.app.Fragment
  3. import android.os.Bundle
  4. import android.view.LayoutInflater
  5. import android.view.View
  6. import android.view.ViewGroup

  7. class BlankFragment : Fragment() {


  8.     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
  9.                               savedInstanceState: Bundle?): View? {
  10.         return inflater.inflate(R.layout.fragment_blank, container, false)
  11.     }

  12. }
复制代码

修改BlankFragment2.kt文件:
  1. package com.example.xinwei.fragmentkotlin

  2. import android.app.Fragment
  3. import android.os.Bundle
  4. import android.view.LayoutInflater
  5. import android.view.View
  6. import android.view.ViewGroup

  7. class BlankFragment2 : Fragment() {


  8.     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
  9.                               savedInstanceState: Bundle?): View? {
  10.         return inflater.inflate(R.layout.fragment_blank_fragment2, container, false)
  11.     }

  12. }
复制代码

修改fragment_blank.xml
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     tools:context="com.example.xinwei.fragmentkotlin.BlankFragment">

  6.     <!-- TODO: Update blank fragment layout -->
  7.     <TextView
  8.         android:layout_width="match_parent"
  9.         android:layout_height="match_parent"
  10.         android:background="#ffff00"
  11.         android:text="@string/hello_blank_fragment" />

  12. </FrameLayout>
复制代码

修改fragment_blank_fragment2.xml
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     tools:context="com.example.xinwei.fragmentkotlin.BlankFragment2">

  6.     <!-- TODO: Update blank fragment layout -->
  7.     <TextView
  8.         android:layout_width="match_parent"
  9.         android:layout_height="match_parent"
  10.         android:background="#ff0000"
  11.         android:text="@string/hello_blank_fragment" />

  12. </FrameLayout>
复制代码

修改activity_main.xml文件
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     android:orientation="vertical">

  8.     <LinearLayout
  9.         android:id="@+id/idlayout1"
  10.         android:layout_width="match_parent"
  11.         android:layout_height="0dp"
  12.         android:layout_weight="8"
  13.         android:orientation="vertical"/>

  14.     <LinearLayout
  15.         android:layout_width="match_parent"
  16.         android:layout_height="0dp"
  17.         android:layout_weight="1"
  18.         android:orientation="horizontal">

  19.         <Button
  20.             android:id="@+id/idbutton1"
  21.             android:layout_width="0dp"
  22.             android:layout_height="wrap_content"
  23.             android:layout_weight="1"
  24.             android:text="button1" />

  25.         <Button
  26.             android:id="@+id/idbutton2"
  27.             android:layout_width="0dp"
  28.             android:layout_height="wrap_content"
  29.             android:layout_weight="1"
  30.             android:text="button2" />

  31.     </LinearLayout>

  32. </LinearLayout>
复制代码

修改MainActivity.kt
  1. package com.example.xinwei.fragmentkotlin

  2. import android.app.FragmentTransaction
  3. import android.support.v7.app.AppCompatActivity
  4. import android.os.Bundle
  5. import android.view.View
  6. import kotlinx.android.synthetic.main.activity_main.*

  7. class MainActivity : AppCompatActivity(),View.OnClickListener {

  8.     override fun onClick(p0: View?) {
  9.         when(p0?.id){
  10.             idbutton1.id->{
  11.                 var manager=fragmentManager
  12.                 var transaction=manager.beginTransaction()
  13.                 transaction?.replace(R.id.idlayout1,BlankFragment())
  14.                 transaction?.commit()
  15.             }
  16.             idbutton2.id->{
  17.                 var manager=fragmentManager
  18.                 var transaction=manager.beginTransaction()
  19.                 transaction?.replace(R.id.idlayout1,BlankFragment2())
  20.                 transaction?.commit()
  21.             }
  22.         }
  23.     }

  24.     override fun onCreate(savedInstanceState: Bundle?) {
  25.         super.onCreate(savedInstanceState)
  26.         setContentView(R.layout.activity_main)
  27.         idbutton1.setOnClickListener(this)
  28.         idbutton2.setOnClickListener(this)
  29.         var manager=fragmentManager
  30.         var transaction=manager.beginTransaction()
  31.         transaction?.replace(R.id.idlayout1,BlankFragment())
  32.         transaction?.commit()
  33.     }
  34. }
复制代码

效果图为:
jdfw.gif

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 01:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表