android – 如何在单击导航抽屉的菜单项时打开选项卡布局中的特定选项卡?

android – 如何在单击导航抽屉的菜单项时打开选项卡布局中的特定选项卡?,第1张

概述实际上我是 Android的新手并且陷入了导航抽屉..  我已成功设计了材料设计的导航抽屉及其菜单项. 但是,当我点击该菜单时,它会打开一个独立的片段,而我希望它在Tab Layout中的特定选项卡中打开它. 我用自定义适配器制作了单独的Tab布局. mytablayout .xml文件为 <?xml version="1.0" encoding="utf-8"?><LinearLayout 实际上我是 Android的新手并且陷入了导航抽屉..
 我已成功设计了材料设计的导航抽屉及其菜单项.
但是,当我点击该菜单时,它会打开一个独立的片段,而我希望它在Tab Layout中的特定选项卡中打开它.

我用自定义适配器制作了单独的Tab布局. mytablayout .xml文件为

<?xml version="1.0" enCoding="utf-8"?><linearLayout  xmlns:androID="http://schemas.androID.com/apk/res/androID"  xmlns:app="http://schemas.androID.com/apk/res-auto"  androID:layout_wIDth="match_parent"androID:orIEntation="vertical"androID:layout_height="wrap_content"><androID.support.design.Widget.TabLayout    androID:ID="@+ID/tabs"    app:tabGravity="fill"    app:tabMode="fixed"    app:elevation="0dp"    androID:background="#6ec6c5"    app:tabIndicatorcolor="#000000"    app:tabSelectedTextcolor="@color/textcolor"    app:tabTextcolor="#A8DCDC"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:minHeight="?attr/actionbarSize"></androID.support.design.Widget.TabLayout><androID.support.v4.vIEw.VIEwPager    androID:ID="@+ID/vIEwpager"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"></androID.support.v4.vIEw.VIEwPager></linearLayout>

我的Tabfragment类在那里:

public class TabFragment extends Fragment {public static TabLayout tabLayout;public static VIEwPager vIEwPager;public static int int_items = 3;@Nullable@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) {    /**     *Inflate tab_layout and setup VIEws.     */    VIEw x = inflater.inflate(R.layout.fragment_tab,null);    tabLayout = (TabLayout) x.findVIEwByID(R.ID.tabs);    vIEwPager = (VIEwPager) x.findVIEwByID(R.ID.vIEwpager);    /**     *Set an Apater for the VIEw Pager     */    vIEwPager.setAdapter(new MyAdapter(getChildFragmentManager(),int_items ));    /**     * Now,this is a workaround,* The setupWithVIEwPager dose't works without the runnable .     * Maybe a Support library BUG .     */    tabLayout.post(new Runnable() {        @OverrIDe        public voID run() {            tabLayout.setupWithVIEwPager(vIEwPager);        }    });    return x;  }}MyAdapter is : public class MyAdapter extends FragmentPagerAdapter {  int int_items;  public MyAdapter(FragmentManager fm,int int_items) {    super(fm);    this.int_items = int_items;}/** * Return fragment with respect to position . */@OverrIDepublic Fragment getItem(int position){    switch (position){        case 0 : return new ProductFragment();        case 1 : return new ProductFragment();        case 2 : return new ProductFragment();    }    return null;}@OverrIDepublic int getCount() {    return int_items;}/** * This method returns the Title of the tab according to the position. */@OverrIDepublic CharSequence getPageTitle(int position) {    switch (position){        case 0 :            return "PRODUCTS";        case 1 :            return "FEATURED";        case 2 :            return "FAVOURITES";    }    return null; }}

我的SplitvIEwActivity显示如下:

public class SplitVIEwActivity extends AppCompatActivity {  private DrawerLayout mDrawerLayout;  private NavigationVIEw navigationVIEw;  FragmentManager mFragmentManager;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_split_vIEw);    Toolbar toolbar = (Toolbar) findVIEwByID(R.ID.toolbar);    setSupportActionbar(toolbar);    getSupportActionbar().setdisplayHomeAsUpEnabled(true);    getSupportActionbar().setHomebuttonEnabled(true);    getSupportActionbar().setHomeAsUpIndicator(R.drawable.menu);    mDrawerLayout = (DrawerLayout) findVIEwByID(R.ID.drawer_layout);    navigationVIEw = (NavigationVIEw) findVIEwByID(R.ID.navigation_vIEw);    getSupportFragmentManager().beginTransaction().replace(R.ID.frame_container,new TabFragment()).commit();    navigationVIEw.setNavigationItemSelectedListener(new NavigationVIEw.OnNavigationItemSelectedListener() {        @OverrIDe        public boolean onNavigationItemSelected(MenuItem menuItem) {            menuItem.setChecked(true);            mDrawerLayout.closeDrawers();            switch (menuItem.getItemID()) {                case R.ID.navigation_item_products:                    getSupportFragmentManager().beginTransaction().replace(R.ID.frame_container,new TabFragment()).commit();                    Toast.makeText(SplitVIEwActivity.this,"Navigation Sub Item 01 Clicked",Toast.LENGTH_SHORT).show();                    // updatedisplay(new AttachmentFragment());                    break;                case R.ID.navigation_item_new_Releases:                    getSupportFragmentManager().beginTransaction().replace(R.ID.frame_container,new ProductFragment()).commit();                 /*   FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();                    fragmentTransaction.replace(R.ID.frame_container,new ProductFragment()).commit();*/                    Toast.makeText(SplitVIEwActivity.this,Toast.LENGTH_SHORT).show();                    // updatedisplay(new ImageFragment());                    break;                case R.ID.navigation_item_favorites:                    getSupportFragmentManager().beginTransaction().replace(R.ID.frame_container,new TabFragment()).commit();                    Toast.makeText(SplitVIEwActivity.this,Toast.LENGTH_SHORT).show();                    // updatedisplay(new MyLocationFragment());                    break;                case R.ID.navigation_item_about_us:                    Toast.makeText(SplitVIEwActivity.this,Toast.LENGTH_SHORT).show();                    // updatedisplay(new MyLocationFragment());                    break;                case R.ID.navigation_item_notification:                    Toast.makeText(SplitVIEwActivity.this,Toast.LENGTH_SHORT).show();                    //  updatedisplay(new MyLocationFragment());                    break;                case R.ID.navigation_sub_item_01:                    Toast.makeText(SplitVIEwActivity.this,Toast.LENGTH_SHORT).show();                    break;                case R.ID.navigation_sub_item_02:                    Toast.makeText(SplitVIEwActivity.this,"Navigation Sub Item 02 Clicked",Toast.LENGTH_SHORT).show();                    break;            }            return true;        }    });}@OverrIDepublic boolean onCreateOptionsMenu(Menu menu) {    // Inflate the menu; this adds items to the action bar if it is present.    getMenuInflater().inflate(R.menu.menu_splash_screen,menu);    return true;}@OverrIDepublic boolean onoptionsItemSelected(MenuItem item) {    int ID = item.getItemID();    switch (ID) {        case androID.R.ID.home:            mDrawerLayout.openDrawer(GravityCompat.START);            return true;        case R.ID.action_settings:            return true;    }    return super.onoptionsItemSelected(item); }}

我的导航菜单item.xml文件如下:

<?xml version="1.0" enCoding="utf-8"?>
<group androID:checkableBehavior="single">    <item        androID:ID="@+ID/navigation_item_products"        androID:checked="true"        androID:title="Products" />    <item        androID:ID="@+ID/navigation_item_new_Releases"        androID:title="New Releases" />    <item        androID:ID="@+ID/navigation_item_favorites"        androID:title="favorites" />    <item        androID:ID="@+ID/navigation_item_about_us"        androID:title="About Us" />    <item        androID:ID="@+ID/navigation_item_notification"        androID:title="Notifications" /></group><item androID:title="">    <menu>        <item            androID:ID="@+ID/navigation_sub_item_01"            androID:title="Configure" />        <item            androID:ID="@+ID/navigation_sub_item_02"            androID:title="logout" />    </menu></item>

一切都很好,除了.所有三个标签已经显示.导航抽屉也很好用.

只有我无法理解的事情,我怎么能在选项卡上点击导航菜单项打开一个片段.当我点击菜单项时,它打开一个独立的片段不在tab.How我能实现这一点..任何帮助将在先进的..

解决方法 只需检查抽屉上点击点击的位置并检查每个vIEwpager选项卡哪个选项卡是您想要的并使用VIEwpager.setCurrentItem(posiition)

导航到该选项卡.

public voID setStartTab(String topTab) {    LocalLog.e("topTAB",topTab+"");    if(topTab==null){   mVIEwPager.setCurrentItem(0);    }else{        for(int i=0;i<mTabs.size();i++){            LocalLog.e("topTAB",mTabs.get(i).name);            if(mTabs.get(i).name.equalsIgnoreCase(topTab)){                LocalLog.e("topTAB",i+"true");            mVIEwPager.setCurrentItem(i);            }        }    }}
总结

以上是内存溢出为你收集整理的android – 如何在单击导航抽屉的菜单项时打开选项卡布局中的特定选项卡?全部内容,希望文章能够帮你解决android – 如何在单击导航抽屉的菜单项时打开选项卡布局中的特定选项卡?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1125761.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存