获取 Tabbed 中当前 Tab 的编号 Activity
Getting the number of the Current Tab in Tabbed Activity
首先,我是 Android 开发的新手,非常感谢您的帮助。
我在 android 工作室中有一个选项卡式 activity,我有一个 FloatingActionButton 可以在选项卡式 activity 的每个片段中添加一个项目。这个按钮然后调用一个新的 Activty,我想传递给新的 activity 选项卡编号,以便我可以将它存储在数据库中。
如何获取生成新 activity 的标签号?
代码如下:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_portal);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new
TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new
TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent startNewPostActivity = new Intent(getApplicationContext(), NewPostActivity.class);
startNewPostActivity.putExtra("Fragment_Position", position_fragment);
startActivity(startNewPostActivity);
}
});
}
为了获得职位,我做了以下工作:
设置 position_fragment = 位置;
但是这个 returns 错误的标签号。
public class SectionsPagerAdapter 扩展了 FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
position_fragment = position; // not good
return PlaceholderFragment.newInstance(position);
}
@Override
public int getCount() {
// Show 5 total pages.
return 5;
}
}
}
试试这个:
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent startNewPostActivity = new Intent(getApplicationContext(), NewPostActivity.class);
startNewPostActivity.putExtra("Fragment_Position", mViewPager.getCurrentItem());
startActivity(startNewPostActivity);
}
});
有两种方法可以在 Tabbed 中获取当前 Tab 的编号 Activity
1.使用ViewPager.getCurrentItem()
Returns the number of pages that will be retained to either side of the current page in the view hierarchy in an idle state. Defaults to 1.
示例代码
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent startNewPostActivity = new Intent(getApplicationContext(), NewPostActivity.class);
startNewPostActivity.putExtra("Fragment_Position", mViewPager.getCurrentItem());
startActivity(startNewPostActivity);
}
2. 使用TabLayout.addOnTabSelectedListener()
Add a TabLayout.OnTabSelectedListener that will be invoked when tab selection changes.
SAMPLE CODE
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
int position = tab.getPosition();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
您可以从这些 switch case 中识别您的选项卡 ID 并在内部调用必要的函数
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());//setting current selected item over viewpager
switch (tab.getPosition()) {
case 0:
//Log.e("TAG","TAB1");
break;
case 1:
//Log.e("TAG","TAB2");
break;
case 2:
//Log.e("TAG","TAB3");
break;
case 3:
//Log.e("TAG","TAB4");
break;
case 4:
//Log.e("TAG","TAB5");
break;
case 5:
//Log.e("TAG","TAB6");
break;
case 6:
//Log.e("TAG","TAB7");
break;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
首先,我是 Android 开发的新手,非常感谢您的帮助。
我在 android 工作室中有一个选项卡式 activity,我有一个 FloatingActionButton 可以在选项卡式 activity 的每个片段中添加一个项目。这个按钮然后调用一个新的 Activty,我想传递给新的 activity 选项卡编号,以便我可以将它存储在数据库中。
如何获取生成新 activity 的标签号?
代码如下:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_portal);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new
TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new
TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent startNewPostActivity = new Intent(getApplicationContext(), NewPostActivity.class);
startNewPostActivity.putExtra("Fragment_Position", position_fragment);
startActivity(startNewPostActivity);
}
});
}
为了获得职位,我做了以下工作: 设置 position_fragment = 位置; 但是这个 returns 错误的标签号。
public class SectionsPagerAdapter 扩展了 FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
position_fragment = position; // not good
return PlaceholderFragment.newInstance(position);
}
@Override
public int getCount() {
// Show 5 total pages.
return 5;
}
}
}
试试这个:
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent startNewPostActivity = new Intent(getApplicationContext(), NewPostActivity.class);
startNewPostActivity.putExtra("Fragment_Position", mViewPager.getCurrentItem());
startActivity(startNewPostActivity);
}
});
有两种方法可以在 Tabbed 中获取当前 Tab 的编号 Activity
1.使用ViewPager.getCurrentItem()
Returns the number of pages that will be retained to either side of the current page in the view hierarchy in an idle state. Defaults to 1.
示例代码
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent startNewPostActivity = new Intent(getApplicationContext(), NewPostActivity.class);
startNewPostActivity.putExtra("Fragment_Position", mViewPager.getCurrentItem());
startActivity(startNewPostActivity);
}
2. 使用TabLayout.addOnTabSelectedListener()
Add a TabLayout.OnTabSelectedListener that will be invoked when tab selection changes. SAMPLE CODE
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
int position = tab.getPosition();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
您可以从这些 switch case 中识别您的选项卡 ID 并在内部调用必要的函数
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());//setting current selected item over viewpager
switch (tab.getPosition()) {
case 0:
//Log.e("TAG","TAB1");
break;
case 1:
//Log.e("TAG","TAB2");
break;
case 2:
//Log.e("TAG","TAB3");
break;
case 3:
//Log.e("TAG","TAB4");
break;
case 4:
//Log.e("TAG","TAB5");
break;
case 5:
//Log.e("TAG","TAB6");
break;
case 6:
//Log.e("TAG","TAB7");
break;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});