问题描述
我有一个带有 TabLayout 的 ViewPager.我通过扩展 FragmentStatePagerAdapter 创建了 ViewPagerAdapter.当我刷 ViewPager 时出现以下错误
I have a ViewPager with a TabLayout. I have created ViewPagerAdapter by extending FragmentStatePagerAdapter. When i swipe ViewPager i got the following error
FATAL EXCEPTION: main
Process: com.anubavam.creatrix, PID: 21388
java.lang.IllegalStateException: Fragment already added: AttendanceViewFragment
这里是 ViewPagerAdapter 类代码
Here is ViewPagerAdapter Class code
private class ViewPagerAdapter extends FragmentStatePagerAdapter {
private List<AttendanceViewFragment> mFragments = new ArrayList<>();
private ArrayList<String> buttons = new ArrayList<>();
ViewPagerAdapter(FragmentManager fm, ArrayList<String> actionButtons) {
super(fm);
this.buttons = actionButtons;
}
void addFragment(AttendanceViewFragment frag) {
mFragments.add(frag);
}
@Override
public Fragment getItem(int position) {
return new AttendanceViewFragment();
}
@Override
public int getCount() {
return mFragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return buttons.get(position);
}
}
这是我将片段添加到 ViewPager 适配器的方式:
This is the way i added Fragments into ViewPager Adapter:
adapter = new ViewPagerAdapter(getSupportFragmentManager(), titles);
for (int i = 0; i < titles.size(); i++) {
//order of the fragment
if (i == 0) {
adapter.addFragment(AttendanceViewFragment.newInstance(studentList.toString(), "T"));
} else if (i == 1) {
adapter.addFragment(AttendanceViewFragment.newInstance(present.toString(), "P"));
} else if (i == 2) {
adapter.addFragment(AttendanceViewFragment.newInstance(absent.toString(), "A"));
} else if (i == 3) {
adapter.addFragment(AttendanceViewFragment.newInstance(late.toString(), "L"));
}
}
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
我尝试使用 FragmentPagerAdapter 而不是 FragmentStatePagerAdapter.但是没有用.建议我解决此问题的任何其他方法.
I tried with FragmentPagerAdapter instead of FragmentStatePagerAdapter. But no use. Suggest me any other approach to solve this issue.
推荐答案
另一方面,您可以通过以下代码使用 viewpager2:
In other hand you can use viewpager2 with this code:
myPagerAdapter.kt
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
class PagerAdapterAccounting internal constructor(fm: FragmentActivity) : FragmentStateAdapter(fm) {
override fun getItemCount(): Int {
return mFragmentList.size
}
override fun createFragment(position: Int): Fragment {
return mFragmentList[position]
}
private val mFragmentList = ArrayList<Fragment>()
private val mFragmentTitleList = ArrayList<String>()
fun addFragment(fragment: Fragment, title: String) {
mFragmentList.add(fragment)
mFragmentTitleList.add(title)
}
fun getTabTitle(position: Int): String {
return mFragmentTitleList[position]
}
}
用户界面:
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabIndicatorColor="@color/colorPrimaryDark"
app:tabIndicatorHeight="2dp"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/colorPrimary" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
及用法:
val fragmentAdapter = PagerAdapterAccounting(this) //must be a fragmentActivity
fragmentAdapter.addFragment(SampleFragment.newInstance(), "theSampleTitle1")
fragmentAdapter.addFragment(SampleFragment2.newInstance(), "theSampleTitle2")
viewPager.adapter = fragmentAdapter
TabLayoutMediator(tabLayout, viewPager,
TabLayoutMediator.TabConfigurationStrategy { tab, position -> tab.text = fragmentAdapter.getTabTitle(position) }).attach()
别忘了添加 appcompatX &材料库:
dont forget to add appcompatX & material lib:
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.2.0-alpha02'
希望能解决您的问题.
这篇关于java.lang.IllegalStateException:已添加片段:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!