android - 我在导航抽屉内有一个微调器,当我选择一个微调器下拉项时,我正在打开一个活动,该活动有三个 TabLayout
问题描述
当我单击微调器的下拉菜单时,我想打开该活动的特定选项卡,我有三个使用 Fragment 的 Tablaout。[在点击微调器上,我想打开一个活动和相关的 TabLayout,它可能是第二个或第三个 在此处输入图像描述
Spinner sp = (Spinner) navigationView.getMenu().findItem(R.id. shop).getActionView();
sp.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,arraySpinner));
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(position){
case 1:
strs = new Bundle();
strs.putInt("item", position);
strs.putString("Women", parent.getItemAtPosition(position).toString());
intent.putExtras(strs);
intent = new Intent(MainActivity.this, Shop.class);
startActivity(intent);
break;
case 2:
intent = new Intent(MainActivity.this, Designer.class);
startActivity(intent);
break;
case 3:
intent = new Intent(MainActivity.this, Designer.class);
startActivity(intent);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
另一个活动选项卡布局代码是:
Integer item = data.getInt("item");
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
if (tab.getPosition() == 1) {
/* toolbar.setBackgroundColor(ContextCompat.getColor(Shop.this,
R.color.white));*/
tabLayout.setBackgroundColor(ContextCompat.getColor(Shop.this,
R.color.white));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(ContextCompat.getColor(Shop.this,
R.color.white));
}
} else if (tab.getPosition() == 2) {
/* toolbar.setBackgroundColor(ContextCompat.getColor(Shop.this,
android.R.color.white));*/
tabLayout.setBackgroundColor(ContextCompat.getColor(Shop.this,
android.R.color.white));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(ContextCompat.getColor(Shop.this,
android.R.color.white));
}
} else {
/* toolbar.setBackgroundColor(ContextCompat.getColor(Shop.this,
R.color.white));*/
tabLayout.setBackgroundColor(ContextCompat.getColor(Shop.this,
R.color.white));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(ContextCompat.getColor(Shop.this,
R.color.white));
}
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
}
我用于替换浴缸布局的适配器类是:
public class PageAdapter extends FragmentPagerAdapter {
private int numOfTabs;
public PageAdapter(FragmentManager fm, int numOfTabs) {
super(fm);
this.numOfTabs = numOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new WomenFragment();
case 1:
return new MenFragment();
case 2:
return new KidsFragment();
default:
return null;
}
}
@Override
public int getCount() {
return numOfTabs;
}
}
解决方案
尝试这个,
在第一次活动中,
Spinner sp = (Spinner) navigationView.getMenu().findItem(R.id. shop).getActionView();
sp.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,arraySpinner));
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(position){
case 1:
Intent intent = new Intent(MainActivity.this, Shop.class);
intent.putExtra("position",position)
startActivity(intent);
break;
case 2:
Intent intent = new Intent(MainActivity.this, Shop.class);
intent.putExtra("position",position)
startActivity(intent);
break;
case 3:
Intent intent = new Intent(MainActivity.this, Shop.class);
intent.putExtra("position",position)
startActivity(intent);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
在另一个活动中,
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
if (tab.getPosition() == 1) {
/* toolbar.setBackgroundColor(ContextCompat.getColor(Shop.this,
R.color.white));*/
tabLayout.setBackgroundColor(ContextCompat.getColor(Shop.this,
R.color.white));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(ContextCompat.getColor(Shop.this,
R.color.white));
}
} else if (tab.getPosition() == 2) {
/* toolbar.setBackgroundColor(ContextCompat.getColor(Shop.this,
android.R.color.white));*/
tabLayout.setBackgroundColor(ContextCompat.getColor(Shop.this,
android.R.color.white));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(ContextCompat.getColor(Shop.this,
android.R.color.white));
}
} else {
/* toolbar.setBackgroundColor(ContextCompat.getColor(Shop.this,
R.color.white));*/
tabLayout.setBackgroundColor(ContextCompat.getColor(Shop.this,
R.color.white));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(ContextCompat.getColor(Shop.this,
R.color.white));
}
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
int pos = 0;
Intent intent = getIntent();
if(intent!=null){
pos = intent.getStringExtra("position");
}
tabLayout.getTabAt(pos).select();
}
推荐阅读
- python - 试图从古腾堡计划书籍的维基百科中获取书籍摘要
- mongoose - 如何在 Prisma 中添加前/后保存中间件?
- python - CNN ERROR 参数无效:所有标签必须是非负整数,批次:10 个标签:-1、-1、-1、-1、-1、-1
- flutter - 一个使用 TabBarView 的选项卡包含多个方法 [Flutter]
- amazon-web-services - 如何将日志从 VPC A 中的 EC2 推送到 VPC B 中的弹性搜索
- django - 如何使 django 关联引用自身
- c# - 我无法通过 Postman “Autofac.Core.DependencyResolutionException”将图像添加到我的 WebAPI 和 SQL 数据
- mysql - MySQL选择具有一个重复值和一个不同值的记录
- visual-studio - 如何一次通过 Visual Studio Live Share 共享多个 .cpp 和 .h 文件?
- orbeon - orbeon 客户端 ajax 调用者发送 /xforms-server POST 时缺少 JSESSIONID