使用父Fragment

时间:2017-01-02 06:07:08

标签: android android-fragments android-viewpager fragmentstatepageradapter

我不知道怎么问这个。我试过了in here,但我想我还不够清楚。所以,我想我只是写了一个小应用程序来描述这种情况。请注意,该应用程序使用谷歌SlidingTabLayout

长话短说,如果我点击Button1,FrameLayout应该包含Fragment1,删除Fragment2(如果存在)。因此,FragmentViewPager也应该作为Fragment2销毁。但是,即便如此,如果我更改设备的方向,我会获得onCreate() FragmentViewPager方法中定义的Toast。

即使暂停/销毁FragmentViewPager,为什么会调用Fragment2 onCreate?当FragmentViewPager被销毁时,Fragment2的吐司是否有可能无法显示?

MainActivity:

package com.abdfahim.testproject;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(onClickListener);

        Button button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(onClickListener);
    }


    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Fragment fragment;
            switch (v.getId()){
                case R.id.button1:
                    fragment = new Fragment1();
                    break;
                case R.id.button2:
                    fragment = new Fragment2();
                    break;
                default:
                    return;
            }
            getFragmentManager().beginTransaction().replace(R.id.frame_container, fragment, v.getTag().toString()).addToBackStack(null).commit();
        }
    };
}

片段1

package com.abdfahim.testproject;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment {

    public Fragment1(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment1,container,false);
        setHasOptionsMenu(true);

        return rootView;
    }
}

Fragment2

package com.abdfahim.testproject;

import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment2 extends Fragment {

    public Fragment2(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment2,container,false);
        setHasOptionsMenu(true);

        CharSequence titles[]= {"Tab A", "Tab B"};

        // Creating The ViewPagerAdapter
        ViewPagerAdapter adapter =  new ViewPagerAdapter(getActivity().getFragmentManager(), titles, titles.length);
        ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager);
        pager.setAdapter(adapter);

        // Assigning the Sliding Tab Layout View
        SlidingTabLayout tabs = (SlidingTabLayout) rootView.findViewById(R.id.tabs);

        // Setting the ViewPager For the SlidingTabsLayout
        tabs.setViewPager(pager);

        return rootView;
    }

    static class ViewPagerAdapter extends FragmentStatePagerAdapter {

        private CharSequence titles[];
        private int numbOfTabs;

        public ViewPagerAdapter(FragmentManager fm, CharSequence mTitles[], int mNumbOfTabs) {
            super(fm);

            this.titles = mTitles;
            this.numbOfTabs = mNumbOfTabs;

        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public Fragment getItem(int position) {
            Bundle bundle = new Bundle();
            bundle.putString("displayText", "Inside Fragment 2, " + titles[position]);
            FragmentViewPager fragment = new FragmentViewPager();
            fragment.setArguments(bundle);
            return fragment;
        }

        // This method return the titles for the Tabs in the Tab Strip

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        // This method return the Number of tabs for the tabs Strip

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

    }
}

FragmentViewPager

package com.abdfahim.testproject;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

public class FragmentViewPager extends Fragment {

    public FragmentViewPager(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_view_pager,container,false);
        setHasOptionsMenu(true);

        Bundle bundle = this.getArguments();
        TextView textView = (TextView) rootView.findViewById(R.id.tabText);
        textView.setText(bundle.getString("displayText"));

        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();
        Toast.makeText(getActivity(), "This is View Pager Fragment", Toast.LENGTH_SHORT).show();
    }
}

2 个答案:

答案 0 :(得分:2)

当你在另一个片段中使用片段时。你使用getChildFragmentManager()而不是getFragmentManager。您可以从onCreateView()或onActivityCreated()

为ViewPager调用setAdapter()

了解更多细节。看看吧

Why it is not possible to use ViewPager within a Fragment? It actually is

答案 1 :(得分:0)

每次点击,OnClickListener都会创建Fragment2的实例,但不会创建Fragment1。这是由于滥用View#getTag

MainActivity#onCreate中,将if定义中的onClickListener更改为此(使用this answer):

switch (v.getId()){
        case R.id.button1:
            fragment = new Fragment1();
            break;
        case R.id.button2:
            fragment = new Fragment2();
            break;
        default:
            return;
    }

在您的代码中,在点击后检查的if中(例如点击button1后),Android会询问v(点击的View)其代码 - 但是没有使用View#setTag设置 - 它返回null,当然不等于为“button1”创建的String对象,因此if恢复为每次else