如果今天是特定的一天,如何打开另一个选项卡(片段)?
How to open another tab (fragment) if today a specific day?
我正在开发一个带有片段选项卡的应用程序,我想在 activity 启动时打开另一个选项卡。如果今天是星期一 - 打开第一个选项卡,如果今天是星期二打开第二个选项卡等等。我该怎么做?
我的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);
}
}
}
尝试viewpager.setCurrentItem(index)
要查找当前星期几和 select 相应的特定选项卡:
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
}
首先,使用日历获取星期几:
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...
}
这是获取日期并根据输入创建选项卡的简单方法。这段代码的调用最好是在应用打开的时候调用,如果只调用一次就检查之前是否调用过。为此,请保存检查您之前是否打开过 "tab of the day" 的值。
正在检查您之前是否打开过标签页
在撰写此答案时,有 4 种不同的保存值的选项:
天数:
从日历中复制class。无需将这些行添加到您的项目中:
/**
* 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;
我正在开发一个带有片段选项卡的应用程序,我想在 activity 启动时打开另一个选项卡。如果今天是星期一 - 打开第一个选项卡,如果今天是星期二打开第二个选项卡等等。我该怎么做?
我的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);
}
}
}
尝试viewpager.setCurrentItem(index)
要查找当前星期几和 select 相应的特定选项卡:
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
}
首先,使用日历获取星期几:
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...
}
这是获取日期并根据输入创建选项卡的简单方法。这段代码的调用最好是在应用打开的时候调用,如果只调用一次就检查之前是否调用过。为此,请保存检查您之前是否打开过 "tab of the day" 的值。
正在检查您之前是否打开过标签页
在撰写此答案时,有 4 种不同的保存值的选项:
天数:
从日历中复制class。无需将这些行添加到您的项目中:
/**
* 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;