1

我对 ActionBarActivity 和 ViewPager 有疑问。

在我的“主要活动”上,这是一个 ActionBarActivity,我有一些标签,它们是片段。

我想在这些选项卡之间实现“滑动功能”。

我已经阅读了以下教程:http: //developer.android.com/training/implementing-navigation/lateral.html

实际上我已经实现了整个代码,但我的应用程序无法工作。:(

选项卡之间没有“滑动功能”。

这是我的主要活动代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_hauptmenue_extended);

        try {

            actionBar = getSupportActionBar();

            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


            // ViewPager ...
            pagerAdapter = new CollectionPagerAdapter(
                    getSupportFragmentManager());

            viewPager = (ViewPager) findViewById(R.id.viewpager);
            viewPager.setAdapter(pagerAdapter);
            viewPager.setCurrentItem(1);
            viewPager
                    .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                        @Override
                        public void onPageSelected(int position) {
                            actionBar.setSelectedNavigationItem(position);
                        }
                    });
                     // Add all tabs to the actionbar...
                    Tab tabB = actionBar.newTab();
            tabB.setText("Home");
            tabB.setIcon(R.drawable.icon_home);
            tabB.setTabListener(new TabListener<Startmenue_activity>(this,
                    "Start", Startmenue_activity.class, viewPager));
            actionBar.addTab(tabB);

            Tab tabA = actionBar.newTab();
            tabA.setText("");
            tabA.setIcon(R.drawable.icon_nachrichten_sel);
            tabA.setTabListener(new TabListener<Nachrichten_activity>(this,
                    "News", Nachrichten_activity.class, viewPager));
            actionBar.addTab(tabA);

            Tab tabC = actionBar.newTab();
            tabC.setText("");
            tabC.setIcon(R.drawable.icon_favoriten);
            tabC.setTabListener(new TabListener<Favoriten_activity>(this,
                    "Favs", Favoriten_activity.class, viewPager));
            actionBar.addTab(tabC);

我的适配器看起来像这样:

public class CollectionPagerAdapter extends FragmentPagerAdapter {

        final int NUM_ITEMS = 3; // number of tabs

        List<Fragment> fragments = new ArrayList<Fragment>();

        public Fragment getItem(int pos) {
            return fragments.get(pos);
        }

        public void addFragment(Fragment f) {
            fragments.add(f);
        }

        public CollectionPagerAdapter(FragmentManager fm) {
            super(fm);

            Fragment home_Fragment = new Startmenue_activity();
            addFragment(home_Fragment);

            Fragment news_Fragment = new Nachrichten_activity();
            addFragment(news_Fragment);

            Fragment favoriten_Fragment = new Favoriten_activity();
            addFragment(favoriten_Fragment);            

        }

        @Override
        public int getCount() {
            return NUM_ITEMS;
        }

    }

哦,我的主要活动 XML 如下所示:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Hauptmenue_extended" >

    <!-- The ViewPager -->

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <!-- The main content view -->

        <FrameLayout
            android:id="@+id/main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </FrameLayout>

        <!-- The navigation drawer -->

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#fff"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />
    </android.support.v4.view.ViewPager>

</android.support.v4.widget.DrawerLayout>
4

2 回答 2

1

关于您的代码,有很多我不完全理解的事情,例如,您为什么要ListViewViewPager? 中添加一个。无论如何,如果您想使用滑动功能实现一些选项卡。这是怎么做的:

首先是xml:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <android.support.v4.view.PagerTabStrip
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top" />

    </android.support.v4.view.ViewPager>

现在活动:

    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.view.ViewPager;

    public class MainActivity extends FragmentActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ViewPager pager = (ViewPager) findViewById(R.id.pager);
            pager.setAdapter(new SampleAdapter(this, getSupportFragmentManager()));
        }
    }

适配器:

    import android.content.Context;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentStatePagerAdapter;

    public class SampleAdapter extends FragmentStatePagerAdapter {
        Context ctxt = null;

        public SampleAdapter(Context ctxt, FragmentManager mgr) {
            super(mgr);
            this.ctxt = ctxt;
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
            case 0:
                return new SimpleFragment("Hi");
            case 1:
                return new SimpleFragment("There");
            case 2:
                return new SimpleFragment("Fella");
            }
            return null;
        }

        @Override
        public String getPageTitle(int position) {
            switch (position) {
            case 0:
                return "First Tab";
            case 1:
                return "Second Tab";
            case 2:
                return "Third Tab";
            }
            return null;
        }
    }

SimpleFragment

    import android.annotation.SuppressLint;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;

    @SuppressLint("ValidFragment")
    public class SimpleFragment extends Fragment {

        String text;

        public SimpleFragment(String string) {
            text = string;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View root = inflater.inflate(R.layout.fragment, container, false);
            TextView textview = (TextView) root.findViewById(R.id.textView1);
            textview.setText(text);
            return root;
        }

    }

和fragment.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </RelativeLayout>

希望这可以帮助 :)

于 2013-10-13T19:15:04.240 回答
1

试试这个。但我没有测试过。

http://wptrafficanalyzer.in/blog/swipable-navigation-tabs-using-actionbarcompat-library/

于 2013-12-14T13:46:21.153 回答