我如何将此活动转换为片段和片段到导航

时间:2018-02-28 13:33:38

标签: java android android-studio android-fragments

如何将其转换为片段并将片段添加到每个导航中 而不是文本我想添加片段并将整个代码更改为片段

任何可以帮助解决这个问题

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 个答案:

答案 0 :(得分:2)

为家庭,仪表板和通知创建自己的片段类。

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();
    }
}