在Android9之前使用setUserVisibleHint方法
open class BaseFragment28:Fragment() {
private var isViewCreated = false
private var isUIVisible = false
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
isViewCreated = true
lazyLoad()
}
override fun setUserVisibleHint(isVisibleToUser: Boolean) {
super.setUserVisibleHint(isVisibleToUser)
if (isVisibleToUser){
isUIVisible = true
lazyLoad()
}else{
isUIVisible = false
}
}
private fun lazyLoad(){
if (isUIVisible && isViewCreated){
loadData()
}
}
open fun loadData(){
}
}
class ViewPagerActivity:AppCompatActivity(){
companion object{
fun startAct(context: Context){
context.startActivity(Intent(context, ViewPagerActivity::class.java))
}
}
var fragmentList = ArrayList<Fragment>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activty_viewpager)
for (i in 0 until 4){
val fragment = supportFragmentManager.findFragmentByTag("android:switcher:${R.id.viewPager}$i")
if (fragment == null){
when(i){
0->{
fragmentList.add(Fragment1.newInstance())
}
1->{
fragmentList.add(Fragment2.newInstance())
}
2->{
fragmentList.add(Fragment3.newInstance())
}
3->{
fragmentList.add(Fragment4.newInstance())
}
}
}else{
fragmentList.add(fragment)
}
}
viewPager.adapter = MyViewPagerAdapter(supportFragmentManager)
}
inner class MyViewPagerAdapter(fragmentManager: FragmentManager):FragmentPagerAdapter(fragmentManager){
override fun getItem(position: Int): Fragment {
return fragmentList[position]
}
override fun getCount(): Int {
return fragmentList.size
}
}
}
在Android9之后
open class BaseFragment29:Fragment() {
private var isFirstVisible = true
override fun onResume() {
super.onResume()
if (isFirstVisible){
loadData()
isFirstVisible = false
}
}
open fun loadData(){
}
}
class ViewPagerActivity:AppCompatActivity(){
companion object{
fun startAct(context: Context){
context.startActivity(Intent(context, ViewPagerActivity::class.java))
}
}
var fragmentList = ArrayList<Fragment>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activty_viewpager)
for (i in 0 until 4){
val fragment = supportFragmentManager.findFragmentByTag("android:switcher:${R.id.viewPager}$i")
if (fragment == null){
when(i){
0->{
fragmentList.add(Fragment1.newInstance())
}
1->{
fragmentList.add(Fragment2.newInstance())
}
2->{
fragmentList.add(Fragment3.newInstance())
}
3->{
fragmentList.add(Fragment4.newInstance())
}
}
}else{
fragmentList.add(fragment)
}
}
viewPager.adapter = MyViewPagerAdapter(supportFragmentManager)
}
inner class MyViewPagerAdapter(fragmentManager: FragmentManager):FragmentPagerAdapter(fragmentManager,
BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT){
override fun getItem(position: Int): Fragment {
return fragmentList[position]
}
override fun getCount(): Int {
return fragmentList.size
}
}
}
使用viewPager2
viewPager2加载View
class ViewPager2Adapter(private val fruitList:List<String>): RecyclerView.Adapter<ViewPager2Adapter.ViewHolder>() {
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view){
val name: TextView = view.findViewById(R.id.tv)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.viewpager2_view,parent,false)
return ViewHolder(view)
}
override fun getItemCount(): Int {
return fruitList.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.name.text = fruitList[position]
}
}
class ViewPager2Activity :AppCompatActivity(){
companion object{
fun startAct(context: Context){
context.startActivity(Intent(context, ViewPager2Activity::class.java))
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_viewpager2)
val list = ArrayList<String>()
list.add("banana")
list.add("apply")
list.add("orange")
viewPager2.adapter = ViewPager2Adapter(list)
}
}
viewPager2加载fragment,默认实现了懒加载,不需要设置
class ViewPager2Activity :AppCompatActivity(){
var fragmentList = ArrayList<Fragment>()
companion object{
fun startAct(context: Context){
context.startActivity(Intent(context, ViewPager2Activity::class.java))
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_viewpager2)
fragmentList.add(Fragment1.newInstance())
fragmentList.add(Fragment2.newInstance())
fragmentList.add(Fragment3.newInstance())
fragmentList.add(Fragment4.newInstance())
viewPager1.adapter = ViewPagerFragmentStateAdapter()
//viewPager监听
viewPager1.registerOnPageChangeCallback(object :ViewPager2.OnPageChangeCallback(){
override fun onPageSelected(position: Int) {
super.onPageSelected(position)
Log.e("TAG","$position")
}
})
TabLayoutMediator(tabLayout,viewPager1){ tab, position ->
when(position){
0->{
tab.text = "页面1"
}
1->{
tab.text = "页面2"
}
2->{
tab.text = "页面3"
}
3->{
tab.text = "页面4"
}
}
}.attach()
}
inner class ViewPagerFragmentStateAdapter():FragmentStateAdapter(this){
override fun getItemCount(): Int {
return fragmentList.size
}
override fun createFragment(position: Int): Fragment {
return fragmentList[position]
}
}
}