programing

FragmentPagerAdapter를 사용하는 ViewPager의 조각은 두 번째로 볼 때 비어 있습니다.

nasanasas 2020. 8. 24. 18:48
반응형

FragmentPagerAdapter를 사용하는 ViewPager의 조각은 두 번째로 볼 때 비어 있습니다.


기본보기에서 다른 조각을 여는 하단에 탭이있는 조각 인터페이스가 있습니다.

항목 목록 인 특정 조각이 하나 있습니다. 사용자가이 목록의 항목 중 하나를 선택하면 이전 조각의 목록에있는 모든 항목 사이를 가로로 스크롤하는 뷰 페이저가 포함 된 다른 조각이 열립니다. 이것은 훌륭하게 작동합니다.

뷰 페이저는 FragmentPagerAdapter를 사용하여 항목을 표시합니다.

문제는 사용자가 목록에서 항목을 선택하고 본 다음 탭 막대의 버튼을 눌러 목록으로 돌아간 다음 다른 항목을 선택할 때 발생합니다. 두 번째로 항목을 선택하면 뷰 페이저 대신 빈 화면이 나타납니다. 이 경우 LogCat에 오류가 발생하지 않습니다.

뷰 페이저가 처음에만 나타나는 이유는 무엇입니까?

FragmentPagerAdapter:

public class ViewPagerAdapter extends FragmentPagerAdapter {
    Cursor mCursor;

    public ViewPagerAdapter(FragmentManager fm, Cursor c) {
        super(fm);
        mCursor = c;
    }

    public void changeCursor(Cursor c) {
        mCursor = c;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mCursor == null) return 0;
        else return mCursor.getCount();
    }

    @Override
    public Fragment getItem(int position) {
        mCursor.moveToPosition(position);
        return TeamCardFragment.newInstance(mCursor, position);
    }
}

PagerFragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Bundle bundle = getArguments();
    mCursorPosition = bundle.getInt(TeamCardCommon.BUNDLE_KEY_CURSOR_POSITION);

    View mView = inflater.inflate(R.layout.team_card_master, container, false);
    mViewPager = (ViewPager)mView.findViewById(R.id.team_card_master_view_pager);

    mAdapter = new ViewPagerAdapter(getFragmentManager(), cursor);
    new setAdapterTask().execute();

    return mView;
}

private class setAdapterTask extends AsyncTask<Void, Void, Void> {
    protected Void doInBackground(Void... params) {
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(mCursorPosition);
    }
}

나는 같은 문제가 있었다. 내 PageAdapter의 부모 클래스를에서 변경하여 "두 번째"표시 문제 android.support.v4.app.FragmentPagerAdapterandroid.support.v4.app.FragmentStatePagerAdapter해결했습니다 ViewPager!


나는 부모 조각에서 로 대체 getFragmentManager()하여 이것을 해결했습니다 getChildFragmentManager(). 이 상위 프래그먼트는 생성자에 프래그먼트 관리자가 필요한 페이징 가능 (슬라이드 가능) 프래그먼트를 포함하기 위해 android.support.v4.app.FragmentPagerAdapter인스턴스화했습니다 . 이 생성자에 반환 값을 전달했습니다 getChildFragmentManager().

hackbod의 링크는 핵심 ( https://developer.android.com/about/versions/android-4.2.html#NestedFragments ) 이었으며이 게시물은 Fragments 내의 Fragments 에서 발견되었습니다.

To nest a fragment, simply call getChildFragmentManager() on the Fragment in which you want to add a fragment. This returns a FragmentManager that you can use like you normally do from the top-level activity to create fragment transactions.


for me i had to call this on my viewpager:

myViewPager.setSaveFromParentEnabled(false);

I had the issue where the viewpager was not refreshing and all i saw was a blank white screen where the fragments should be. I was passing in getChildFragmentManager but it did not help.


In my very particular case, where I was using a CoordinatorLayout with an AppBarLayout and the ViewPager, what solved it for me was removing the android:fitsSystemWindows="true" from my AppBarLayout xml properties.

Don't ask me why. I know it sounds a little bit ridiculous and like it should have no correlation, but it was this single line the only thing that was causing trouble as I was already using getChildFragmentManager() in my adapter. I spent a whole day debugging my code just to find this, so I hope it saves someone else some time.


I had the same issue for which I changed adapter from FragmentPagerAdapter to FragmentStatePagerAdapter and getFragmentManager() in the parent fragment to getChildFragmentManager()

The reason behind this is FragmentStatePagerAdapter is helpful in storing a large number of pages and memory associated with each page visited is less as it keeps only the saved state of the fragment while the page is not visible. This reduces the overhead while switching between the fragments.


This would help you.

    viewPager.setOffscreenPageLimit(position);

Try to put setOffscreenPageLimit (int limit) on parent ViewPager.

mViewPager.setOffscreenPageLimit(totalPages-1);

This worked for me like charm.

In my case I had fragment inside TabLayout with ViewPager.

And another ViewPager inside that fragment. First time all working fine but when I change tab and return back, some of my fragment gone blank.


We got around this by re-implementing the view pager items as standard views rather than fragments and changing the adapter accordingly.


You can also initialize the adapter for cases where you experience this error when your app is minimized and later called up.

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Bundle bundle = getArguments();
    mCursorPosition = bundle.getInt(TeamCardCommon.BUNDLE_KEY_CURSOR_POSITION);
    View mView = inflater.inflate(R.layout.team_card_master, container, false);
    mViewPager = (ViewPager)mView.findViewById(R.id.team_card_master_view_pager);
    initViewPagerAdapter();
    return mView;
}
private void initViewPagerAdapter(){
    mAdapter = new ViewPagerAdapter(getFragmentManager(), cursor);
    new setAdapterTask().execute();
}

@Override
public void onResume(){
    super.onResume();
    initViewPagerAdapter();
}

I had the same thing, in the second time i call the Pager Adapter, the child view return a NullPointerException. And changing the adapder in FragmentStatePagerAdapter solve my issue too.


I was getting this same problem on Xamarin Android with a blank screen the second time around.. Setting the following fixed it for me.

viewPager.SaveFromParentEnabled = false;

Change FragmentPagerAdapter into FragmentStatePagerAdapter Use getChildFragmentManager() instead of getFragmentManager()

getChildFragmentManager() - because according to documentation, it will return a private FragmentManager for placing and managing Fragments inside of this Fragment. Meanwhile getFragmentManager() will return the FragmentManager for interacting with fragments associated with this activity

참고URL : https://stackoverflow.com/questions/7746652/fragment-in-viewpager-using-fragmentpageradapter-is-blank-the-second-time-it-is

반응형