MainActivity是一个带有5个片段的操作栏tabswipe片段活动。如何在不影响其他选项卡图标的情况下更改特定选项卡图标,例如在第3位置处的图标,同时滑动到位置3。 tabs.setIcon()工作,但在滑动到其他位置时,选项卡图标将更改为上一个。有没有办法只从片段中更改特定的选项卡图标。
public class MainActivity extends android.support.v4.app.FragmentActivity
implements ActionBar.TabListener, OnClickListener {
public static ActionBar actionBar;
// Titre des tabs
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Crashlytics.start(this);
setContentView(R.layout.activity_main);
context = this;
customActionBar();
View tabView = this.getLayoutInflater().inflate(R.layout.tabview, null);
TextView tabText = (TextView) tabView.findViewById(R.id.tabText);
//tabText.setText("Home"); tabText.setText("Home");
ImageView tabImage = (ImageView) tabView.findViewById(R.id.tabIcon);
tabImage.setImageDrawable(this.getResources().getDrawable(
R.drawable.home));
// Images des onglets
actionBar.addTab(actionBar.newTab().setCustomView(tabView)
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.offer)
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.plumeactionbar)
.setTabListener(this));
if (CommonUtilities.pref.GetPreferences("hasFavoris").equalsIgnoreCase("true")){
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.bookmark_yellow)
.setTabListener(this));
} else {
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.bookmark)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
// System.out.println("onpageselected");
if (position == 1 && TabsPagerAdapter.getPage() == 4) {
actionBar.setSelectedNavigationItem(position);
} else {
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setSelectedNavigationItem(position);
}
if (position == 0) {
CommonUtilities.offset = 0;
setOnBackPressedListener(null);
}
System.out.println("ITEM_POSITION_THREE_NOTCALLED: "+position);
if (position==3){
//System.out.println("ITEM_POSITION_THREE_CALLED: "+position);
// MyAnnoncesFragment myAnnoncesFragment = new MyAnnoncesFragment();
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
Intent i = new Intent("TAG_REFRESH");
lbm.sendBroadcast(i);
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null && AddOffersFragment.edit_AddTitre != null)
imm.hideSoftInputFromWindow(
AddOffersFragment.edit_AddTitre.getWindowToken(), 0);
InputMethodManager imm2 = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm2 != null && NotLoggedFragment3.edit_pwd != null)
imm2.hideSoftInputFromWindow(
NotLoggedFragment3.edit_pwd.getWindowToken(), 0);
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
@Override
public void onTabReselected(Tab arg0, android.app.FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@SuppressWarnings("deprecation")
@Override
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
mtab=tab;
if (hasFavoris){
if (tab.getPosition()==3){
tab.setIcon(R.drawable.bookmark_yellow);
}
}
if (isLogged){
if (tab.getPosition()==4){
tab.setIcon(R.drawable.user_blue);
}
}
}
@Override
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
}
public void switchToTab(int tab) {
}
public void setOnBackPressedListener(
OnBackPressedListener onBackPressedListener) {
this.onBackPressedListener = onBackPressedListener;
System.out.println("Main Activity : set on back pressed listener");
}
public void onBackPressed() {
if (onBackPressedListener != null) {
System.out.println("Main Activity : Delegation : doback");
onBackPressedListener.doBack();
} else if (viewPager.getCurrentItem() > 0) {
System.out
.println("Main Activity : ViewPager -> setcurrentitem at 0");
viewPager.setCurrentItem(0);
} else {
System.out.println("Main Activity : renvoi au super");
super.onBackPressed();
}
}
@SuppressWarnings("deprecation")
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_search:
switch (TabsPagerAdapter.getPage()) {
case 0:
ResearchFragment.domaine = "Offre";
break;
case 1:
ResearchFragment.domaine = "Trades";
break;
case 2:
ResearchFragment.domaine = "Jobs";
break;
case 3:
ResearchFragment.domaine = "Rencontres";
break;
}
TabsPagerAdapter.setPage(4);
ViewPager mPager = (ViewPager) findViewById(R.id.pager);
mAdapter.startUpdate(mPager);
mAdapter.notifyDataSetChanged();
mAdapter.finishUpdate(mPager);
mPager.setCurrentItem(1);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
return true;
}
return super.onOptionsItemSelected(item);
}
public static void refresh() {
mAdapter.notifyDataSetChanged();
}
@Override
protected void onResume() {
super.onResume();
// playServicesHelper.checkPlayServices();
}
public void customActionBar(){
View customNav = LayoutInflater.from(this).inflate(R.layout.action_bar_custom, null); // layout which contains your button.
imCloseButton =
(ImageView)customNav.findViewById(R.id.imCloseButton);
menuInfoButton =
(ImageView)customNav.findViewById(R.id.menuInfoButton);
imCloseButton.setOnClickListener(this);
menuInfoButton.setOnClickListener(this);
actionBar.setCustomView(customNav);
actionBar.setDisplayShowCustomEnabled(true);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.imCloseButton:
IsTaskIsRunning.renewClass();
finish();
break;
case R.id.menuInfoButton:
Intent i = new Intent(MainActivity.this, InfoFragment.class);
startActivity(i);
break;
}
}
}