当我单击微调框的下拉菜单时,我想打开该活动的特定选项卡,我有三个使用Fragment的Tablaout。 [在单击微调器上,我想打开一个活动,并且相关的TabLayout可能是第二个或第三个 enter image description here
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;
}
}
答案 0 :(得分:0)
尝试一下,
在第一次活动中,
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();
}