我正在为我的应用实现底部标签,当我在标签之间切换时,标签指示器会滞后(缓慢加载)。我正在使用tablayout定位底部。在这种情况下,我会在每个选项卡上单击以加载四个片段,即您的一天,您的时间,panganga和kundali。加载片段时,每次延迟大约1秒
public class MainPage extends MyActivity {
private static final int LOC_PER = 201;
YourDay yourDay;
YourTime yourTime;
Panchang panchang;
Kundali kundali;
Advice advice;
FrameLayout dummy;
TabLayout tabs;
private static MainPage inst;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adjustFontScale(getResources().getConfiguration());
setContentView(R.layout.main_page);
dummy = findViewById(R.id.mp_dummy);
yourDay = new YourDay();
tabs = findViewById(R.id.mp_tabs);
tabs.setTabIndicatorFullWidth(false);
tabs.addTab(tabs.newTab().setText(getResources().
getString(R.string.yday)).setIcon(R.drawable.your_day_w));
tabs.addTab(tabs.newTab().setText(getResources().
getString(R.string.ytime)).setIcon(R.drawable.your_time_w));
tabs.addTab(tabs.newTab().setText(getResources().
getString(R.string.panchang)).setIcon(R.drawable.panchang_w));
tabs.addTab(tabs.newTab().setText(getResources().
getString(R.string.kundali)).setIcon(R.drawable.kundali_w));
getLocationPermission();
setCurrentTab(tabs.getTabAt(0));
tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
setCurrentTab(tab);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
tab.getIcon().
setColorFilter(ContextCompat.getColor(getApplicationContext(),
R.color.white),
PorterDuff.Mode.SRC_IN);
}
@Override
public void onTabReselected(TabLayout.Tab tab) { }
});
}
public void showAdvice(){
advice = new Advice(MainPage.this,
android.R.style.Theme_Light_NoTitleBar);
advice.show();
}
public void goToProfiles(){
startActivity(new Intent(getApplicationContext(),
SwitchProfile.class));
}
public void setDates(TextView d, TextView my){
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dayFormat = new SimpleDateFormat("EEEE",
Locale.getDefault());
SimpleDateFormat monthFormat = new SimpleDateFormat("MMM",
Locale.getDefault());
final String today = dayFormat.format(calendar.getTime());
d.setText(String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)));
String t = today + "\n"
+ monthFormat.format(calendar.getTime()) + ", " +
String.valueOf(calendar.get(Calendar.YEAR));
my.setText(t);
}
private void setCurrentTab(TabLayout.Tab tab) {
tab.getIcon().
setColorFilter(ContextCompat.getColor(getApplicationContext(),
R.color.black),
PorterDuff.Mode.SRC_IN);
switch (tab.getPosition()) {
case 0:
setFrag(yourDay);
break;
case 1:
if(yourTime == null){
yourTime = new YourTime();
}
setFrag(yourTime);
break;
case 2:
if(panchang == null){
panchang = new Panchang();
}
setFrag(panchang);
break;
case 3:
if(kundali == null){
kundali = new Kundali();
}
setFrag(kundali);
break;
}
}
void setFrag(Fragment fragment) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.mp_dummy, fragment)
.commit();
}
@Override
public void onBackPressed() {
moveTaskToBack(true);
}
void getLocationPermission() {
if (ActivityCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainPage.this, new String[]
{Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION}, LOC_PER);
}}
public static MainPage instance() {
return inst;
}
}
答案 0 :(得分:0)
结结巴结是由于每次切换选项卡时都要更换碎片。
考虑将ViewPager与TabLayout结合使用 视图分页器可以在内存中保留多个片段,因此您可以在它们之间切换而无需加载/卸载片段。
有关如何完成此操作的好例子: https://medium.com/@Abdulkadir98/android-sliding-tabs-with-viewpager-851f9c996cb5