java.lang.IllegalStateException:已添加片段:

时间:2017-05-15 07:43:22

标签: android android-fragments android-viewpager

我有一个带TabLayout的ViewPager。我通过扩展FragmentStatePagerAdapter创建了ViewPagerAdapter。当我滑动ViewPager时,我收到以下错误

 FATAL EXCEPTION: main
                                                                   Process: com.anubavam.creatrix, PID: 21388
                                                                   java.lang.IllegalStateException: Fragment already added: AttendanceViewFragment

这是ViewPagerAdapter类代码

 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适配器中的方式:

  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。但没用。建议我解决这个问题的任何其他方法。

2 个答案:

答案 0 :(得分:1)

创建viewPagerAdapter类并粘贴此代码:

public class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

创建适配器后,添加如下片段:

ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneToOneChat(), "Chat");
adapter.addFragment(new GroupChatFragment(), "Group Chat");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);

答案 1 :(得分:0)

另一方面,您可以将viewpager2与以下代码结合使用:

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和材料库:

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.2.0-alpha02'

希望能解决您的问题。