RadioGroup&ViewPager
既然學(xué)習(xí)了Kotlin,那么就用Kotlin實(shí)現(xiàn)一個(gè)簡單的常見Android APP的布局框架姥闭。
首先在MainActivity布局文件中,添加我們需要用到的的控件
其實(shí)這里的RadioButton中很多相同的屬性我們可以抽出來放在Style中裸准,在真實(shí)的項(xiàng)目開發(fā)中脚乡,我們有必要這么做;真實(shí)項(xiàng)目開發(fā)中這些很多東西很少在布局中心寫死寄雀,這里要注意5寐恕!盒犹!
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/rg_main"
android:id="@+id/vp_main">
</android.support.v4.view.ViewPager>
<RadioGroup
android:layout_width="0dp"
android:layout_height="45dp"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="@id/vp_main"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:id="@+id/rg_main">
<RadioButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:textColor="@drawable/rb_checked"
android:gravity="center"
android:background="#F67"
android:button="@null"
android:layout_weight="1"
android:checked="true"
android:id="@+id/rb_index"
android:textSize="18sp"
android:text="首頁"
/>
<RadioButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#a67"
android:id="@+id/rb_circle"
android:button="@null"
android:textColor="@drawable/rb_checked"
android:textSize="18sp"
android:gravity="center"
android:text="圈子"
/>
<RadioButton
android:id="@+id/rb_my"
android:layout_width="0dp"
android:textSize="18sp"
android:layout_weight="1"
android:background="#888"
android:button="@null"
android:textColor="@drawable/rb_checked"
android:gravity="center"
android:layout_height="match_parent"
android:text="我的"
/>
</RadioGroup>
</android.support.constraint.ConstraintLayout>
創(chuàng)建Fragment懂更,并且直接通過提示把布局創(chuàng)建出來
- 首頁
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.allen.constraintlayouttest01.R
/**
* Created by Sin on 2019/1/10
*/
class IndexFragment:Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
var fmView:View = inflater.inflate(R.layout.fragment_index,container,false)
return fmView
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:text="首頁界面"
android:gravity="center"
android:textSize="30sp"/>
</android.support.constraint.ConstraintLayout>
- 圈子
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.allen.constraintlayouttest01.R
/**
* Created by Sin on 2019/1/10
*/
class CircleFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
var fmView: View = inflater.inflate(R.layout.fragment_circle, container, false)
return fmView
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:text="圈子界面"
android:gravity="center"
android:textSize="30sp"/>
</android.support.constraint.ConstraintLayout>
- 我的
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.allen.constraintlayouttest01.R
/**
* Created by Sin on 2019/1/10
*/
class MyFragment:Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
var fmView:View = inflater.inflate(R.layout.fragment_my,container,false)
return fmView
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:text="我的界面"
android:gravity="center"
android:textSize="30sp"/>
</android.support.constraint.ConstraintLayout>
創(chuàng)建適配器
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentStatePagerAdapter
/**
* Created by Sin on 2019/1/10
*/
class ViewPagerAdapter(var fmlist: List<Fragment>, fm: FragmentManager?) : FragmentStatePagerAdapter(fm) {
override fun getItem(p0: Int) = fmlist[p0]
override fun getCount() = fmlist.size
}
接下來我們就可以直接在MainActivity中使用了
import android.os.Bundle
import android.support.v4.view.ViewPager
import android.support.v7.app.AppCompatActivity
import com.example.allen.constraintlayouttest01.adapter.ViewPagerAdapter
import com.example.allen.constraintlayouttest01.fragment.CircleFragment
import com.example.allen.constraintlayouttest01.fragment.IndexFragment
import com.example.allen.constraintlayouttest01.fragment.MyFragment
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initView()
setFragment()
setEvent()
}
private fun setEvent() {
rg_main.setOnCheckedChangeListener { group, checkedId ->
when (checkedId) {
R.id.rb_index -> vp_main.setCurrentItem(0)
R.id.rb_circle -> vp_main.setCurrentItem(1)
R.id.rb_my -> vp_main.setCurrentItem(2)
}
}
vp_main.setOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {
}
override fun onPageSelected(p0: Int) = rg_main.check(rg_main.getChildAt(p0).id)
override fun onPageScrollStateChanged(p0: Int) {
}
})
}
private fun setFragment() {
val fm1 = IndexFragment()
val fm2 = CircleFragment()
val fm3 = MyFragment()
val list = listOf(fm1, fm2, fm3)
vp_main.adapter = ViewPagerAdapter(list, supportFragmentManager)
}
private fun initView() {
setContentView(R.layout.activity_main)
}
}