如何onSaveInstanceState一个FragmentPagerAdapter

时间:2019-07-01 09:18:15

标签: android android-recyclerview android-viewpager fragmentpageradapter

所以我有两个片段,两个片段都有RecyclerView,并且都通过FragmentPagerAdapterTabLayout连接。尝试使用onSaveInstanceState时,无法确定要onSaveInstanceState插入哪个片段。这是代码:

public class MainActivity extends AppCompatActivity {

private TabLayout tabLayout;
private ViewPager viewPager;
private ViewPagerAdapter adapter;
private TvShow tvShow = new TvShow();
private FilmShow filmShow = new FilmShow();

public MainActivity() {
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = findViewById(R.id.toolbar);

    // placing toolbar in place of action bar
    setSupportActionBar(toolbar);



    /*Membuat tabLayout*/
    tabLayout = findViewById(R.id.tablayout);
    viewPager = findViewById(R.id.viewpager);

    adapter = new ViewPagerAdapter(getSupportFragmentManager());


    //adding fragment
    adapter.AddFragment(tvShow, "Tv");
    adapter.AddFragment(filmShow, "Film");

    // Adapter Setrup
    viewPager.setAdapter(adapter);

    tabLayout.setupWithViewPager(viewPager);

    if (savedInstanceState != null) {
        getSupportFragmentManager().beginTransaction().replace(R.id.frame_container,tvShow);
        getSupportFragmentManager().beginTransaction().replace(R.id.frame_container,filmShow);
        toolbar.setTitle(adapter.getPageTitle(0));
        toolbar.setTitle(adapter.getPageTitle(1));
    } else {
        tvShow = (TvShow) getSupportFragmentManager().getFragment(savedInstanceState, "Tv");
        filmShow = (FilmShow) getSupportFragmentManager().getFragment(savedInstanceState, "Film");

        getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, tvShow);
        getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, filmShow);

        toolbar.setTitle(adapter.getPageTitle(0));
        toolbar.setTitle(adapter.getPageTitle(1));
    }
}

@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
    outState.putString("Title", String.valueOf(adapter.getPageTitle(0))); //
    outState.putString("Title", String.valueOf(adapter.getPageTitle(1))); //

    getSupportFragmentManager().putFragment(outState, "Tv", tvShow);
    getSupportFragmentManager().putFragment(outState, "Film", filmShow);


    super.onSaveInstanceState(outState, outPersistentState);

}

..........

上面的代码给出空错误

tvShow = (TvShow) getSupportFragmentManager().getFragment(savedInstanceState, "Tv");

编辑

添加了另一个扩展FragmentPagerAdapter的类

    package picodiploma.dicoding.database;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import picodiploma.dicoding.database.picodiploma.dicoding.database.film.FilmShow;
import picodiploma.dicoding.database.picodiploma.dicoding.database.tv.TvShow;

public class ViewPagerAdapter extends FragmentPagerAdapter {

private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> FragmentListTitles = new ArrayList<>();
private TvShow tvShow;
private FilmShow filmShow;

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

@Override
public Fragment getItem(int i) {
    return fragmentList.get(i);
}

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

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

public void AddFragment(Fragment fragment, String title) {
    fragmentList.add(fragment);
    FragmentListTitles.add(title);
}


@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
    Fragment fragment = (Fragment) super.instantiateItem(container, position);
    switch (position) {
        case 0 :
            tvShow = (TvShow) fragment;
            break;
        case 1:
            filmShow = (FilmShow) fragment;
            break;
    }
    return fragment;
}

public class DetailOnPageChangeListener extends ViewPager.SimpleOnPageChangeListener {

    private int currentPage;

    @Override
    public void onPageSelected(int position) {
        currentPage = position;
    }

    public final int getCurrentPage() {
        return currentPage;
    }
}
}

代码可能具有无法解释的逻辑,这只是试图将各个部分放在一起,看看是否有任何连接

使用tabLayout侦听器解决,以获取当前页面

0 个答案:

没有答案