原创文章,转载请注明出处:http://blog.csdn.net/liangzp1990520/article/details/78869154
什么是视图滑动?
它类似于桌面和微信的主界面,在不用开启新Activity
的情况下,平滑切换不同的页面。
ViewPager
实现平滑切换的主要容器,它继承于ViewGroup,但是你不能在设计期往里面添加控件,它有自己的运行机制。
Fragment
字面意思是碎片,我们可以理解为Activity
界面的一部分,你就当它是一个Activity
!一个Activity
!一个Activity
!它有自己的生命周期,但他的生命周期又依赖于Activity
。
Adapter
ViewPager
与Fragment
之间的桥梁,ViewPager
就像是歌手,Adapter
就是它的经纪人,负责联系并组织活动,ViewPager
只管唱好歌就行了。
下面开始代码演示滑动视图如何实现
先定义一个主布局
<?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="org.cgt.myapplication.MainActivity">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
没错,演示代码,就是这么简单,然后再定义四个子布局,分别命名为content1,..2,..3,..4
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView1" />
</LinearLayout>
注意修改TextView
的ID和text,好区分开。
然后再定义四个类,为什么是四个,上面说了,你就当它是一个Activity
,自己处理自己的事务。它们继承于Fragment
,名字分别为Fragment1,..2,..3,..4。
package org.cgt.myapplication
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.content1.*
/**
* Created by Beam on 2017/12/22.
* */
class Fragment1: Fragment() {
override fun onCreateView(lf: LayoutInflater?, parent: ViewGroup?, bundle: Bundle?): View {
return lf?.inflate(R.layout.content1, parent, false)!!
}
override fun onViewCreated(view: View?, bundle: Bundle?) {
super.onViewCreated(view, savedInstanceState)
text1.text = javaClass.simpleName
}
}
什么?代码看不懂,不好意思,我用的是Kotlin
,Kotlin
是一门成熟的语言,它完全兼容Java
,没有什么是Java
能做到而Kotlin
做不到的,高效简洁的语法糖让我欲罢不能,是谷歌内定的官方语言,不是打广告啊!不是打广告!不是打广告!
言归正传,Fragment
是一个静态类(注意:android.support.v4.app.Fragment
),它必须重载onCreateView
函数,为其填充视图,就如Activity
的setContentView
,我这里为其设置了content1布局,剩下的三个类为content2、3、4,onViewCreated
不是必须的,但是你要对视图进行操作,比如监听事件之类的,就在这里了,我这里把text1要显示的内容改成了Fragment1,剩下的三个类把text1改成text2、3、4,其它不变。如此,Fragment就定义好了。
回到主Activity
package org.cgt.myapplication
import android.os.Bundle
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
/**
* Created by Beam on 2017/12/22.
* */
class MainActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
vPager.adapter = VPAdapter(supportFragmentManager) /*绑定适配器*/
}
inner class VPAdapter(fm: FragmentManager): FragmentPagerAdapter(fm) {
private val fragments = arrayListOf(Fragment1(), Fragment2(), Fragment3(), Fragment4()) /*把你定义的Fragment加入数组*/
override fun getItem(position: Int) = fragments[position]
override fun getCount() = fragments.size
}
}
我们先定义一个适配器类VPAdapter
(inner:内部的类),它继承于FragmentPagerAdapter,必须要重载两个函数,getCount
和getItem
,getCount
你有几个滑页,getItem
获取滑页,我们定义一数组fragments
,保存刚才定义的Fragment
,你定义了几个加几个,仅三行代码就定义好了,Kotlin
真的好用!然后在Activity
的onCreate
为你的ViewPager
绑定适配器vPager.adapter = VPAdapter(supportFragmentManager)
,OK搞定,一个简单的滑动视图APP就做好了,调试看看吧!
PS:有些东西可以能没写清楚,可以留言交流!本文为原创文章,转载请注明出处!