ActionBarSherlock,ViewPager,TabsAdapter嵌套的标签片段 [英] ActionBarSherlock, ViewPager, TabsAdapter nested Tab Fragments
问题描述
我实现了ViewPager和TabsAdapter一个ActionBarSherlock。它运作良好,但现在我试图从加载在标签1框架推的另一个片段。
I implemented a ActionBarSherlock with a ViewPager and a TabsAdapter. It works well but now I tried to "push" from a Framework Loaded in "Tab 1" another Fragment.
行为应该是这样的:
- 在我有3页签在我的应用程序,当启动我看到的第一个选项卡,其中一个按钮一个片段是在它
- pressing在第一个选项卡按钮,片段在第一个选项卡应及时更换,另一片段应显示
我想实现这一点,但我有一个黑色的片段更换时。
I tried to implement this, but I've a black Fragment when Replacing.
在code:
FragmentDemoActivity.java
public class FragmentDemoActivity extends SherlockFragmentActivity {
CustomViewPager mViewPager;
TabsAdapter mTabsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
mViewPager = (CustomViewPager)findViewById(R.id.pager);
mViewPager.setSwipingEnabled(false);
mTabsAdapter = new TabsAdapter(this, bar, mViewPager);
mTabsAdapter.addTab(bar.newTab().setText("tab1"),
FragmentA.class, null);
mTabsAdapter.addTab(bar.newTab().setText("tab2"),
FragmentB.class, null);
setTitle(R.string.app_name);
if (savedInstanceState != null) {
bar.setSelectedNavigationItem(savedInstanceState.getInt("tab"));
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tab", getSupportActionBar().getSelectedNavigationIndex());
}
public static class TabsAdapter extends FragmentPagerAdapter
implements ViewPager.OnPageChangeListener, ActionBar.TabListener {
private final Context mContext;
private final ActionBar mBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
static final class TabInfo {
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
}
public TabsAdapter(FragmentActivity activity, ActionBar bar, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mBar = bar;
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(ActionBar.Tab tab, Class<? extends Fragment> clss, Bundle args) {
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mBar.addTab(tab);
notifyDataSetChanged();
}
@Override
public int getCount() {
return mTabs.size();
}
@Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Object tag = tab.getTag();
for (int i=0; i<mTabs.size(); i++) {
if (mTabs.get(i) == tag) {
mViewPager.setCurrentItem(i,false);
}
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
}
FragmentA.java
public class FragmentA extends Fragment {
Button button;
int num;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved)
{
return inflater.inflate(R.layout.frag_a, group, false);
}
@Override
public void onActivityCreated (Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
num = 0;
button = (Button) getActivity().findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentB f1 = new FragmentB();
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.replace(R.id.pager, f1,"newTag");
ft.show(f1);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
});
}
}
我是新的Android的发展,所以我会很高兴,如果有人能够帮助我。我已经研究了很多时间寻找一个解决方案,但结果并不如我所料。
I'm new at Android Development, so I'd be happy if someone is able to help me. I already investigated a lot of time to search a solution but the result wasn't as I expected.
我的当前项目: http://cl.ly/3q1f2t1O2Y3j
推荐答案
我抓到你了!
让我解释一下,你需要把一个ID在你的布局,要更换,我的例子:
Let me explain, you need to put an id on your Layout that you want to replace, my example:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Framelay"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/txtSearchTopic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="TextView" />
</FrameLayout>
所以,我只是把我的:机器人:ID =@ + ID / Framelay 然后,我可以用这个ID来替换为新的片段,我的意思是,这一切的布局里面的内容将被新的片段所取代,因为我叫DetailFragment。
So I just put mine: android:id="@+id/Framelay" Then I can use this Id to replace for the new fragment, what I mean is that all the content inside this Layout will be replaced by the new Fragment, as I called DetailFragment.
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
DetailFragment fragment3 = new DetailFragment();
fragmentTransaction.replace(R.id.Framelay, fragment3);
fragmentTransaction.commit();
这篇关于ActionBarSherlock,ViewPager,TabsAdapter嵌套的标签片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!