在片段中添加带选项卡的导航抽屉

时间:2017-01-27 08:54:58

标签: android android-fragments tabs android-tablayout

Acualy我创建了一个导航抽屉活动,这个活动工作完美,但我想添加第一项Navigaion抽屉带标签布局和剩下的项目与片段(我已经完成)所以请你知道我该怎么办这样:

NavigationDrawer活动:

public class NotificationDrawer extends AppCompatActivity {

private NavigationView navigationView;
private DrawerLayout drawer;
private View navHeader;
private ImageView imgNavHeaderBg, imgProfile;
private TextView txtName, txtWebsite;
private Toolbar toolbar;
private static final String urlProfileImg = "http://www.google/images.com;
public static int navItemIndex = 0;

// tags used to attach the fragments
private static final String TAG_HOME = "home";
private static final String TAG_VEHICLE = "vehicle";
private static final String TAG_ADDRESS= "address";
public static String CURRENT_TAG = TAG_HOME;

// toolbar titles respected to selected nav menu item
private String[] activityTitles;

// flag to load home fragment when user presses back key
private boolean shouldLoadHomeFragOnBackPress = true;
private Handler mHandler;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drawer_navigation);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
   // toolbar.setNavigationIcon(R.drawable.ic_sort_white_24dp);
    setSupportActionBar(toolbar);
 /*   getSupportActionBar().setHomeButtonEnabled( true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_sort_white_24dp);*/
    toolbar.setNavigationIcon(R.drawable.ic_sort_white_24dp);
    mHandler = new Handler();
    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    navigationView = (NavigationView) findViewById(R.id.nav_view);
           // Navigation view header
    navHeader = navigationView.getHeaderView(0);
    txtName = (TextView) navHeader.findViewById(R.id.name);
    txtWebsite = (TextView) navHeader.findViewById(R.id.website);
    imgNavHeaderBg = (ImageView) navHeader.findViewById(R.id.img_header_bg);
    imgProfile = (ImageView) navHeader.findViewById(R.id.img_profile);

    // load toolbar titles from string resources
    activityTitles = getResources().getStringArray(R.array.nav_item_activity_titles);

    // load nav menu header data
 //  loadNavHeader();
    txtName.setText("Techiee");
    txtWebsite.setText("www.techiee.com");
    // initializing navigation menu
    setUpNavigationView();

    if (savedInstanceState == null) {
        navItemIndex = 0;
        CURRENT_TAG = TAG_HOME;
        loadHomeFragment();
    }
}

private void loadHomeFragment() {
    // selecting appropriate nav menu item
    selectNavMenu();

    // set toolbar title
    setToolbarTitle();
  if (getSupportFragmentManager().findFragmentByTag(CURRENT_TAG) != null) {
        drawer.closeDrawers();

        // show or hide the fab button
        //toggleFab();
        return;
    }

    Runnable mPendingRunnable = new Runnable() {
        @Override
        public void run() {
            // update the main content by replacing fragments
            Fragment fragment = getHomeFragment();
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
                    android.R.anim.fade_out);
            fragmentTransaction.replace(R.id.frame, fragment, CURRENT_TAG);
            fragmentTransaction.commitAllowingStateLoss();
        }
    };

    // If mPendingRunnable is not null, then add to the message queue
    if (mPendingRunnable != null) {
        mHandler.post(mPendingRunnable);
    }

    // show or hide the fab button
    //toggleFab();

    //Closing drawer on item click
    drawer.closeDrawers();

    // refresh toolbar menu
    invalidateOptionsMenu();
}

private Fragment getHomeFragment() {
    switch (navItemIndex) {
        case 0:
            // home
            HomeFragment homeFragment = new HomeFragment();
            return homeFragment;
        case 1:
            // registered_vehicle
            Registered_Vehicle registeredVehicle = new Registered_Vehicle();
            return registeredVehicle;
        case 2:
            // registeredAddress fragment
            RegisteredAddress registeredAddress = new RegisteredAddress();
            return registeredAddress;

        default:
            return new HomeFragment();
    }
}

private void setToolbarTitle() {
    getSupportActionBar().setTitle(activityTitles[navItemIndex]);
}

private void selectNavMenu() {
    navigationView.getMenu().getItem(navItemIndex).setChecked(true);
}

private void setUpNavigationView() {
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

        // This method will trigger on item Click of navigation menu
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
        //    navigationView.setItemIconTintList(null);
            //Check to see which item was being clicked and perform appropriate action
            switch (menuItem.getItemId()) {
                //Replacing the main content with ContentFragment Which is our Inbox View;
                case R.id.nav_home:
                    navItemIndex = 0;
                    CURRENT_TAG = TAG_HOME;
                    break;
                case R.id.registered_vehi:
                    navItemIndex = 1;
                    CURRENT_TAG = TAG_VEHICLE;
                    break;
                case R.id.registered_address:
                    navItemIndex = 2;
                    CURRENT_TAG = TAG_ADDRESS;
                    break;
                    drawer.closeDrawers();

                    navItemIndex = 0;
            }

            //Checking if the item is in checked state or not, if not make it in checked state
            if (menuItem.isChecked()) {
                menuItem.setChecked(false);
            } else {
                menuItem.setChecked(true);
            }
            menuItem.setChecked(true);

            loadHomeFragment();

            return true;
        }
    });


    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close) {

        @Override
        public void onDrawerClosed(View drawerView) {
            // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
            super.onDrawerOpened(drawerView);
        }
    };


    //Setting the actionbarToggle to drawer layout
    drawer.setDrawerListener(actionBarDrawerToggle);

    //calling sync state is necessary or else your hamburger icon wont show up
    actionBarDrawerToggle.syncState();
    actionBarDrawerToggle.setDrawerIndicatorEnabled(false);
    toolbar.setNavigationIcon(R.drawable.ic_sort_white_24dp);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            drawer.openDrawer(GravityCompat.START);
        }
    });
}

drawer_navigation.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"

        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:itemIconTint="@color/buttonbackground"
        app:menu="@menu/activity_main_drawer" />

    </android.support.v4.widget.DrawerLayout

&GT;

1 个答案:

答案 0 :(得分:1)

使用Tablayout添加NavigationDrawer

1.创建一个新文件“TabFragment.java”以膨胀&amp;代码TabLayoutviewPager元素。

2.要viewPager显示内容,需要适配器。使用FragmentPagerAdapter用于夸大特定于标签的片段。

3. FragmentPagerAdapter优先于三种主要方法:

a)getCount():它返回要与视图寻呼机绑定的标签总数。

b)getItem(int position):它将特定于标签的片段wrt返回到它的位置。

c)getPageTitle(int position)`:根据位置返回标题名称

4.最后,使用ViewPager的{​​{1}}方法附加TabLayout setupWithViewPager(viewPager)

以下是详细实施:https://androidbelieve.com/navigation-drawer-with-swipe-tabs-using-design-support-library/

希望它有所帮助。

当您使用TabLayout时,您可以使用:

toolbar

设置标题..点击片段