我在Android中玩碎片。
我知道我可以通过使用以下代码更改一个片段:
FragmentManager fragMgr = getSupportFragmentManager();
FragmentTransaction fragTrans = fragMgr.beginTransaction();
MyFragment myFragment = new MyFragment(); //my custom fragment
fragTrans.replace(android.R.id.content, myFragment);
fragTrans.addToBackStack(null);
fragTrans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragTrans.commit();
我的问题是,在Java文件中,如何获得当前显示的片段实例?
如果你有嵌套片段,如viewpagers内部viewpagers等
你想要得到所有嵌套的片段。
感谢Matt Mombrea的好意,回答一个稍微调整的版本。
private List<Fragment> getVisibleFragments(List<Fragment> searchFragments, List<Fragment> visibleFragments) {
if (searchFragments != null && searchFragments.size() > 0) {
for (Fragment fragment : searchFragments) {
List<Fragment> nestedFragments = new ArrayList<>();
List<Fragment> childFMFragments = fragment.getChildFragmentManager().getFragments();
List<Fragment> fmFragments = fragment.getFragmentManager().getFragments();
fmFragments.retainAll(childFMFragments);
nestedFragments.addAll(childFMFragments);
nestedFragments.addAll(fmFragments);
getVisibleFragments(nestedFragments, visibleFragments);
if (fragment != null && fragment.isVisible()) {
visibleFragments.add(fragment);
}
}
}
return visibleFragments;
}
用法如下:
List<Fragment> allVisibleFragments = getVisibleFragments(searchFragments, visibleFragments);
例如:
List<Fragment> visibleFragments = new ArrayList<>();
List<Fragment> searchFragments = MainActivity.this.getSupportFragmentManager().getFragments();
Toast.makeText(this, ""+getVisibleFragments(searchFragments, visibleFragments), Toast.LENGTH_LONG).show();
在滚动片段的情况下,当你使用ViewPager类的实例时,假设mVeiwPager,你可以调用mviewpage . getcurrentitem()来获取当前片段int数。
在MainLayout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context="unidesign.photo360.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="false"
android:theme="@style/AppTheme.AppBarOverlay"
app:expanded="false">
<android.support.v7.widget.Toolbar
android:id="@+id/main_activity_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:title="@string/app_name">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
</android.support.design.widget.CoordinatorLayout>
在MainActivity.kt
class MainActivity : AppCompatActivity() {
lateinit var mViewPager: ViewPager
lateinit var pageAdapter: PageAdapter
// ...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
pageAdapter = PageAdapter(supportFragmentManager)
mViewPager = findViewById(R.id.view_pager)
// ...
}
override fun onResume() {
super.onResume()
var currentFragment = pageAdapter.getItem(mViewPager.currentItem)
// ...
}