简体   繁体   中英

Android gui with actionbar, fragments and viewpager

Hello!

I've been programming for a long time but just started developing for android, which seems to be quite different from c++ to say at least...

Anyway, I'm trying to implement a gui with some tabs, that should be able to change tab on swipe left/right. The tabs are implemented with actionbar (or ActionBarSherlock to be precise), the swiping with ViewPager from the support lib. The individual tabs are fragments (SherlockFragment:s that is). At the bottom there should be a statusbar of some kind just displaying some text.

The problem I'm having is that the tabs are never shown. I see the actionbar on top with logo/title, with a tab list under it. At the bottom of the screen I see the statusbar-text, but nothing else.

If I uncomment the line //context.setContentView(pager); in SwipeBar() constructor, the content of the tabs are shown, but then the statusbar is not shown. (This line feels really wrong to have here anyway but I can't figure out how to get some content in my tabs without it.)

I've tried everything I can think of. I've read everything I could find about it and tried allot of different examples on how to do each functionality by itself. But combining them... I just can't get this to work. I'd really appreciate your help!


My main (activity) class:
Main.java

public class Main extends SherlockFragmentActivity {
private SwipeBar    tabs;

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tabs = new SwipeBar(this, R.id.pager);
    tabs.add(One.class, "one", null);
    tabs.add(Two.class, "two", null);
    tabs.add(Three.class, "three", null);
}
}

This is my class that implements actionbar/viewpager/fragments:
SwipeBar.java

public class SwipeBar extends FragmentPagerAdapter implements OnPageChangeListener,
    TabListener {
private final ActionBar                 bar;
private final SherlockFragmentActivity  ctx;
private final ViewPager                 pager;
private final ArrayList<TabInfo>        tabs;

public SwipeBar(final SherlockFragmentActivity context, final int viewPagerId) {
    super(context.getSupportFragmentManager());
    bar = context.getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    pager = new ViewPager(context);
    pager.setId(viewPagerId);
    pager.setAdapter(this);
    pager.setOnPageChangeListener(this);
    ctx = context;
    // context.setContentView(pager);
    tabs = new ArrayList<TabInfo>();
}

public void add(final Class<?> clss, final int tabHeaderStringId, final Bundle args) {
    final ActionBar.Tab tab = bar.newTab();
    final TabInfo info = new TabInfo(clss, args);
    tab.setTag(info);
    tab.setText(tabHeaderStringId);
    tab.setTabListener(this);
    tabs.add(info);
    bar.addTab(tab);
    notifyDataSetChanged();
}

@Override
public int getCount() {
    return tabs.size();
}

@Override
public Fragment getItem(final int pos) {
    final TabInfo info = tabs.get(pos);
    return Fragment.instantiate(ctx, info.clss().getName(), info.args());
}

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

public void onPageScrollStateChanged(final int arg0) {}

public void onPageSelected(final int pos) {
    bar.setSelectedNavigationItem(pos);
}

public void onTabReselected(final Tab tab, final FragmentTransaction ft) {}

public void onTabSelected(final Tab tab, final FragmentTransaction ft) {
    final TabInfo tag = (TabInfo)tab.getTag();
    for(int i = 0; i < tabs.size(); i++)
        if(tabs.get(i) == tag) pager.setCurrentItem(i);
}

public void onTabUnselected(final Tab tab, final FragmentTransaction ft) {}
}

The main layout:
Main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

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

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="#33FF0000"
    android:gravity="center"
    android:layout_weight="0"
    android:text="statusbar">
</TextView> 

</LinearLayout>

And the code for the three pages:
One/Two/Three.xml/java

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="this is tab one" />
</LinearLayout>

public class One extends SherlockFragment {
@Override
public View onCreateView(final LayoutInflater inflater,
            final ViewGroup container, final Bundle savedInstanceState) {
    return (LinearLayout)inflater.inflate(R.layout.one, container, false);
}
}

Okay, I finally solved it. The above code came from trying to add ViewPager functionality to the tabs I had done. I started over and implemented a regular ViewPager (like in the various examples out there) and then added an ActionBar. The actual fragment content is now in the ViewPager and the ActionBar tabs is empty, but set through the ViewPager callbacks to indicate the current one.

I have still no clue what was wrong with the original attempt though...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM