如何使用容器片段调用相同片段

时间:2018-08-28 21:56:51

标签: android android-fragments fragment

起初我进行了研究,但没有找到解决这个问题的方法!

我有一个ContainerFragment可以在我的3 Fragment中打开,如下所示?

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

import com.mobileboss.buon.giorno.sena.notte.Gif.activity.MainActivityGif;
import com.mobileboss.buon.giorno.sena.notte.Gif.activity.SearchActivity;
import com.mobileboss.fragments.ThreeFragment;
import com.mobileboss.fragments.TwoFragment;

import java.util.ArrayList;
import java.util.List;


public class ContainerFragment extends Fragment {

    private TabLayoutSetupCallback mToolbarSetupCallback;
    private List<String> mTabNamesList;

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

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

        if (context instanceof MainActivity) {
            mToolbarSetupCallback = (TabLayoutSetupCallback) context;
        } else {
            throw new ClassCastException(context.toString() + " must implement TabLayoutSetupCallback");
        }
    }

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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        android.support.v4.app.Fragment fragment = null;
        fragment = new Galeria();
        android.support.v4.app.Fragment fragment2 = new PageFragment();
        //android.support.v4.app.Fragment fragment3 = new MainActivityGif();

        final String query = "BOm DIA";//textView.getText().toString().trim();

        android.support.v4.app.Fragment fragment3  = new SearchActivity(); // replace your custom fragment class
        Bundle bundle = new Bundle();
        //FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
        //bundle.putString(SearchActivity.KEY_QUERY, query); // use as per your need
        bundle.putString(query, query); // use as per your need
        fragment3.setArguments(bundle);

        View view = inflater.inflate(R.layout.fragment_container, container, false);
        ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewPager);

        ContainerFragment.ItemsPagerAdapter adapter = new ContainerFragment.ItemsPagerAdapter(getChildFragmentManager());
        adapter.addFrag(fragment, "ONE");
        adapter.addFrag(fragment2, "TWO");
        adapter.addFrag(fragment3, "THREE");
        viewPager.setOffscreenPageLimit(2);
        viewPager.setAdapter(adapter);
        mToolbarSetupCallback.setupTabLayout(viewPager);

        return view;
    }


    public static class ItemsPagerAdapter extends FragmentStatePagerAdapter {

        private List<String> mTabs = new ArrayList<>();
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ItemsPagerAdapter(FragmentManager fm) {
            super(fm);

            //mTabs = tabNames;
        }

        @Override
        public Fragment getItem(int position) {
           // return PageFragment.newInstance();
            return mFragmentList.get(position);
        }

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

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

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

        public void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }


    }

    public interface TabLayoutSetupCallback {
        void setupTabLayout(ViewPager viewPager);
    }
}

在我的第三个片段中,当单击屏幕顶部的任何按钮时,如下图所示,我称相同的片段,但作为文本参数传递。

IMG

            Bundle bundle = new Bundle();
            ////bundle.putString(SearchActivity.KEY_QUERY, query); // use as per your need
            bundle.putString(KEY_QUERY, suggestion); // use as per your need

            android.support.v4.app.FragmentTransaction fragmentTransaction =
                    getActivity().getSupportFragmentManager().beginTransaction();

            android.support.v4.app.Fragment fragment3  = new SearchActivity();
            String className = fragment3.getClass().getName();

            FragmentManager fragmentManager = getChildFragmentManager ();
            fragment3.setArguments(bundle);

            //FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container, fragment3 , className);
            fragmentTransaction.addToBackStack(className);
            fragmentTransaction.detach(fragment3);
            fragmentTransaction.attach(fragment3);
            fragmentTransaction.commit();

我的问题是,当我调用相同的Fragment时,它会使用我通过的参数正确加载:

            Bundle bundle = new Bundle();
            bundle.putString(KEY_QUERY, suggestion); 

但是我无法播放TabLayout,PagerView不再起作用,我也无法更改片段。

你能帮我吗? DEMO

0 个答案:

没有答案