我有一个带有3个碎片的滑动标签。第一个片段有一个按钮,它应该在同一个滑动标签下打开一个片段。 我有以下观点:
https://drive.google.com/file/d/0B1QZMHJgpviceXlOWWFUdHdnTTQ/edit?usp=sharing
我想从上面按下按钮的最终视图如下:
https://drive.google.com/file/d/0B1QZMHJgpvicZzlSeXhLTFFlNE0/edit?usp=sharing
我尝试使用替换,但片段相互重叠。
片段活性,适配器和片段的代码如下:
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
}
}
适配器
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
Log.d("adapter constructor", "started");
}
@Override
public Fragment getItem(int index) {
Log.d("adapter", "started");
switch (index) {
case 0:
// Top Rated fragment activity
return new Homefragment();
case 1:
// Games fragment activity
return new Recentfragment();
case 2:
// Movies fragment activity
return new Searchfragment();
}
Log.d("adapter", "ended");
return null;
}
@Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
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;
}
}
我有这个片段中的按钮。我想点击按钮打开一个片段。 有什么帮助吗?
答案 0 :(得分:0)
如果没有看到你的代码,我无法明确地说出来,但你可以尝试这样的事情:
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
查看tutorial了解更多信息。