无法在android

时间:2016-01-04 07:38:35

标签: android tabs android-viewpager

这是我目前的情况:  我的活动中有4 Tabs。我正在调用一个新的API来从每个选项卡上的服务器加载数据。问题是正确的片段没有加载到正确的索引上。我用谷歌搜索了herehere,但那里提供的解决方案对我没有帮助。我的代码如下:

  public class OffersTab extends FragmentActivity implements OnTabChangeListener,
    OnPageChangeListener {

private TabsPagerAdapter mAdapter;
private ViewPager mViewPager;
private TabHost mTabHost;
ImageView imv_home;

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

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.offers);

    imv_home = (ImageView) findViewById(R.id.imv_home);
    imv_home.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            finish();
        }
    });
    mViewPager = (ViewPager) findViewById(R.id.viewpager);

    // Tab Initialization
    initialiseTabHost();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
    // Fragments and ViewPager Initialization

    mViewPager.setAdapter(mAdapter);
            mViewPager.setOnPageChangeListener(OffersTab.this);

}

// Method to add a TabHost
private static void AddTab(OffersTab activity, TabHost tabHost,
        TabHost.TabSpec tabSpec) {
    tabSpec.setContent(new MyTabFactory(activity));
    tabHost.addTab(tabSpec);
}

// Manages the Tab changes, synchronizing it with Pages
public void onTabChanged(String tag) {

    int pos = this.mTabHost.getCurrentTab();
    this.mViewPager.setCurrentItem(pos);
}

@Override
public void onPageScrollStateChanged(int arg0) {
}

// Manages the Page changes, synchronizing it with Tabs
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
    int pos = this.mViewPager.getCurrentItem();
    this.mTabHost.setCurrentTab(pos);
}

@Override
public void onPageSelected(int arg0) {
}

// Tabs Creation
private void initialiseTabHost() {
    mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup();

    // TODO Put here your Tabs
    OffersTab.AddTab(this, this.mTabHost, this.mTabHost
            .newTabSpec("Ladies").setIndicator("Ladies"));
    OffersTab.AddTab(this, this.mTabHost, this.mTabHost
            .newTabSpec("Babies").setIndicator("Babies"));
    OffersTab.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Girls")
            .setIndicator("Girls"));
    OffersTab.AddTab(
            this,
            this.mTabHost,
            this.mTabHost.newTabSpec("Accessories").setIndicator(
                    "Accessories"));

    mTabHost.setOnTabChangedListener(this);
}

}

适配器类

  public class TabsPagerAdapter extends FragmentPagerAdapter {

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

@Override
public Fragment getItem( int index) {
    Log.i("index", "" + index);
    switch (index) {
    case 0:
        // Top Rated fragment activity
        return new LadiesFragment();
    case 1:
        // Games fragment activity
        return new BabyFragment();
    case 2:
        // Movies fragment activity
        return new GirlsFragment();
    case 3:
        // Movies fragment activity
        return new AccesoryFragment();
    default:
        break;
    }

    return new LadiesFragment();
}

@Override
public int getCount() {
    // get item count - equal to number of tabs
    return 4;
}

 }

xml layout

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

<LinearLayout
    android:id="@+id/yyy"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:orientation="horizontal" >

    <RelativeLayout
        android:id="@+id/rel_top_header_exp"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:background="@color/orange" >

        <LinearLayout
            android:id="@+id/lnr_top_home"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentRight="true"
            android:gravity=""
            android:weightSum="3" >

            <ImageView
                android:id="@+id/imv_srch_home"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_alignParentLeft="true"
                android:layout_gravity="center_vertical"
                android:layout_marginRight="10dp"
                android:layout_weight="1"
                android:src="@drawable/search_icon" />

            <ImageView
                android:id="@+id/imv_wish_home"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_alignParentLeft="true"
                android:layout_gravity="center_vertical"
                android:layout_marginRight="10dp"
                android:layout_weight="1"
                android:src="@drawable/wish_list_icon" />

            <ImageView
                android:id="@+id/imv_cart_home_exp"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_alignParentLeft="true"
                android:layout_gravity="center_vertical"
                android:layout_marginRight="5dp"
                android:layout_weight="1"
                android:src="@drawable/my_bag_icon" />
        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@+id/imv_home"
            android:text="@string/app_name"
            android:textSize="@dimen/text16" />

        <ImageView
            android:id="@+id/imv_home"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:src="@drawable/back" />
    </RelativeLayout>
</LinearLayout>

<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/yyy" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

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

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

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom" />
    </LinearLayout>
</TabHost>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

在实施TabLayout.OnTabSelectedListener的片段/活动中,请使用:

@Override
public void onTabSelected(TabLayout.Tab tab) {

    pager.setCurrentItem(tab.getPosition());

}