简体   繁体   中英

TabLayout shows a blank fragment, the second time

In my Android application I'm using a TabLayout with a FragmentStatePagerAdapter to show dynamically generated fragments. It shows correctly the fragments the first time, but when going back to some that are already viewed and are not the adjacent ones it shows a blank page...

I've looked into this and there seems to be two way of fixing this:

  • Using FragmentStatePagerAdapter instead of FragmentPagerAdapter , which I already did and it didn't change anything.

  • Change
    adapter = new FragmentStatePagerAdapter(*getSupportFragmentManager*());
    into:
    adapter = new FragmentStatePagerAdapter(*getChildFragmentManager*());
    that I can't do because I'm in AppCompatActivity and this method is in Fragment...

Is there a way to solve this issue without extending this activity out of FragmentActivity that would screw up other stuff in my project?

Code:

private class SubsPagerAdapter extends FragmentStatePagerAdapter {

    private final ArrayList<Fragment> fragmentsList = new ArrayList<>();
    private final ArrayList<String> titleList = new ArrayList<>();

    SubsPagerAdapter(FragmentManager manager) {
        super(manager);
    }

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

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

    void addFragment(Fragment fragment, String title) {
        fragmentsList.add(fragment);
        titleList.add(title);
    }

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

    @Override
    public Parcelable saveState() {
        return null;
    }
}

Implementation:

subsPager = (ViewPager) findViewById(R.id.subsPager);
                        tabLayout = (TabLayout) findViewById(R.id.tabs_view);

subsPager.setAdapter(adapter);
tabLayout.setupWithViewPager(subsPager)

Edit - Fragment Code:

public class PostsListFragment extends Fragment {

private ArrayList<CustomPost> posts;

private Sorting sorting;

private String name;
private RecyclerView recyclerView;
private SubPostsAdapter adapter;
private LinearLayoutManager linearLayoutManager;
private Fetcher fetcher

public PostsListFragment() {
    this.posts = new ArrayList<>();
}

public static Fragment newInstance(String name) {
    PostsListFragment pf = new PostsListFragment();
    pf.name = name;

    return pf;
}

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    recyclerView = (RecyclerView) inflater.inflate(R.layout.posts_list_holder, container, false);

    //default sorting
    this.sorting = Sorting.HOT;
    this.fetcher = new Fetcher(name);

    loadItems();

    return recyclerView;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
    setHasOptionsMenu(true);
}

private void loadItems() {
    if (posts.size() == 0) {
        new Thread() {
            @Override
            public void run() {
                posts.addAll(fetcher.fetchPosts(sorting));

                new Thread() {
                    @Override
                    public void run() {
                        linearLayoutManager = new LinearLayoutManager(recyclerView.getContext());

                        adapter = new SubPostsAdapter(posts, getActivity());

                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                recyclerView.setLayoutManager(linearLayoutManager);
                                recyclerView.setAdapter(adapter);
                            }
                        });

                    }
                }.start();
            }
        }.start();
    }
}
}

It was more stupid then it looked, removing this line:

 if (posts.size() == 0) {

from loadItems in the Fragment, makes it work as intended. Thanks to cricket_007 for the advices!

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