为片段的每个子项创建导航底部视图

时间:2017-09-25 13:09:55

标签: android fragment bottomnavigationview

您好我是Android新手,所以如果我的问题看起来多余,请耐心等待,因为我无法在任何地方找到合适的答案。我有一个主要活动,底部导航栏上有4个选项卡,三个片段用于填充这些选项卡。现在当我启动任何这些片段的子节点时,子节点不会显示导航栏。我如何实现这一目标?

MainActivity.class主要功能

    BottomNavigationView bottomNavigationView = (BottomNavigationView)
            findViewById(R.id.navigation);
    //  BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {


        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment selectedFragment = null;
            switch (item.getItemId()) {
                case R.id.action_item1:
                    selectedFragment = TabOneFragment.newInstance();

                    break;
                case R.id.action_item2:
                    selectedFragment = TabTwoFragment.newInstance();
                    break;
                case R.id.action_item3:
                    selectedFragment = TabThreeFragment.newInstance();
                    break;


            }
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace( R.id.frame_layout,selectedFragment);
            transaction.commit();
            return true;
        }
    });

    //Manually displaying the first fragment - one time only
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.frame_layout, TabOneFragment.newInstance());
    transaction.commit();

TabActivity

   public class TabOneFragment extends Fragment {




ArrayList<SettingsClass> data;
private Button send;
ListView listView;

public static TabOneFragment newInstance() {
    TabOneFragment fragment = new TabOneFragment();
    return fragment;
}


@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

}

@Override
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
    super.onViewStateRestored(savedInstanceState);

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_tab_one_fragment, container, false);


    return v;
}

@Override
public void onViewCreated(View view, @Nullable final Bundle savedInstanceState) {
    data = new ArrayList<>();

    data.add(new SettingsClass("Option1", R.drawable.o1));
    data.add(new SettingsClass("Option2", R.drawable.o2));
    listView = view.findViewById(R.id.listView);

    MyAdapter adapter = new MyAdapter(getActivity().getApplicationContext(), data);

    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch (position) {

                case 0: {

                    Intent intent = new Intent(getActivity().getApplicationContext(),ChildActivity.class);
                    startActivity(intent);
                    break;
                }
                case 1: {

                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + "NO_CONTACT"));
                    startActivity(intent);

                    break;
                }



            }
        }
    });

未使用导航底部选项卡创建ChildActivity。任何人都可以指导我吗? 选项卡一的布局:

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"

>
 <ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView">
</ListView>



 </RelativeLayout>

0 个答案:

没有答案