为什么我的SlidingTab滚动条颜色不会改变并始终返回位置0

时间:2015-08-14 03:22:08

标签: android

enter image description here此代码有什么问题?单击标签页2后,它会加载数据,下面的滚动条颜色不会改变,并且总是返回标签页1.我该怎么办?我想要的是通过滚动条颜色指示在我点击我想要的页面后选择了哪个选项卡。

以下是代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    titlebar = getIntent().getStringExtra("Branch");
    setTitle( titlebar +" Order" );
    setContentView(R.layout.activity_main);

    // Creating The Toolbar and setting it as the Toolbar for the activity



    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);

    // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
    adapter =  new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);

    // Assigning ViewPager View and setting the adapter
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);


    // Assiging the Sliding Tab Layout View
    tabs = (SlidingTabLayout) findViewById(R.id.tabs);
    tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

    // Setting Custom Color for the Scroll bar indicator of the Tab View
    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
        @Override
        public int getIndicatorColor(int position) {


                return getResources().getColor(R.color.tabsScrollColor);

        }
    });
    // Setting the ViewPager For the SlidingTabsLayout
    tabs.setViewPager(pager);



    pager.setOnPageChangeListener(pagelistener);
    pagelistener.onPageSelected(0);



}


final ViewPager.OnPageChangeListener pagelistener = new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (position==0){

            new LoadProducts().execute();


        }else if(position==1){

            new LoadCart().execute();
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
};

ViewPagerAdapter.java:

package com.example.work.mcoatorderingapp;

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


public class ViewPagerAdapter extends FragmentStatePagerAdapter {

CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created


// Build a Constructor and assign the passed Values to appropriate values in the class
public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb) {
    super(fm);

    this.Titles = mTitles;
    this.NumbOfTabs = mNumbOfTabsumb;

}

//This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int position) {

    if(position == 0) // if the position is 0 we are returning the First tab
    {
        OrderTab ordertab = new OrderTab();
        return ordertab;
    }
    else if(position==1)     // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
    {
        ViewOrderTab viewordertab = new ViewOrderTab();
        return viewordertab;
    }
    else
    {
        OrderStatus orderstatus = new OrderStatus();
        return orderstatus;
    }

}

// This method return the titles for the Tabs in the Tab Strip

@Override
public CharSequence getPageTitle(int position) {
    return Titles[position];
}

// This method return the Number of tabs for the tabs Strip

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


public static interface FirstShot
{
    public void onFirstShot();
}

}

1 个答案:

答案 0 :(得分:0)

问题是tabs.setOnPageChangeListener(pagelistener),而是执行此操作OnPageChangeListener

,不是为ViewPager创建和分配SlidingTabLayout,而是为SlidingTabLayout.java创建并分配一个来监听其事件。

下面是解释该部分的android示例项目中 /** * Set the {@link android.support.v4.view.ViewPager.OnPageChangeListener}. When using {@link SlidingTabLayout} you are * required to set any {@link android.support.v4.view.ViewPager.OnPageChangeListener} through this method. This is so * that the layout can update it's scroll position correctly. * * @see android.support.v4.view.ViewPager#setOnPageChangeListener(android.support.v4.view.ViewPager.OnPageChangeListener) */ public void setOnPageChangeListener(ViewPager.OnPageChangeListener listener) { mViewPagerPageChangeListener = listener; } 的部分。

packetizer::initial_data(...)

不推荐使用setOnPageChangeListener,使用addOnPageChangeListener代替代码中的{。}}。