java - 检测用户所在的滑动选项卡

标签 java android android-layout android-fragments android-studio

这是我的第一个应用程序。我想知道如何检测用户当前所在的滑动选项卡。我在 ViewPagerAdapter 中知道它,但我无能为力,因为它是一个 fragment 。我如何访问这些信息?

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Toolbar mToolbar;

    ViewPager pager;
    ViewPagerAdapter adapter;
    SlidingTabLayout tabs;
    CharSequence Titles[] = {"Factorial", "Permutation", "Random"};
    int Numboftabs = 3;

    FloatingActionButton fab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        // 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.fab_pressed);
            }
        });

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

        fab = (FloatingActionButton) findViewById(R.id.fab);

        fab.show(false);

        //Animation FAB
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.simple_grow);

        fab.startAnimation(animation);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Toast.makeText(this, "Text", Toast.LENGTH_SHORT).show();

            Animation animation = AnimationUtils.loadAnimation(this, R.anim.simple_grow);

            fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.startAnimation(animation);

            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

ViewPagerAdapter.java

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
        {
            FactorialTab factorialTab = new FactorialTab();

            return factorialTab;

        } 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
        {
            PermutationTab permutationTab = new PermutationTab();
            return permutationTab;
        }
        else {
            RandomTab randomTab = new RandomTab();
            return randomTab;
        }
    }

    // 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;
    }
}

RandomTab.java(滑动选项卡之一)

public class RandomTab  extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.tab_random, container, false);
        return v;
    }
}

最佳答案

为 ViewPager 设置 OnPageChangeListener

pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == WHATEVER) {
                    //do what you want

                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

关于java - 检测用户所在的滑动选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32215917/

相关文章:

android - Google Play 计费库 3.0 发布。如何从 2.2.0 迁移,需要进行任何更改吗?

java - 无法在 Android 中使用带有 Intent 的已安装媒体应用程序播放音频文件

android - Android:通过Intent发送ParcelFileDescriptor时发生异常

android - 如何在 Android 中显示顶部栏消息

Java - 根据给定的坐标、方位和距离计算第二个坐标

java - 在照片中查找字符,特别是数字 - 是否有框架?

android - 想要创建一个在特定区域可点击的半 donut 形状按钮

android - CoordinatorLayout 内容子项与 BottomNavigationView 重叠

java - HazelCast TransactionalMap 和并发线程访问

java - 在 OpenGL ES (Android) 中使用带有纹理的索引缓冲区有什么意义吗?