Я установил ViewPager в Navigation Drawer, но ViewPager занимает весь экран в Android - PullRequest
0 голосов
/ 13 марта 2019

Я пытаюсь добавить NavigationDrawer и ViewPager в моем приложении.

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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/lightGrey">

        <include android:id="@+id/app_title_bar"
            layout="@layout/action_bar"/>


    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:headerLayout="@layout/nav_drawer_header"
            />
    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_drawer_header"
        app:menu="@menu/notification_drawer_menu"
        app:itemIconTint="@color/ods_enable_gray"
        app:itemTextColor="@color/ods_enable_gray"/>

</android.support.v4.widget.DrawerLayout>

MyActivity - я инициализировал метод Viewpager в onCreate() и метод NavigationItemSelected,

private MenuItem selectedNavMenuItem;
private NavigationView navigationView;

ViewPagerNotificationAdapter pagerAdapter = new ViewPagerNotificationAdapter(getSupportFragmentManager(), fragmentBundle);
        viewPager.setAdapter(pagerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                invalidateOptionsMenu();
            }

            @Override
            public void onPageSelected(int position) {

                if (selectedNavMenuItem != null)
                    selectedNavMenuItem.setChecked(false);
                else
                    navigationView.getMenu().getItem(position).setChecked(false);

                navigationView.getMenu().getItem(position).setChecked(true);
                selectedNavMenuItem = navigationView.getMenu().getItem(position);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });




@Override
    public boolean onNavigationItemSelected(MenuItem item) {
        try {
            selectedNavMenuItem = item;
            int intItemID = item.getItemId();
            Fragment fragment = null;
            Bundle bundle = new Bundle();
            actionsToShow = new ArrayList<>();
            navigationView.getMenu().findItem(intItemID).setCheckable(true);

            if (intItemID == R.id.nav_overview) {

                viewPager.setCurrentItem(0);
                 if (!isWONotif)
                    actionsToShow = AppSettings.getMenuActionsforNotifications(AppSettings.TabList.Overview);
            } else if (intItemID == R.id.nav_items) {

                viewPager.setCurrentItem(1);
                //fragment = new ItemsFragment();
                if (!isWONotif)
                    actionsToShow = AppSettings.getMenuActionsforNotifications(AppSettings.TabList.Items);
            } else if (intItemID == R.id.nav_activities) {
                viewPager.setCurrentItem(2);
                //fragment = new ActivitiesFragment();
                if (!isWONotif)
                    actionsToShow = AppSettings.getMenuActionsforNotifications(AppSettings.TabList.Activities);
            } else if (intItemID == R.id.nav_tasks) {

                viewPager.setCurrentItem(3);
                //fragment = new TasksFragment();
                if (!isWONotif)
                    actionsToShow = AppSettings.getMenuActionsforNotifications(AppSettings.TabList.Tasks);
            } else if (intItemID == R.id.nav_attachments) {
                viewPager.setCurrentItem(4);


                /*Class fragmentClass = Common.getNextClass(Collections.NOTIFICATION_DOCS_FRAGMENT);
                if (fragmentClass == null)
                    fragmentClass = DocsFragment.class;
                fragment = (BaseFragment) Common.getClassInstance(fragmentClass);*/
                if (!isWONotif)
                    actionsToShow = AppSettings.getMenuActionsforNotifications(AppSettings.TabList.Attachments);
            } else if (intItemID == R.id.nav_history) {
                viewPager.setCurrentItem(5);
                /*bundle.putBoolean(Collections.ARG_IS_HISTORY, intItemID == R.id.nav_history);
                fragment = new HistoryPendingFragment();*/
                if (!isWONotif)
                    actionsToShow = AppSettings.getMenuActionsforNotifications(null);
            }else if(intItemID == R.id.nav_pending){
                viewPager.setCurrentItem(6);
                /*bundle.putBoolean(Collections.ARG_IS_HISTORY, intItemID == R.id.nav_history);
                fragment = new HistoryPendingFragment();*/
                if (!isWONotif)
                    actionsToShow = AppSettings.getMenuActionsforNotifications(null);
            }
            if (fragment != null) {
                hideShowActions();
                /*bundle.putBoolean("TwoColumnLayout", false);
                bundle.putBoolean(Collections.ARG_IS_WO_NOTIFICATION, isWONotif);
                fragment.setArguments(bundle);
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.notification_detail_container, fragment, "NotificationDetails")
                        .commit();*/
                drawer.closeDrawer(Gravity.LEFT, true);
                fragmentsMap.put(intItemID, fragment);
            }

        } catch (Exception e) {
            DliteLogger.WriteLog(this.getClass(), AppSettings.LogLevel.Error, e.getMessage());
        }
        return true;
    }

Ниже мой ViewPagerAdapter класс,

public class ViewPagerNotificationAdapter extends FragmentPagerAdapter {

    private final Bundle fragmentBundle;
    private boolean isWONotif = false;

    public ViewPagerNotificationAdapter(FragmentManager fm, Bundle fragmentBundle) {
        super(fm);
        this.fragmentBundle = fragmentBundle;

    }

    @Override
    public Fragment getItem(int position) {


        isWONotif = (boolean) this.fragmentBundle.get(Collections.ARG_IS_WO_NOTIFICATION);

            if(position == 0){
                //return new DetailsFragment();
                final DetailsFragment df=new DetailsFragment();
                this.fragmentBundle.putBoolean(Collections.ARG_IS_WO_NOTIFICATION, isWONotif);
                df.setArguments(this.fragmentBundle);
                return df;


            }else if(position == 1){
                //return new ItemsFragment();
                final ItemsFragment ifg=new ItemsFragment();
                this.fragmentBundle.putBoolean(Collections.ARG_IS_WO_NOTIFICATION, isWONotif);
                ifg.setArguments(this.fragmentBundle);
                return ifg;
            }else if(position == 2){
                //return new ActivitiesFragment();
                final ActivitiesFragment af=new ActivitiesFragment();
                this.fragmentBundle.putBoolean(Collections.ARG_IS_WO_NOTIFICATION, isWONotif);
                af.setArguments(this.fragmentBundle);
                return af;
            }else if(position == 3){
                //return new TasksFragment();
                final TasksFragment tf=new TasksFragment();
                this.fragmentBundle.putBoolean(Collections.ARG_IS_WO_NOTIFICATION, isWONotif);
                tf.setArguments(this.fragmentBundle);
                return tf;
            }else if(position == 4){
                /*Class fragmentClass = Common.getNextClass(Collections.NOTIFICATION_DOCS_FRAGMENT);
                if (fragmentClass == null)
                    fragmentClass = DocsFragment.class;
                fragment = (BaseFragment) Common.getClassInstance(fragmentClass);*/
                return new DocsFragment();
            }else if(position == 5){
                final HistoryPendingFragment hf=new HistoryPendingFragment();
                this.fragmentBundle.putBoolean(Collections.ARG_IS_HISTORY, true);
                hf.setArguments(this.fragmentBundle);
                return hf;
            }else{
                final HistoryPendingFragment hf=new HistoryPendingFragment();
                this.fragmentBundle.putBoolean(Collections.ARG_IS_HISTORY, false);
                hf.setArguments(this.fragmentBundle);
                return hf;
            }

    }

    @Override
    public int getCount() {

        return 7;
    }

}

Я работаю на панели навигации и с ViewPager. Viewpager работает, Я могу видеть NavigationDrawer в фоновом режиме, а также могу скользить NavigationDrawer с ViewPager, но ViewPager занимает весь экран. Я должен показать NavigationDrawer, который отображается в фоновом режиме. Пожалуйста помоги мне с этим. Заранее спасибо.

...