OnClick Fragment to Fragment开关导致空白屏幕

时间:2017-04-23 22:35:46

标签: java android android-fragments fragment fragmenttransaction

当从一个片段切换到另一个片段(在我的应用程序的第二个选项卡中)时,我的第二个片段是空白的。我尝试过这里链接的解决方案,但似乎都没有效果:

Transaction of fragments in android results in blank screen

Android: Getting white screen with fragment transaction

MainActivity:

public class MainActivity extends AppCompatActivity {

    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;

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

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

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter, and add pages.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mSectionsPagerAdapter.addPage(new Received());
        mSectionsPagerAdapter.addPage(new Send());
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_alert_partners, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            if(getArguments().getInt(ARG_SECTION_NUMBER)==1) {
                View rootView = inflater.inflate(R.layout.fragment_received, container, false);
                return rootView;
            }
            else if(getArguments().getInt(ARG_SECTION_NUMBER)==2) {
                View rootView = inflater.inflate(R.layout.fragment_send, container, false);
                return rootView;
            }
            else {//main empty fragment in case of error. Never used in normal behaviour.
                View rootView = inflater.inflate(R.layout.fragment_alert_partners, container, false);
                return rootView;
            }
        }
    }


    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        //Create an Array list that will hold the pages.
        ArrayList<Fragment> pages = new ArrayList<>();

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

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            return pages.get(position);
        }

        //Add a page
        public void addPage(Fragment f) {
            pages.add(f);
        }

        @Override
        public int getCount() {
            // Show 2 total pages.
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Received";
                case 1:
                    return "Send";

            }
            return null;
        }
    }
}

SendFragment:

public class Send extends Fragment {

    private OnFragmentInteractionListener mListener;
    private TextView text1;


    public Send() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_send, container, false);

        //Need getView() for fragment since setContentView must be set first but is not possible in fragment.
        text1 = (TextView)v.findViewById(R.id.textview1);          

        text1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                                FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            Send2 send2 = new Send2();
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.hide(Send.this);
            fragmentTransaction.add(android.R.id.content, send2);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
            }
        });

        return v;
    }

    public interface OnFragmentInteractionListener {

    }
}

Send2Fragment:

public class Send2 extends Fragment {

    private OnFragmentInteractionListener mListener;

    public Send2() {
        // Required empty public constructor
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View v = inflater.inflate(R.layout.fragment_send2, container, false);
        return v;
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {

    }
}

1 个答案:

答案 0 :(得分:1)

必须在我替换的片段的布局代码末尾添加一个framelayout:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/send1frameLayoutId">
</FrameLayout>

然后使用此代码切换到上一个片段的同一选项卡中的片段:

            Fragment send2 = new Send2();
            FragmentManager fragmentManager = getChildFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

            fragmentTransaction.replace(R.id.send1frameLayoutId, send2);

            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();