简体   繁体   English

带有操作栏,片段和ViewPager的Android GUI

[英]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... 我已经进行了很长时间的编程,但是刚开始为android开发,至少可以说与c ++完全不同。

Anyway, I'm trying to implement a gui with some tabs, that should be able to change tab on swipe left/right. 无论如何,我正在尝试实现带有某些选项卡的gui,它应该能够在向左/向右滑动时更改选项卡。 The tabs are implemented with actionbar (or ActionBarSherlock to be precise), the swiping with ViewPager from the support lib. 这些选项卡是使用actionbar(或精确地说是ActionBarSherlock)实现的,并通过支持库中的ViewPager进行滑动。 The individual tabs are fragments (SherlockFragment:s that is). 各个选项卡是片段(即SherlockFragment:s)。 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); 如果我取消注释//context.setContentView(pager); in SwipeBar() constructor, the content of the tabs are shown, but then the statusbar is not shown. 在SwipeBar()构造函数中,显示了选项卡的内容,但随后不显示状态栏。 (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 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: 这是我的类,实现actionbar / viewpager / fragments:
SwipeBar.java 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 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 / 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. 上面的代码来自尝试将ViewPager功能添加到我完成的选项卡中。 I started over and implemented a regular ViewPager (like in the various examples out there) and then added an ActionBar. 我重新开始并实现了常规的ViewPager(如那里的各种示例所示),然后添加了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. 现在,实际的片段内容在ViewPager中,并且ActionBar选项卡为空,但通过ViewPager回调进行设置以指示当前片段。

I have still no clue what was wrong with the original attempt though... 我仍然不知道最初尝试有什么问题...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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