如何从一个片段传递到另一个片段

时间:2016-04-27 08:23:02

标签: android android-fragments fragment fragmenttransaction

5 个答案:

答案 0 :(得分:2)

你可以这样做......

//when navigation item is selected by user
    @Override
    public void onDrawerItemSelected(View view, int position) {
        displayView(position);
    }

    public void displayView(int position) {
        String title = getString(R.string.app_name);
        switch (position) {
            case 0:
                fragment = new UserProjectListFragment();                    
                break;
            case 1:
                fragment = new ContactUsFragment();
                break;
            case 2:
                fragment = new HelpFragment();
                break;
            case 3:
                //and so on

                break;

            default:
                break;
        }
        if (fragment != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container_body, fragment).addToBackStack("fragBack");
            fragmentTransaction.commit();
            // set the toolbar title
            getSupportActionBar().setTitle(title);
        }
    }

/并且您的问题是否可以从一个片段替换到另一个片段?我在哪里可以放置fragmentTransaction? 是 喜欢这个

Fragment videoFragment = new VideoPlayerFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.video_fragment, videoFragment).commit();

答案 1 :(得分:1)

你可以做那样的事

[{
   "id": "widget-1",
   "width": 200,
   "height": 20,
   "top": 0,
   "left": 0,
   "right": 200,
   "bottom": 0,
}, {
   "id": "widget-2",
   "width": 200,
   "height": 20,
   "top": 0,
   "left": 200,
   "right": 0,
   "bottom": 0,
}]

您可以接收数据

String cid=id.getText().toString();

 Fragment fr=new friendfragment();

 FragmentManager fm=getFragmentManager();

 android.app.FragmentTransaction ft=fm.beginTransaction();

 Bundle args = new Bundle();

 args.putString("CID", cid);

 fr.setArguments(args);

 ft.replace(R.id.content_frame, fr);

 ft.commit(); 

答案 2 :(得分:0)

答案 3 :(得分:0)

您可以像这样创建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">

<android.support.design.widget.TabLayout
    android:id="@+id/pagerTabStrip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    app:tabIndicatorColor="?android:attr/textColorPrimary"
    app:tabIndicatorHeight="5dp"
    app:tabSelectedTextColor="?android:attr/textColorPrimary"
    app:tabTextColor="?android:attr/textColorPrimary" />

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

然后在片段中创建寻呼机适配器:

PagerAdapter adapter = new PagerAdapter(getChildFragmentManager());
    mPagerTabStrip.setTabsFromPagerAdapter(adapter);
    mPagerTabStrip.setOnTabSelectedListener(new  TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
    mViewPager.setAdapter(adapter);
    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mPagerTabStrip));

private class PagerAdapter extends FragmentPagerAdapter {
    private Map<Integer, String> mFragmentTags;
    private FragmentManager mFragmentManager;

    public PagerAdapter(FragmentManager fm) {
        super(fm);
        this.mFragmentManager = fm;
        mFragmentTags = new HashMap<>();
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = new MyBookFragment();
        Bundle args = new Bundle();
        args.putInt("MyBookFragment", position);
        args.putInt("mSelectedPosition", mSelectedPosition);
        fragment.setArguments(args);
        return fragment;
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Object objects = super.instantiateItem(container, position);
        if (objects instanceof Fragment) {
            Fragment fragment = (Fragment) objects;
            String tag = fragment.getTag();
            mFragmentTags.put(position, tag);
        }
        return objects;
    }

    public Fragment getFragment(int position) {
        String tag = mFragmentTags.get(position);
        if (TextUtils.isEmpty(tag)) {
            return null;
        }
        return mFragmentManager.findFragmentByTag(tag);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return getString(R.string.now);
            default:
                return getString(R.string.my_book_reservation);
        }
    }
}

答案 4 :(得分:0)

你可以尝试这个东西用于导航列表..我觉得对你有帮助

navigation_list是Navigatindrawer上的listview

    navigation_list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {



                    switch (position) {
                    case 0:

                        getSupportFragmentManager()
                                .beginTransaction()
                                .replace(R.id.frame_container,
                                        new NewsDateFragment()).commit();
                        mDrawerLayout.closeDrawer(Gravity.LEFT);
                        break;

                    case 1:

                        getSupportFragmentManager()
                                .beginTransaction()
                                .replace(R.id.frame_container,
                                        new NewsDateFragment()).commit();
                        mDrawerLayout.closeDrawer(Gravity.LEFT);
                        break;
                    case 2:

                        getSupportFragmentManager()
                                .beginTransaction()
                                .replace(R.id.frame_container,
                                        new GossipDateFragment()).commit();
                        mDrawerLayout.closeDrawer(Gravity.LEFT);
                        break;
                    case 3:

                        getSupportFragmentManager()
                                .beginTransaction()
                                .replace(R.id.frame_container,
                                        new StroriesFragment()).commit();
                        mDrawerLayout.closeDrawer(Gravity.LEFT);
                        break;

                    case 4:

                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frame_container, new LearnFragment())
                                .commit();
                        mDrawerLayout.closeDrawer(Gravity.LEFT);

                        break;

                    case 5:

                        getSupportFragmentManager()
                                .beginTransaction()
                                .replace(R.id.frame_container,
                                        new WordsFocusFragment()).commit();
                        mDrawerLayout.closeDrawer(Gravity.LEFT);
                        break;

                    case 7:

                        getSupportFragmentManager()
                                .beginTransaction()
                                .replace(R.id.frame_container,
                                        new SupportFragment()).commit();
                        mDrawerLayout.closeDrawer(Gravity.LEFT);
                        break;

                    case 8:

                        // user.execute(user_id,intEventid);

                        session.logoutUser();

                        Intent i = new Intent(getApplicationContext(),
                                LoginActivity.class);
                        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(i);




                        mDrawerLayout.closeDrawer(Gravity.LEFT);

                        break;

                    }





        }
    });