Android 将viewpager添加到选项卡

Android 将viewpager添加到选项卡,android,tabs,Android,Tabs,我这里有带选项卡的代码,当您选择每个选项卡时它都会工作,但我无法滑动以转到下一个选项卡。 每个选项卡都有listview,它有自己的活动。我只想添加滑动手势以转到下一个选项卡。我该怎么做?我真的很感谢你的帮助。提前谢谢你 public class AndroidTabAndListView extends TabActivity { // TabSpec Names private static final String INBOX_SPEC = "Inbox"; pri

我这里有带选项卡的代码,当您选择每个选项卡时它都会工作,但我无法滑动以转到下一个选项卡。
每个选项卡都有listview,它有自己的活动。我只想添加滑动手势以转到下一个选项卡。我该怎么做?我真的很感谢你的帮助。提前谢谢你

public class AndroidTabAndListView extends TabActivity {
    // TabSpec Names
    private static final String INBOX_SPEC = "Inbox";
    private static final String OUTBOX_SPEC = "Outbox";
    private static final String PROFILE_SPEC = "Profile";

    ViewPager pager;



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


           pager = (ViewPager) findViewById(R.id.pager);

        TabHost tabHost = getTabHost();

        // Inbox Tab
        TabSpec inboxSpec = tabHost.newTabSpec(INBOX_SPEC);
        // Tab Icon
        inboxSpec.setIndicator(INBOX_SPEC, getResources().getDrawable(R.drawable.icon_inbox));
        Intent inboxIntent = new Intent(this, InboxActivity.class);
        // Tab Content
        inboxSpec.setContent(inboxIntent);

        // Outbox Tab
        TabSpec outboxSpec = tabHost.newTabSpec(OUTBOX_SPEC);
        outboxSpec.setIndicator(OUTBOX_SPEC, getResources().getDrawable(R.drawable.icon_outbox));
        Intent outboxIntent = new Intent(this, OutboxActivity.class);
        outboxSpec.setContent(outboxIntent);

        // Profile Tab
        TabSpec profileSpec = tabHost.newTabSpec(PROFILE_SPEC);
        profileSpec.setIndicator(PROFILE_SPEC, getResources().getDrawable(R.drawable.icon_profile));
        Intent profileIntent = new Intent(this, ProfileActivity.class);
        profileSpec.setContent(profileIntent);

        // Adding all TabSpec to TabHost
        tabHost.addTab(inboxSpec); // Adding Inbox tab
        tabHost.addTab(outboxSpec); // Adding Outbox tab
        tabHost.addTab(profileSpec); // Adding Profile tab
    }
}
xml


好吧,我发现您没有在xml中使用
查看页面。因为我没有像您这样尝试过,我建议您像下面的例子那样做会容易得多:



仍然没有用,您可以阅读

希望对你有帮助。干杯:)

内页使用片段。 为选项卡按钮创建一个扩展FragmentPagerAdapter的类

例如:

activity_main.xml

<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" />

你能提供你的
layout.xml
代码吗?更新了代码。我只想在下一个选项卡中添加滑动功能。仅此而已。谢谢你的时间。我尝试使用固定选项卡+滑动,但如何在以下内容中使用意图:
intent outboxIntent=newintent(这是OutboxActivity.class);outboxSpec.setContent(outboxIntent)请帮我解决这个问题,我真的很困惑,但我喜欢意图部分,因为我在每个活动中都有3个自定义列表视图,我希望它位于viewholder上,但我也不知道tp如何做到这一点。你能解释一下吗。再次感谢。我添加了代码,但是我在哪里传递意图呢<代码>公共静态类ContentFragment扩展了片段{@Override public View onCreateView(LayoutFlater充气器,ViewGroup容器,Bundle savedInstanceState){View rootView=inflater.inflate(R.layout.main2,容器,false);Bundle args=getArguments();String content=args.getString(“section_content”);((TextView)rootView.findViewById(android.R.id.text1)).setText(content);返回rootView;}
:如何在此处使用自定义适配器转换现有活动??您不能使用intent。只需实例化片段类。片段必须始终嵌入到活动中。AndroidTabAndListView是从FragmentActivity扩展而来的,它将包含您的子片段。您应该将现有活动转换为片段.见我编辑的答案。
<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" />
public class AndroidTabAndListView extends FragmentActivity implements ActionBar.TabListener {

    TabSpecNames mAppSectionsPagerAdapter;
    ViewPager mViewPager;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create the adapter that will return a fragment for each of the three primary sections
        // of the app.
        mAppSectionsPagerAdapter = new TabSpecNames(getSupportFragmentManager());

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();

        // Specify that the Home/Up button should not be enabled, since there is no hierarchical
        // parent.
        actionBar.setHomeButtonEnabled(false);

        // Specify that we will be displaying tabs in the action bar.
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Set up the ViewPager, attaching the adapter and setting up a listener for when the
        // user swipes between sections.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mAppSectionsPagerAdapter);
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                // When swiping between different app sections, select the corresponding tab.
                // We can also use ActionBar.Tab#select() to do this if we have a reference to the
                // Tab.
                actionBar.setSelectedNavigationItem(position);
            }
        });

        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
            // Create a tab with text corresponding to the page title defined by the adapter.
            // Also specify this Activity object, which implements the TabListener interface, as the
            // listener for when this tab is selected.
            actionBar.addTab(
                    actionBar.newTab()
                            .setText(mAppSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this));
        }
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }


    public static class TabSpecNames extends FragmentPagerAdapter {

        public TabSpecNames(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {

            switch (i) {
            case 0:
                return new InboxFragment();
            case 1:
                return new OutboxFragment();
            case 2:
                return new ProfileFragment();
            default:
                break;
            }
            return null;
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            String sections[] = {"Inbox", "Outbox", "Profile"};
            return sections[position];
        }
    }

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

    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:text="Inbox (99)"/>

</LinearLayout>
public class InboxFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, 
            ViewGroup container,
            Bundle savedInstanceState) 
    {

        View rootView = inflater.inflate(R.layout.fragment_inbox, 
                container, false);

        return rootView;
    }
}