在单击按钮上从另一个片段调用片段

时间:2014-09-25 17:42:01

标签: android android-fragments

我有一个带有3个碎片的滑动标签。第一个片段有一个按钮,可以在同一个滑动标签下打开一个片段。

我尝试使用替换,但片段相互重叠。

片段活性,适配器和片段的代码如下:

public class Slidingtab extends FragmentActivity implements
ActionBar.TabListener{

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Post", "Recent", "Search" };
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_slidingtab);
    Log.d("Slding tab", "started");
    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener((TabListener) this));
    }

    /**
     * on swiping the viewpager make respective tab selected
     * */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
    Log.d("Slding tab", "ended");
}

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

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
    viewPager.setCurrentItem(tab.getPosition());

}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

}

HomeFragment

public class Homefragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    Log.d("Home fragment", "started");
    View rootView = inflater.inflate(R.layout.activity_homefragment, container, false);
    Log.d("Home fragment", "ended");
    return rootView;
}
}

我有这个片段中的按钮。我想点击按钮打开一个片段。

另外

Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
我尝试过这个。然后这两个片段相互重叠

任何帮助?

2 个答案:

答案 0 :(得分:0)

您必须添加transaction.addToBackStack(null);在提交之前。 它避免重叠碎片。

答案 1 :(得分:0)

FragmentName Name = new FragmentName ();//Name of fragment where you want to go. 

FragmentTransaction fragmentTransaction= 

getActivity().getSupportFragmentManager().beginTransaction();  

fragmentTransaction.replace(R.id.frame, Name);              

fragmentTransaction.commitAllowingStateLoss();