我正在开发一个带有片段标签的应用,我想在活动开始时打开另一个标签。如果今天是星期一 - 打开第一个标签,如果今天是星期二打开第二个标签等等我怎么能这样做?
我的fragmentactivity.java:
public class FragmentActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragmentactivity);
sp = PreferenceManager.getDefaultSharedPreferences(this);
String select_class = sp.getString("select_class", "");
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new MondayFragment(), "ПН");
adapter.addFragment(new TuesdayFragment(), "ВТ");
adapter.addFragment(new WednesdayFragment(), "СР");
adapter.addFragment(new ThursdayFragment(), "ЧТ");
adapter.addFragment(new FridayFragment(), "ПТ");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
答案 0 :(得分:3)
首先,使用日历获取星期几:
Calendar c = Calendar.getInstance();
int date = c.get(Calendar.DAY_OF_WEEK);
现在,要确定要打开哪个选项卡,最简单的方法是使用开关。您可以说case Calendar.SUNDAY:
或case 1:
。它们都是相同的,但Calendar.SUNDAY
更容易,因为您不需要知道日期的数量
switch(date){
case Calendar.SUNDAY:
//create tab
break;
case Calendar.MONDAY:
//create tab
break;
and so on...
}
这是获取日期并根据输入创建标签的简单方法。调用此代码的最佳方法是在打开应用程序时调用它,如果您只想调用一次,则应检查它是否已被调用过。为此,请保存检查您之前是否已打开“当日标签”的值。
检查您是否在
之前打开了标签页在撰写此答案时,有4种不同的保存值选项:
日期数字:
从Calendar类复制。无需将这些行添加到您的项目中:
/**
* Value of the {@code DAY_OF_WEEK} field indicating Sunday.
*/
public static final int SUNDAY = 1;
/**
* Value of the {@code DAY_OF_WEEK} field indicating Monday.
*/
public static final int MONDAY = 2;
/**
* Value of the {@code DAY_OF_WEEK} field indicating Tuesday.
*/
public static final int TUESDAY = 3;
/**
* Value of the {@code DAY_OF_WEEK} field indicating Wednesday.
*/
public static final int WEDNESDAY = 4;
/**
* Value of the {@code DAY_OF_WEEK} field indicating Thursday.
*/
public static final int THURSDAY = 5;
/**
* Value of the {@code DAY_OF_WEEK} field indicating Friday.
*/
public static final int FRIDAY = 6;
/**
* Value of the {@code DAY_OF_WEEK} field indicating Saturday.
*/
public static final int SATURDAY = 7;
答案 1 :(得分:0)
尝试viewpager.setCurrentItem(index)
答案 2 :(得分:0)
要查找一周中的当前日期并相应地选择特定标签:
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_WEEK);
switch (day) {
case Calendar.SUNDAY:
viewPager.setCurrentItem(0);
case Calendar.MONDAY:
viewPager.setCurrentItem(1);
case Calendar.TUESDAY:
viewPager.setCurrentItem(2);
...
... //etc
}