我有一个带有bottomnavigationview的应用程序,使用它可以在五个不同的片段/页面之间导航。当我切换到其他页面时,应用会等待(即加载页面)0.5-1秒钟,然后显示它。我希望在移动到另一页时,应用程序在前台显示加载屏幕,而在后台加载页面,加载完成后,加载屏幕消失,出现加载的页面了吗?我怎样才能做到这一点?制作加载屏幕不是问题,但是以我想要的方式实现它是。 我的MainActivity.java:
public class MainActivity extends AppCompatActivity {
RelativeLayout btmnavl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new Home()).commit();
LinearLayout team = findViewById(R.id.team_lay);
BottomNavigationView bottomNavigationView = findViewById(R.id.btmnav);
bottomNavigationView.setOnNavigationItemSelectedListener(navlistner);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new Live()).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener navlistner =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()){
case R.id.live:
selectedFragment = new Live();
hide();
break;
case R.id.home:
selectedFragment = new Home();
break;
case R.id.schedule:
selectedFragment = new Schedule();
break;
case R.id.feed:
selectedFragment = new Feed();
break;
case R.id.recent:
selectedFragment = new Recent();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
}
};
private void hide(){
btmnavl = findViewById(R.id.btmnavl);
btmnavl.setVisibility(View.GONE);
}
}
答案 0 :(得分:1)
要添加仅在页面加载时可见的进度条,请将以下内容添加到活动XML:
<RelativeLayout
android:id="@+id/loadingPanel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:elevation="4dp">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:indeterminateTintMode="src_atop"/>
</RelativeLayout>
然后您可以使用loadingPanel = (RelativeLayout) findViewById(R.id.loadingPanel);
并设置可见性:
loadingPanel.setVisibility(View.GONE);
loadingPanel.setVisibility(View.VISIBLE);
根据您的情况,可以将其设置为GONE
之前的.commit()
,因此:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new Live());
loadingPanel.setVisibility(View.GONE);
transaction.commit();
我相信这应该对您有用,只是在加载其他片段时不要忘记将其设置回VISIBLE
,并编辑上述XML以更好地适合您的特定布局。