-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
weikaiyun
committed
Jan 12, 2021
1 parent
d3376e2
commit c138969
Showing
2 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
app/src/main/java/com/common/weikaiyun/adapter/CustomFragmentPagerAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.common.weikaiyun.adapter | ||
|
||
import android.annotation.SuppressLint | ||
import android.util.SparseArray | ||
import android.view.ViewGroup | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.FragmentManager | ||
import androidx.fragment.app.FragmentPagerAdapter | ||
|
||
/** | ||
* This class is FragmentPagerAdapter where all the fragments are in an array and you can access to them later. | ||
* @property mFragments the list of fragments | ||
* 适用viewpager | ||
*/ | ||
@SuppressLint("WrongConstant") | ||
abstract class CustomFragmentPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) { | ||
|
||
val mFragments = SparseArray<Fragment>() | ||
|
||
override fun instantiateItem(container: ViewGroup, position: Int): Any { | ||
val fragment = super.instantiateItem(container, position) as Fragment | ||
mFragments.put(position, fragment) | ||
return fragment | ||
} | ||
|
||
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) { | ||
mFragments.remove(position) | ||
super.destroyItem(container, position, `object`) | ||
} | ||
|
||
/** | ||
* Returns the instance of the fragment if it is created, null otherwise. | ||
*/ | ||
fun getFragment(position: Int): Fragment? { | ||
return if (mFragments.size() == 0) { | ||
null | ||
} else mFragments.get(position) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters