问题
how to convert this to fragment and add fragment to each navigation instead of text i want add fragment and change this whole code into fragment
any can help with this
public class QuestionPaper extends AppCompatActivity {
private TextView TextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener OnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
TextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
TextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_question_paper);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
}
回答1:
Create you own fragment class for home, dashboard and notification.
public class QuestionPaper extends Fragment {
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:
HomeFragment homeFragment = HomeFragment.newInstance();
createFragment(homeFragment);
return true;
case R.id.navigation_dashboard:
DashBoardFragment dashBoardFragment = DashBoardFragment.newInstance();
createFragment(dashBoardFragment);
return true;
case R.id.navigation_notifications:
NotiFragment notiFragment = NotiFragment.newInstance();
createFragment(notiFragment);
return true;
}
return false;
}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_question_paper, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mTextMessage = (TextView) view.findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) view.findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
public void createFragment(Fragment fragment) {
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
ft.add(R.id.activity_home_container, fragment, null);
ft.addToBackStack(null);
ft.commitAllowingStateLoss();
}
}
来源:https://stackoverflow.com/questions/49030822/how-do-i-convert-this-activity-into-fragment-and-fragment-to-navigation