问题
In my app I have navigation drawer with some fragments. When I choose in the drawer I do this code:
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, f, tag);
ft.commit();
in the MainActivity.
Now in one fragment I want to put a TabLayout in the bottom of the screen and I have this troubles:
- is it possible to
replace
from one fragment to another? Where could I putfragmentTransaction
? - if I want to call a third fragment (for example a Send button in one of this tab fragments) I could use the same functions for the tab?
Thank you for the answers
回答1:
You can do like this...
//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);
}
}
/and for your question is it possible to replace from one fragment to another? Where could I put fragmentTransaction? yes do like this
Fragment videoFragment = new VideoPlayerFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.video_fragment, videoFragment).commit();
回答2:
you can do Something like that
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();
you can receive data
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("CID");
return inflater.inflate(R.layout.fragment, container, false);
}
回答3:
If you want to have nested Fragment
s, you need to target minimum API level 17, from which version it is supported. See here.
回答4:
You can create xml like this:
<?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" />
Then you create pager adapter in your fragment:
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);
}
}
}
回答5:
You can try this thing for navigation list..i think is helpful for you
navigation_list is listview on Navigatindrawer
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;
}
}
});
来源:https://stackoverflow.com/questions/36884231/how-to-pass-from-one-fragment-to-another