gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:12:16 26 4
gpt4 key购买 nike

这是我的第一个应用程序。我想知道如何检测用户当前所在的滑动选项卡。我在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com