我有一个Bottom Navigation View
,我想以这样的方式使用fragment
和Navigation
:每次用户单击navigation
视图的部分时, fragment
应该加载先前的状态,而不是创建新的fragment
。我有一个Frame Layout
,所有fragment
应该在该Frame Layout
中加载或替换。
这是我的代码opublic类MainActivity扩展了AppCompatActivity {
BottomNavigationView navigation;
Home home;
Trending trending;
Upload upload;
ImageView uploadImage;
final public static int UPLOAD_ACTIVITY_CODE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navigation = findViewById(R.id.navigation);
uploadImage = findViewById(R.id.uploadimg);
home = new Home();
trending = new Trending();
upload = new Upload();
fragmentLoad(home);
uploadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), UploadActivity.class);
startActivityForResult(intent,UPLOAD_ACTIVITY_CODE);
}
});
navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.home:
fragmentLoad(home);
break;
case R.id.trending:
fragmentLoad(trending);
break;
case R.id.upload:
fragmentLoad(upload);
break;
}
return true;
}
});
}
public void fragmentLoad(android.support.v4.app.Fragment fragment){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame, fragment);
transaction.commit();
}
public void fragmentLoad(android.support.v4.app.Fragment fragment,Bundle bundle){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction().show(fragment);
fragment.setArguments(bundle);
transaction.replace(R.id.frame, fragment);
transaction.commit();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==UPLOAD_ACTIVITY_CODE && resultCode==UPLOAD_ACTIVITY_CODE && data!=null){
String desc = data.getStringExtra("Desc");
String title= data.getStringExtra("Title");
String photo= data.getStringExtra("PhotoUri");
String duration= data.getStringExtra("duration");
String video= data.getStringExtra("videoUri");
Bundle bundle = new Bundle();
bundle.putString("desc",desc);
bundle.putString("title",title);
bundle.putString("photo",photo);
bundle.putString("duration",duration);
bundle.putString("videoUri",video);
fragmentLoad(upload,bundle);
navigation.setSelectedItemId(R.id.upload);
}
}
}
答案 0 :(得分:2)
您应该从每个片段创建一个实例,然后使用FragmentManager
根据底部导航状态显示它们。您应该在setData
片段类中编写一个名为Upload
的方法来重新初始化其视图。我为这种情况写了一个课程,希望对您有帮助。
public class MainActivity extends AppCompatActivity {
public static final int UPLOAD_ACTIVITY_CODE = 100;
private BottomNavigationView navigation;
private ImageView uploadImage;
private Home home;
private Trending trending;
private Upload upload;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navigation = findViewById(R.id.navigation);
uploadImage = findViewById(R.id.uploadimg);
home = new Home();
trending = new Trending();
upload = new Upload();
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
navigation.getMenu().findItem(R.id.home).setChecked(true);
getSupportFragmentManager().beginTransaction()
.add(R.id.frame, home)
.add(R.id.frame, category)
.add(R.id.frame, search)
.commit();
setTabStateFragment(TabState.HOME);
uploadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), UploadActivity.class);
startActivityForResult(intent, UPLOAD_ACTIVITY_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == UPLOAD_ACTIVITY_CODE && resultCode == UPLOAD_ACTIVITY_CODE && data != null) {
String desc = data.getStringExtra("Desc");
String title = data.getStringExtra("Title");
String photo = data.getStringExtra("PhotoUri");
String duration = data.getStringExtra("duration");
String video = data.getStringExtra("videoUri");
Bundle bundle = new Bundle();
bundle.putString("desc", desc);
bundle.putString("title", title);
bundle.putString("photo", photo);
bundle.putString("duration", duration);
bundle.putString("videoUri", video);
upload.setData(bundle);
setTabStateFragment(TabState.UPLOAD);
}
}
private void setTabStateFragment(TabState state) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
switch (state) {
case HOME: {
transaction.show(home);
transaction.hide(trending);
transaction.hide(upload);
}
case TRENDING: {
transaction.hide(home);
transaction.show(trending);
transaction.hide(upload);
}
case UPLOAD: {
transaction.hide(home);
transaction.hide(trending);
transaction.show(upload);
}
}
transaction.commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.home: {
setTabStateFragment(TabState.HOME);
return true;
}
case R.id.trending: {
setTabStateFragment(TabState.TRENDING);
return true;
}
case R.id.upload: {
setTabStateFragment(TabState.UPLOAD);
return true;
}
}
return false;
}
};
enum TabState {
HOME,
TRENDING,
UPLOAD,
}
}