我应该在代码中做出哪些更改,以便根据系统日(即周一,周二......)打开标签?在上一个代码中,我使用了calander类来从系统中获取Day,但是我如何在第一个和第二个代码中实现它,以便在打开应用程序时相应的选项卡(今天是今天的2月8日周日所以星期日应该打开)?
package com.example.timetabletest;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
ActionBar actionbar;
ViewPager viewpager;
FragmentPageAdapter ft;
private String[] tabs = { "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY",
"FRIDAY", "SATURDAY" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager = (ViewPager) findViewById(R.id.pager);
ft = new FragmentPageAdapter(getSupportFragmentManager());
actionbar = getActionBar();
actionbar.setBackgroundDrawable(new ColorDrawable(Color.BLACK));
actionbar.setDisplayShowTitleEnabled(true);
viewpager.setAdapter(ft);
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (String tab_name : tabs) {
actionbar.addTab(actionbar.newTab().setText(tab_name)
.setTabListener(this));
}
viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
actionbar.setSelectedNavigationItem(arg0);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
MenuInflater blowUp = getMenuInflater();
blowUp.inflate(R.menu.activity_main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about:
Intent abt = new Intent(MainActivity.this, AboutUs.class);
startActivity(abt);
break;
}
return false;
}
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction arg1) {
// TODO Auto-generated method stub
viewpager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
}
package com.example.timetabletest;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class FragmentPageAdapter extends FragmentPagerAdapter {
public FragmentPageAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case 0:
return new MondayFragment();
case 1:
return new TuesdayFragment();
case 2:
return new WedFragment();
case 3:
return new ThursdayFragment();
case 4:
return new FridayFragment();
case 5:
return new SatFragment();
default:
break;
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 6;
}
}
Calendar c = Calendar.getInstance();
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
String weekDay = null;
if (Calendar.MONDAY == dayOfWeek) {
weekDay = "monday";
} else if (Calendar.TUESDAY == dayOfWeek) {
weekDay = "tuesday";
} else if (Calendar.WEDNESDAY == dayOfWeek) {
weekDay = "wednesday";
} else if (Calendar.THURSDAY == dayOfWeek) {
weekDay = "thursday";
} else if (Calendar.FRIDAY == dayOfWeek) {
weekDay = "friday";
} else if (Calendar.SATURDAY == dayOfWeek) {
weekDay = "saturday";
} else if (Calendar.SUNDAY == dayOfWeek) {
weekDay = "sunday";
}
答案 0 :(得分:0)
只需使用以下行设置当前标签即可。
viewPager.setCurrentItem(1, true);
//The true is if you want to do a smooth scroll to your page.
//If you don't want to scroll, you can put false.
将此代码放在onCreate方法的底部,它应该可以正常工作。
Calendar c = Calendar.getInstance();
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
if (Calendar.MONDAY == dayOfWeek) {
viewPager.setCurrentItem(0, true);
} else if (Calendar.TUESDAY == dayOfWeek) {
viewPager.setCurrentItem(1, true);
} else if (Calendar.WEDNESDAY == dayOfWeek) {
viewPager.setCurrentItem(2, true);
} else if (Calendar.THURSDAY == dayOfWeek) {
viewPager.setCurrentItem(3, true);
} else if (Calendar.FRIDAY == dayOfWeek) {
viewPager.setCurrentItem(4, true);
} else if (Calendar.SATURDAY == dayOfWeek) {
viewPager.setCurrentItem(5, true);
} else if (Calendar.SUNDAY == dayOfWeek) {
viewPager.setCurrentItem(6, true);
}