将FragmentTabHost与ViewPager一起使用

Ana*_*fam 5 android android-fragments android-viewpager android-fragmentactivity fragment-tab-host

我想创建使用刷卡标签界面FragmentTabHostViewPager.到目前为止,我得到了碎片滑动.唯一发生的事情是碎片能够在标签后面滑动,这些标签被禁用/卡住而不能正常工作.如何使FragmentTabHost选项卡与ViewPager配合使用以在选项卡之间滑动?

我尝试使用没有viewpager的FragmentTabHost,它工作正常,但它只是不使用viewpager.

这是它的样子截图: 在此输入图像描述

主要Java源代码:

import java.util.List;
import java.util.Vector;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.ViewPager;



public class MainActivity extends FragmentActivity {
 private FragmentTabHost mTabHost;
 private ViewPager pager;
 private MyPagerAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);

        pager = (ViewPager) findViewById(R.id.viewpager);                                   
        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this, BatteryFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, DeviceFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, NetworkFragment.class.getName()));         
        adapter = new MyPagerAdapter(getSupportFragmentManager(),       fragments);

        pager.setAdapter(adapter);

        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("battery").setIndicator("Battery",
                getResources().getDrawable(R.drawable.ic_battery_tab)),
                BatteryFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("network").setIndicator("Network",
                getResources().getDrawable(R.drawable.ic_network_tab)),
                   NetworkFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("device").setIndicator("Device",
                getResources().getDrawable(R.drawable.ic_device_tab)),
                DeviceFragment.class, null);




}


public class MyPagerAdapter extends FragmentPagerAdapter {
    private List<Fragment> fragments;

    public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }       

    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub
        return this.fragments.get(arg0);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return this.fragments.size();
    }

}


}
Run Code Online (Sandbox Code Playgroud)

XML代码:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TabWidget
        android:id="@android:id/tabs"            
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"/>       

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0"/>

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dp" />

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

</LinearLayout>

</android.support.v4.app.FragmentTabHost>
Run Code Online (Sandbox Code Playgroud)

kar*_* vs 1

将以下内容添加到您的代码中:

              pager.setOnPageChangeListener(new OnPageChangeListener() {

                @Override
                public void onPageSelected(int arg0) {
                    // TODO Auto-generated method stub
                    mTabHost.setCurrentTab(arg0);
                }

                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onPageScrollStateChanged(int arg0) {
                    // TODO Auto-generated method stub

                }
            });
Run Code Online (Sandbox Code Playgroud)

问题是当您滑动片段时,选项卡无法知道它,通过上面的代码指定,您可以使此工作正常进行。我遇到了同样的问题,但通过此解决了。希望这会有所帮助。