简体   繁体   中英

How do I dynamically add and delete fragments with viewpager2 and fragmentstateadapter?

so I've been trying to dynamically add and delete fragments from my application for months now. I recently migrated over to viewpager2 and fragmentstateadapter but to no avail. My goal is to delete a fragment at a specific location completely, but have the ability to add a new fragment to the end of the list with no previous data. When I do this however, the application duplicates fragments. Here is my adapter code:

package com.dc.shark_reel_t5.ui.main;

import android.content.Intent;
import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Lifecycle;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.dc.shark_reel_t5.R;

import java.util.ArrayList;

/**
 * A [FragmentPagerAdapter] that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentStateAdapter {

    private FragmentManager mFragmentManager;
    private FragmentTransaction currTransaction = null;
    private int count = 0;
    private ArrayList<String> mTitles = new ArrayList<String>();
    private ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
    private ArrayList<Integer> mFragmentIDs = new ArrayList<Integer>();


    public SectionsPagerAdapter(FragmentManager fm, Lifecycle lc) {
        super(fm, lc);
        mFragmentManager = fm;
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        return mFragments.get(position);
    }

    @Nullable
    public CharSequence getPageTitle(int position) { return (CharSequence) mTitles.get(position); }


    public String getTitle(int position) {
        return mTitles.get(position);
    }

    //Dynamic(runtime) methods:

    //On call: adds a hook to the end of the list in UI(DOES NOT CHANGE BACK END VARIABLES YET)
    public void addHookFrag(){
        mTitles.add("Hook " + (mFragments.size() + 1));
        PlaceholderFragment currFrag = PlaceholderFragment.newInstance(mFragments.size() + 1);
        mFragmentIDs.add(currFrag.getId());
        mFragments.add(currFrag);
        notifyItemInserted(mFragments.size()-1);
    }

    public void delHookFrag(int position){
        mFragments.remove(position);
        mFragmentIDs.remove(position);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mFragments.size();
    }

    @Override
    public long getItemId(int position){
        if(position >= getItemCount() || position < 0)
            return RecyclerView.NO_ID;

        return mFragmentIDs.get(position);
    }

    @Override
    public boolean containsItem(long itemId){
        return mFragmentIDs.contains((int)itemId);
    }

}

Thanks.

I developed a solution. It turns out the ID that is passed to containsItem is from getItemID, so the whole system relies on the programmer to come up with a method for creating unique IDs. I used a counter, because the only thing that should differentiate the fragments is chronological order, which in this case increases linearly. Here are the pertinent attributes of my adapter that helped me:

public class SectionsPagerAdapter extends FragmentStateAdapter {

...

    private int idGen = 0;

    @Override
    public long getItemId(int position) {
        return (long)mFragmentIDs.get(position);
    }

    @Override
    public boolean containsItem(long itemId) {
        return mFragmentIDs.contains((int)itemId);
    }

...

}

I do not show it here, but I manage a list of Fragment IDs. When adding a new fragment, I increment idGen and add the incremented value to the list. When deleting, I remove the ID and the fragment from their respective lists, as well as updating the section number of all effected fragments(easily done with a setter method in your fragment class). Good luck to all with this problem, took me long enough!

The below code has the FragmentStateAdapter. Viewpager2 uses recycler view to reuse the views. You can add a new page to the data list and use notifyDataSetChanged or notifyItemInserted method to add a new page to the viewpager. Likewise notifyItemRemoved can be used to dynamically remove a page from the position and adjust the viewpager

class StoriesPagerAdapter(fragment: Fragment, var dataList:MutableList<Content> = mutableListOf()) : FragmentStateAdapter(fragment) {
override fun getItemCount(): Int {
    return dataList.size
}

override fun createFragment(position: Int): Fragment {
    return StoryViewFragment.newInstance(dataList[position], position)
}

companion object {
    var currentItem = 0
        get() = field
        set(value) {
            field = value
        }
}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM