带TabLayout的导航抽屉

时间:2018-05-05 16:39:12

标签: android android-fragments android-tablayout android-navigation-drawer

我正在制作时间表。在哪个导航抽屉将显示日期和标签栏将显示时间明智的午餐,晚餐,早餐。

问题:如何制定条件,以便我们同时了解所选导航抽屉项目和所选标签项目。

MessActivity.java

<meta name="google-site-verification" content="blah233445556667 blahblahblah" />

tabsPager.java

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mess);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    TabLayout tablayout = (TabLayout) findViewById(R.id.tabs);
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);

    tablayout.setupWithViewPager(viewPager);


    tabsPager tabsPager = new tabsPager(getSupportFragmentManager());
    viewPager.setAdapter(tabsPager);


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tablayout));

    tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            if (tab.getPosition() == 1) {
                toolbar.setBackgroundColor(ContextCompat.getColor(MessActivity.this,
                        R.color.colorAccent));
                tablayout.setBackgroundColor(ContextCompat.getColor(MessActivity.this,
                        R.color.colorAccent));
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().setStatusBarColor(ContextCompat.getColor(MessActivity.this,
                            R.color.colorAccent));
                }
            } else if (tab.getPosition() == 2) {
                toolbar.setBackgroundColor(ContextCompat.getColor(MessActivity.this,
                        android.R.color.darker_gray));
                tablayout.setBackgroundColor(ContextCompat.getColor(MessActivity.this,
                        android.R.color.darker_gray));
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().setStatusBarColor(ContextCompat.getColor(MessActivity.this,
                            android.R.color.darker_gray));
                }
            } else {
                toolbar.setBackgroundColor(ContextCompat.getColor(MessActivity.this,
                        R.color.colorPrimary));
                tablayout.setBackgroundColor(ContextCompat.getColor(MessActivity.this,
                        R.color.colorPrimary));
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().setStatusBarColor(ContextCompat.getColor(MessActivity.this,
                            R.color.colorPrimaryDark));
                }
            }


        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}


@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.monday) {

    } else if (id == R.id.tuesday) {

    } else if (id == R.id.wednesday) {
    } else if (id == R.id.thursday) {

    } else if (id == R.id.friday) {

    } else if (id == R.id.saturday) {

    } else if (id == R.id.sunday) {

    } else if (id == R.id.next) {

    } else if (id == R.id.today) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}

screenshot

screenshot

EDIT MessActivity

public class tabsPager extends FragmentStatePagerAdapter {


String[] titles=new String[]{"Breakfast","Lunch","Dinner"};

public tabsPager(FragmentManager fm) {
    super(fm);
}

@Nullable
@Override
public CharSequence getPageTitle(int position) {
    return titles[position];
}

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            BreakfastFragment breakfastFragment =new BreakfastFragment();

            return  breakfastFragment;
        case 1: LunchFragment lunchFragment =new LunchFragment();
            return lunchFragment;
        case 2:
            DinnerFragment dinnerFragment =new DinnerFragment();
            return dinnerFragment;



    }
    return  null;
}

@Override
public int getCount() {
    return 3;
}
}

tabsPager

public class MessActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
public static int selectedNavigationMenuID= 0;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mess);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    TabLayout tablayout = (TabLayout) findViewById(R.id.tabs);
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);

    tablayout.setupWithViewPager(viewPager);



    tabsPager tabsPager = new tabsPager(getSupportFragmentManager());
    viewPager.setAdapter(tabsPager);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tablayout));

    tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {

            if (tab.getPosition() == 1) {
                toolbar.setBackgroundColor(ContextCompat.getColor(MessActivity.this,
                        R.color.colorAccent));
                tablayout.setBackgroundColor(ContextCompat.getColor(MessActivity.this,
                        R.color.colorAccent));
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().setStatusBarColor(ContextCompat.getColor(MessActivity.this,
                            R.color.colorAccent));
                }





            } else if (tab.getPosition() == 2) {
                toolbar.setBackgroundColor(ContextCompat.getColor(MessActivity.this,
                        android.R.color.darker_gray));
                tablayout.setBackgroundColor(ContextCompat.getColor(MessActivity.this,
                        android.R.color.darker_gray));
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().setStatusBarColor(ContextCompat.getColor(MessActivity.this,
                            android.R.color.darker_gray));
                }
            } else {
                toolbar.setBackgroundColor(ContextCompat.getColor(MessActivity.this,
                        R.color.colorPrimary));
                tablayout.setBackgroundColor(ContextCompat.getColor(MessActivity.this,
                        R.color.colorPrimary));
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().setStatusBarColor(ContextCompat.getColor(MessActivity.this,
                            R.color.colorPrimaryDark));
                }
            }


        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

}


@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean  onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    selectedNavigationMenuID=item.getItemId();
    int id = item.getItemId();
    if (id==R.id.next)
    {

    }
    else if(id==R.id.today){}

    else if (id == R.id.monday) { }
    else if (id == R.id.tuesday) { }
    else if (id == R.id.wednesday) {}
    else if (id == R.id.thursday) {

    } else if (id == R.id.friday) {

    } else if (id == R.id.saturday) {

    } else if (id == R.id.sunday) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}


}

BreakFastFragment

  public tabsPager(FragmentManager fm) {
    super(fm);
}





@Nullable
@Override
public CharSequence getPageTitle(int position) {
    return titles[position];
}


public void set(int selectedNavigationMenuID) { MessActivity.selectedNavigationMenuID = selectedNavigationMenuID; }



@Override
public Fragment getItem(int position) {
    switch (position) {



        case 0:set(MessActivity.selectedNavigationMenuID);
            BreakfastFragment breakfastFragment=new BreakfastFragment();
            return breakfastFragment;
        case 1:set(MessActivity.selectedNavigationMenuID);
            LunchFragment lunchFragment=new LunchFragment();
            return lunchFragment;
        case 2:set(MessActivity.selectedNavigationMenuID);
            DinnerFragment dinnerFragment=new DinnerFragment();
            return dinnerFragment;
    }
    return  null;
}

@Override
public int getCount() {
    return 3;
}
}

1 个答案:

答案 0 :(得分:1)

对于标签选择项,使用navigationview很简单 但要获得public class MessActivity extends AppCompatActivityimplements NavigationView.OnNavigationItemSelectedListener { public static int selectedNavigationMenuID= 0; //assuming the first item will be selected by default. public boolean onNavigationItemSelected(MenuItem item) { selectedNavigationMenuID=item.getItemId(); } 的所选项目,有两种方法

  1. 使用全局变量来保存id

    private int getCheckedItem(NavigationView navigationView) {
    Menu menu = navigationView.getMenu();
    for (int i = 0; i < menu.size(); i++) {
        MenuItem item = menu.getIndex(i);
        if (item.isChecked()) {
            return i;
        }
    }
    return -1;
    }
    

    }

  2. 使用将从navigationView

    返回所选位置的方法
        public class MessActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
        private SectionsPagerAdapter mSectionsPagerAdapter;
        private ViewPager viewPager;
        private Toolbar toolbar;
        private TabLayout tablayout;
        private DrawerLayout drawer;
        private String selectedNavMenu;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_mess);
    
            toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            viewPager = (ViewPager) findViewById(R.id.container);
            drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);
            selectedNavMenu = "Monday" ;//setting default value
            mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), selectedNavMenu);
            viewPager.setAdapter(mSectionsPagerAdapter);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.addDrawerListener(toggle);
            toggle.syncState();
            tablayout = (TabLayout) findViewById(R.id.tabs);
            viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tablayout));
            tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    if (tab.getPosition() == 1) {
                        toolbar.setBackgroundColor(ContextCompat.getColor(MessActivity.this,
                                R.color.colorAccent));
                        tablayout.setBackgroundColor(ContextCompat.getColor(MessActivity.this,
                                R.color.colorAccent));
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            getWindow().setStatusBarColor(ContextCompat.getColor(MessActivity.this,
                                    R.color.colorAccent));
                        }
                    } else if (tab.getPosition() == 2) {
                        toolbar.setBackgroundColor(ContextCompat.getColor(MessActivity.this,
                                android.R.color.darker_gray));
                        tablayout.setBackgroundColor(ContextCompat.getColor(MessActivity.this,
                                android.R.color.darker_gray));
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            getWindow().setStatusBarColor(ContextCompat.getColor(MessActivity.this,
                                    android.R.color.darker_gray));
                        }
                    } else {
                        toolbar.setBackgroundColor(ContextCompat.getColor(MessActivity.this,
                                R.color.colorPrimary));
                        tablayout.setBackgroundColor(ContextCompat.getColor(MessActivity.this,
                                R.color.colorPrimary));
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            getWindow().setStatusBarColor(ContextCompat.getColor(MessActivity.this,
                                    R.color.colorPrimaryDark));
                        }
                    }
                    //Here so that even if ur fragment is regenerated you get the latest value of the selectedNavigationItem
                    mSectionsPagerAdapter.getItem(viewPager.getCurrentItem()).setSelectedNavigationItem(selectedNavMenu);
                }
    
                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
    
                }
    
                @Override
                public void onTabReselected(TabLayout.Tab tab) {
    
                }
            });
        }
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            selectedNavMenu = (String) item.getTitle();
            //Just so that new created fragment will get the latest value
            mSectionsPagerAdapter.selectedNavigationMenuText = selectedNavMenu;
            mSectionsPagerAdapter.getItem(viewPager.getCurrentItem()).setSelectedNavigationItem(selectedNavMenu);
            drawer.closeDrawer(GravityCompat.START);
            return true;
        }
    }
    
  3. 这是新代码

    <强>活动

    public class SectionsPagerAdapter extends FragmentPagerAdapter {
    
    SparseArray<PlaceholderFragment> sparseArray = new SparseArray<>();
    String selectedNavigationMenuText;
    
    public SectionsPagerAdapter(FragmentManager fm, String selectedNavigationMenuText) {
        super(fm);
        this.selectedNavigationMenuText = selectedNavigationMenuText;
    }
    
    @Override
    public PlaceholderFragment getItem(int position) {
        try {
            if (sparseArray.valueAt(position) != null) {
                return sparseArray.get(position);
            } else {
                PlaceholderFragment placeHolderFragment = PlaceholderFragment.newInstance(position, selectedNavigationMenuText);
                sparseArray.put(position, placeHolderFragment);
                return placeHolderFragment;
            }
        } catch (Throwable throwable) {
            PlaceholderFragment placeHolderFragment = PlaceholderFragment.newInstance(position, selectedNavigationMenuText);
            sparseArray.put(position, placeHolderFragment);
            return placeHolderFragment;
        }
    }
    
    @Override
    public int getCount() {
        return 3;
    }
    }
    

    <强> PagerAdapter

    public class PlaceholderFragment extends Fragment {
    private static final String ARG_SECTION_NUMBER = "section_number";
    private static final String SELECTED_NAVIGATION_ITEM = "selectedTab";
    private int selecteTabItem;
    private TextView textView;
    private String selectedMenuItem;
    
    public PlaceholderFragment() {
    }
    
    public static PlaceholderFragment newInstance(int sectionNumber, String selectedNavigationItem) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        args.putString(SELECTED_NAVIGATION_ITEM, selectedNavigationItem);
        fragment.setArguments(args);
        return fragment;
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_mess, container, false);
        textView = rootView.findViewById(R.id.section_label);
        selecteTabItem = getArguments().getInt(ARG_SECTION_NUMBER);
        selectedMenuItem = getArguments().getString(SELECTED_NAVIGATION_ITEM);
        setText();
        return rootView;
    }
    
    private void setText() {
        switch (selecteTabItem) {
            case 0:
                textView.setText("BREAKFAST : " + selectedMenuItem);
                break;
            case 1:
                textView.setText("LUNCH : " + selectedMenuItem);
                break;
            case 2:
                textView.setText("DINNER : " + selectedMenuItem);
                break;
    
        }
    }
    
    public void setSelectedNavigationItem(String selectedMenuItem) {
        this.selectedMenuItem = selectedMenuItem;
        setText();
    }
    }
    

    <强>片段

      getActivity().getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      getActivity().getSupportActionBar().setDisplayShowHomeEnabled(true);