当我们回来时,视图寻呼机片段状态寻呼机适配器中的白屏? [英] White screen in view pager fragment state pager adapter when we come back?

查看:83
本文介绍了当我们回来时,视图寻呼机片段状态寻呼机适配器中的白屏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用FragmentStatePagerAdapter将视图分页器用于加载片段. 当我第一次来时它将正常工作,但是如果我从寻呼机适配器重定向到其他片段并回来,它将显示空白屏幕.

I have used view pager for load fragments using FragmentStatePagerAdapter. when i will come first time it will working but if i am redirect to other fragment from pager adapter and come back it will display blank screen.

    **fragment_community.xml**

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <include
        android:id="@+id/includeTop"
        layout="@layout/layout_header"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/includeTop"
        android:orientation="vertical"
        >

        <android.support.design.widget.TabLayout
            android:id="@+id/tabAddFriend"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_alignParentTop="true"
            android:background="@drawable/tab_friend_selector"
            android:fadeScrollbars="false"
            app:tabGravity="fill"
            app:tabIndicatorHeight="4dp"
            app:tabMaxWidth="0dp"
            app:tabMode="fixed"
            app:tabSelectedTextColor="@color/colorDarkGray"
            app:tabTextColor="@color/colorGray">
        </android.support.design.widget.TabLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/pagerCommunity"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/tabAddFriend"/>

    </LinearLayout>

</LinearLayout>

    **Community Fragment :**
    import android.os.Bundle;
    import android.support.design.widget.TabLayout;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.content.ContextCompat;
    import android.support.v4.view.ViewPager;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.LinearLayout;    
    import com.****.app.R;
    import com.****.app.adapter.CommunityPagerAdapter;
    import com.****.app.customcontrol.CustomTextViewMedium;
    import com.****.app.customcontrol.CustomTextViewRegular;
    import com.****.app.utils.General;
    import com.****.app.utils.ManageFragment;

    public class CommunityFragment extends Fragment{

        public static final int FOLLOWERS = 0;
        public static final int FOLLOWING = 1;
        public static final int GROUPS = 2;
        public static String LOGTAG = CommunityFragment.class.getSimpleName ();
        // define activity layouts
        LinearLayout linearBack, linearAction;
        ImageView imgBack, imgAction;
        CustomTextViewRegular txtTitle;
        FragmentActivity activity;

        String[] tabList;
        //CustomViewPager pagerCommunity;
        ViewPager pagerCommunity;
        TabLayout tabAddFriend;

        FragmentManager manager;
        CommunityPagerAdapter communityPagerAdapter;
        String operation="";
        public CommunityFragment (){

        }

        @Override
        public void onCreate (Bundle savedInstanceState){
            super.onCreate (savedInstanceState);
            activity = this.getActivity ();
            tabList = activity.getResources ().getStringArray (R.array.communityArray);
             //Bundle bundle=activity.getIntent ().getExtr
            if(getArguments ()!=null){
                operation= getArguments ().getString (General.REQUEST_TYPE);
            }
        }

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

            linearAction = (LinearLayout) view.findViewById (R.id.linearAction);
            linearAction.setVisibility (View.VISIBLE);
            linearBack = (LinearLayout) view.findViewById (R.id.linearBack);
            imgBack = (ImageView) view.findViewById (R.id.imgBack);
            imgAction = (ImageView) view.findViewById (R.id.imgAction);
            imgAction.setImageResource (R.drawable.ic_block);

            txtTitle = (CustomTextViewRegular) view.findViewById (R.id.txtTitle);
            txtTitle.setText (getString (R.string.community));

            tabAddFriend = (TabLayout) view.findViewById (R.id.tabAddFriend);

            pagerCommunity = (ViewPager) view.findViewById (R.id.pagerCommunity);


            linearBack.setOnClickListener (new View.OnClickListener (){
                @Override
                public void onClick (View v){
                    ManageFragment.back (activity);
                }
            });

            imgBack.setOnClickListener (new View.OnClickListener (){
                @Override
                public void onClick (View v){
                    ManageFragment.back (activity);
                }
            });

            pagerCommunity.addOnPageChangeListener (new ViewPager.OnPageChangeListener (){
                @Override
                public void onPageScrolled (int position, float positionOffset, int positionOffsetPixels){

                }

                @Override
                public void onPageSelected (int position){

                    if (position == FOLLOWERS){
                        imgAction.setImageResource (R.drawable.ic_block);
                    } else if (position == FOLLOWING){
                        imgAction.setImageResource (R.drawable.ic_plus);
                    } else if (position == GROUPS){
                        imgAction.setImageResource (R.drawable.ic_plus);
                    }
                    updateCustomTabTextView (position);
                }

                @Override
                public void onPageScrollStateChanged (int state){

                }
            });
            linearAction.setOnClickListener (new View.OnClickListener (){
                @Override
                public void onClick (View v){

                    int position = pagerCommunity.getCurrentItem ();
                    if (position == FOLLOWERS){
                        imgAction.setImageResource (R.drawable.ic_block);
                        ManageFragment.replace (activity, R.id.content_frame, new BlockedUsersFragment (), BlockedUsersFragment.LOGTAG, BlockedUsersFragment.LOGTAG);

                    } else if (position == FOLLOWING){
                        imgAction.setImageResource (R.drawable.ic_plus);
                        ManageFragment.replace (activity, R.id.content_frame, new FollowingAddFragment (), FollowingAddFragment.LOGTAG, FollowingAddFragment.LOGTAG);
                    } else if (position == GROUPS){
                        imgAction.setImageResource (R.drawable.ic_plus);
                        ManageFragment.replace (activity, R.id.content_frame, new NewGroupFragment (), NewGroupFragment.LOGTAG, NewGroupFragment.LOGTAG);
                    }

                }
            });
            setTabLayout ();
            return view;
        }

        private void setTabLayout (){
            manager = activity.getSupportFragmentManager ();
            communityPagerAdapter = new CommunityPagerAdapter (activity, manager);
            pagerCommunity.setAdapter (communityPagerAdapter);
            tabAddFriend.setupWithViewPager (pagerCommunity);
            setCustomTabTextView ();

            if(!operation.equalsIgnoreCase ("")){
                int type=Integer.parseInt (operation);
                if(type==FOLLOWERS){
                    imgAction.setImageResource (R.drawable.ic_block);
                }else if(type==FOLLOWING){
                    imgAction.setImageResource (R.drawable.ic_plus);
                }
                pagerCommunity.setCurrentItem (type);
            }
        }

        public void updateCustomTabTextView (int position){
            for (int i = 0; i < tabAddFriend.getTabCount (); i++){
                View view = tabAddFriend.getTabAt (i).getCustomView ();
                CustomTextViewMedium textTabTitle = (CustomTextViewMedium) view.findViewById (R.id.textTabTitle);
                if (i == position){
                    textTabTitle.setTextColor (ContextCompat.getColor (activity, R.color.colorDarkGray));
                } else{
                    textTabTitle.setTextColor (ContextCompat.getColor (activity, R.color.colorGray));
                }
            }
        }

        private void setCustomTabTextView (){
            for (int i = 0; i < tabAddFriend.getTabCount (); i++){
                TabLayout.Tab tab = tabAddFriend.getTabAt (i);
                View view = LayoutInflater.from (activity).inflate (R.layout.layout_add_friend_tab, null);
                CustomTextViewMedium textTabTitle = (CustomTextViewMedium) view.findViewById (R.id.textTabTitle);
                textTabTitle.setText (tabList[i]);
                textTabTitle.setTextSize (12);
                if (pagerCommunity.getCurrentItem () == i){
                    textTabTitle.setTextColor (ContextCompat.getColor (activity, R.color.colorDarkGray));
                } else{
                    textTabTitle.setTextColor (ContextCompat.getColor (activity, R.color.colorGray));
                }
                tab.setCustomView (view);
            }
        }
    }

    **CommunityPagerAdapter.java**

    import android.app.Activity;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentStatePagerAdapter;

    import com.****.app.R;
    import com.****.app.fragment.ActivityFragment;
    import com.****.app.fragment.FollowingFragment;
    import com.****.app.fragment.GroupFragment;

    public class CommunityPagerAdapter extends FragmentStatePagerAdapter{

        Activity activity;

        public CommunityPagerAdapter (Activity activity, FragmentManager fm){
            super (fm);
            this.activity = activity;
        }

        @Override
        public Fragment getItem (int position){
            switch (position){
                case 0:
                    return new ActivityFragment ();
                case 1:
                    return new FollowingFragment ();
                case 2:
                    return new GroupFragment ();
            }
            return null;
        }

        @Override
        public int getCount (){
            return 3;
        }

        @Override
        public CharSequence getPageTitle (int position){
            String title = " ";
            switch (position){
                case 0:
                    title = activity.getResources ().getString (R.string.followers);
                    break;
                case 1:
                    title = activity.getResources ().getString (R.string.following);
                    break;
                case 2:
                    title = activity.getResources ().getString (R.string.groups);
                    break;
            }
            return title;
        }

    }

推荐答案

为从其他帖子中提供解决方案而投票否决?谢谢!

Down-voted for providing the solution from another post? Thanks!

好的,我再次尝试描述发生了什么以及为什么Fragment没有重新连接:

Alright, I give this another try to describe what happens and why Fragments aren't re-attached:

还原后的Fragment(从又回到弹出堆栈)是完全由View-层次结构而不是弹出状态的View-层次结构重新创建的.一种解决方法是,确保已还原的Fragment s'View被重新附加,并且其中之一是在使用原始代码并进行以下更改的情况下编写FragmentStatePagerAdapter的另一种实现:

The restored Fragment (from going back a.k.a. popping the back-stack) is fully re-created with a View-hierarchy but not the View-hierarchy of the popped state. A workaround for this is to make sure that already restored Fragments' View is re-attached and one of doing it is to write another implementation of FragmentStatePagerAdapter were the original code is used and the following changes are made:

@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
    // If we already have this item instantiated, there is nothing
    // to do.  This can happen when we are restoring the entire pager
    // from its saved state, where the fragment manager has already
    // taken care of restoring the fragments we previously had instantiated.
    if (mFragments.size() > position) {
        Fragment f = mFragments.get(position);
        if (f != null) {
            return f;
        }
    }
...
}

稍作修改为:

@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
    // If we already have this item instantiated, there is nothing
    // to do.  This can happen when we are restoring the entire pager
    // from its saved state, where the fragment manager has already
    // taken care of restoring the fragments we previously had instantiated.
    if (mFragments.size() > position) {
        Fragment f = mFragments.get(position);
        if (f != null) {
            if (mCurTransaction == null) {
                mCurTransaction = mFragmentManager.beginTransaction();
            }

            mCurTransaction.detach(f);
            mCurTransaction.attach(f);

            return f;
        }
    }
...
}

请查看我最近的帖子,了解一个非常相似的场景以及我的解决方法.

Please have a look at my recent post for a very similar scenario and the way I solved it.

https://stackoverflow.com/a/52661538/6391598

这篇关于当我们回来时,视图寻呼机片段状态寻呼机适配器中的白屏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆