如何使用底部导航活动更改片段?
2022-09-01 20:13:39
我创建了一个带有“底部导航活动”的新项目:
这是生成的代码:
package com.aaron.waller.mrpolitik;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
}
return true;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
}
如何更改为带有底部栏的新片段?例如,我有3个片段:Fragment1 Fragment2和Fracment3,我想用底部栏中的3个按钮更改为它们。另外,我希望我可以通过左右滑动手指来切换碎片,我该怎么做?