如何意图进入特定的选项卡片段?

2024-01-03

有两个活动:MainActivity 和 LoginActivity。我也有一个Fragment,名称是TabFragment(带有TabLayout),它的父级是MainActivity。我如何从我的登录活动中意图进入特定的 TabFragment。

这是我的主要活动:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        id = getIntent().getStringExtra("id");


        if (savedInstanceState == null) {
     getSupportFragmentManager().beginTransaction().replace(R.id.confrag, new FragmentUtama()).commit();
            getSupportFragmentManager().beginTransaction().replace(R.id.confrag, new TabFragment()).commit();
        }
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        sharedPreferences = getSharedPreferences("login.conf", MODE_PRIVATE);
        editor = sharedPreferences.edit();
        String username = sharedPreferences.getString("username", "");
        String password = sharedPreferences.getString("password", "");
        Log.d(TAG, "Username:"+username+"\npassword:"+password);


        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.setDrawerListener(toggle);
        toggle.syncState();

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

    }

这是我的选项卡片段:

  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.viewpager, null);

        tabLayout = (TabLayout)view.findViewById(R.id.tabs);
        viewPager = (ViewPager)view.findViewById(R.id.viewpager);
        viewPager.setAdapter(new Adapterbaru(getChildFragmentManager()));
         tabLayout.post(new Runnable() {
            @Override
            public void run() {
                tabLayout.setupWithViewPager(viewPager);
            }
        });
        Long tsLong = System.currentTimeMillis()/1000;
        String ts = tsLong.toString();


        return view;
    }

    public interface OnFragmentInteractionListener {

        void onFragmentInteraction(Uri uri);
    }
    public class Adapterbaru extends FragmentPagerAdapter{
        public Adapterbaru(FragmentManager fm){
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0 : {
                    return new Fragmentone();
                }
                case 1 : {
                    return new FragmentTwo();

                }
                case 2 : {
                    return new Fragmenttree();

                }
            }
            return null;
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            switch(position){
                case 0 : {
                    return "First";

                }
                case 1 : {
                    return "Second";

                }
                case 2 : {
                    return "Third";
                }
            }
            return null;
        }

  1. 从你的LoginActivity,传递选项卡片段(FragmentOne, FragmentTwo, FragmentThree) id as 0, 1, 2通过Intent.

    Intent intent  = new Intent(LoginActivity.this, MainActivity.class);
    intent.putExtra("FRAGMENT_ID", 0);
    startActivity(intent);
    
  2. In your MainActivity, 获取片段 idIntent

    // Inside OnCreate()
    int fragmentId = getIntent().getIntExtra("FRAGMENT_ID", 0);
    
  3. From MainActivity, show TabFragment using FragmentManager并通过fragmentId to TabFragment using Bundle

    // Inside OnCreate()
    Bundle bundle = new Bundle();
    bundle.putString("TARGET_FRAGMENT_ID", fragmentId);
    TabFragment tabFragment = new TabFragment();
    tabFragment.setArguments(bundle);
    
    getSupportFragmentManager().beginTransaction().replace(R.id.confrag, tabFragment).commit();
    
  4. In TabFragment, 获取目标片段(FragmentOne, FragmentTwo, FragmentThree)身份证号来自Bundle最后使用显示所需的片段ViewPager.setCurrentItem(POSITION) method.

    // Inside OnCreateView()
    int position = getArguments().getInt("TARGET_FRAGMENT_ID"); 
    viewpager.setCurrentItem(position);
    

希望这会有所帮助~

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何意图进入特定的选项卡片段? 的相关文章

随机推荐

  • 从一个类获取值以在另一个类中使用它(java)

    我有两节课 ABC 班上 double a 0 5 public double lala return a 我想在另一个类中使用它 比如说 DEF 类 ABC abc double baba abc lala 但它说 java lang N
  • 如何在 OS X Yosemite 中开发状态栏应用程序?

    我一直在尝试为 Yosemite 开发一个状态栏应用程序 这是我的第一个 OS X 应用程序 但是当我搜索该文档时 NSStatusItem文档说几乎所有的属性和方法 例如 title highlightMode and image 在 O
  • NDepend - 寻找死方法

    我有两个公共方法 A 和 B 我修改了 CQL 以向我显示公共方法和死方法 如果 B 仅被 A 调用 并且 A 作为死方法 在应用程序中的任何地方都没有调用或引用 NDepend 是否可以通过进行一些设置或通过 CQL 将这两个方法显示为死
  • PHP5 中有效且可读的注释方法是什么?

    在我学习 PHP 的过去 2 个月里 我发现了不止两种人们用来注释代码的风格 我没有看到太多的一致性 我认为这通常意味着艺术家在工作 所以我想知道 仍然可读 实用的有效评论方式是什么 在一个地方并排查看所有有效的可能性将提供我正在寻找的改进
  • 以编程方式绘制 OpenCV 色调曲线

    我想实现像色调曲线这样的东西 I have predefined set of curves that I should apply to the image For instance 据我了解 在此图表中 我们看到当前音调值与新音调值的依
  • 导航栏隐藏不工作 IOS

    大家好 我是 iOS 开发新手 我有一个主屏幕 导航栏隐藏 从那里我使用后转导航到另一个视图 但是当我单击返回时 它在主屏幕上显示导航栏 这是我的问题描述 在主屏幕 onviewload 中我正在做 self navigationContr
  • 将 docker-compose.yml 文件转换为 kubernetes

    我正在使用运行以下命令的 kompose 将 docker compose 文件转换为 kubernetes kompose 转换 f docker compose yml to kubernetes image yaml 命令完成后 输出
  • Python 2 中 dict.items() 和 dict.iteritems() 有什么区别?

    之间是否存在任何适用的差异dict items http docs python org library stdtypes html dict items and dict iteritems http docs python org li
  • Vim:在视觉模式下选择文本块的更快方法

    I have been using vim for quite some time and am aware that selecting blocks of text in visual mode is as simple as SHIF
  • 在 Ubuntu Server 16.04 上安装 CouchDB 2.1

    有谁知道如何在 Ubuntu Server 16 04 上安装 CouchDB 手册中的CouchDB 文档 http docs couchdb org en 2 1 0 install unix html似乎被打破了 一个简单的apt g
  • 同一台计算机上的两个 GitHub 帐户

    首先 我知道这个问题已经被问过并回答了好几次 但是我找到的所有解决方案都不起作用 任务非常简单 我有两个 GitHub 帐户 一个是个人帐户 第二个是商业帐户 我尝试使用一些配置 但都是这样的 Host github com persona
  • 使用“scale_x_log10”时,如何将“geom_text”准确映射到“geom_bin2d”?

    关于如何标记计数的一个很好的答案geom bin2d 可以在这里找到 使用 R 获取热图中 bin 的计数 https stackoverflow com questions 28771018 getting counts on bins
  • 什么是全球交易?

    JSR 907 JTA 1 2定义 全局事务 术语 UserTransaction begin 方法启动一个全球交易和 将事务与调用线程关联起来 这意味着什么 是最外面的事务还是什么 基本上 本地事务和全局事务之间的区别在于资源限制 全局事
  • MVC 区域不渲染 _Layout

    我有 NET CORE 2 2 的 MVC 项目我在项目中添加了区域 它在本地计算机中可以正常工作 当应用程序成功发布时 我使用 Azure Devops 设置 CI CD 管道 我注意到我的区域渲染不正确 下面的内容来自 azure 网站
  • 如何在 Excel 中使用 VBA 创建和填充 ActiveX 组合框。

    我在尝试在 Excel 的 vba 中创建并填充 ActiveX 组合框时遇到问题 下面的代码在作为两个单独的宏运行时有效 但是当我尝试将两者放在一起时 会创建一个空的组合框 谁能告诉我这是为什么以及如何克服这个问题 提前致谢 杰威 Sub
  • Nvidia NPP nppiFilter 在与 2d 内核卷积时产生垃圾

    Nvidia 性能基元 NPP http developer nvidia com cuda nvidia performance primitives提供了nppiFilter用于将用户提供的图像与用户提供的内核进行卷积的函数 对于一维卷
  • 在 Debian Squeeze 上升级 phpmyadmin(和其他软件包)

    我刚刚使用 Debian Squeeze 最新稳定版本 6 0 4 设置了一个新的虚拟机 我想要一个网络服务器 所以我安装了常用的 apache php5 mysql phpmyadmin 等 一切都很顺利 一切正常 我的问题是关于升级包的
  • 在列表中查找元素的Scheme 函数是什么?

    我有一个元素列表 a b c 我想查找其中是否有 真或假 x 例如 其中 x 可以是 a 或 d 有为此的内置函数吗 如果您需要使用内置等价运算符之一进行比较 您可以使用memq memv or member http www scheme
  • AngularJS:自定义迭代/数据转换和分组...当简单的 ng-repeat 无法解决问题时

    还是这个问题Angular js 更复杂的条件循环 https stackoverflow com q 15943193 301596但我觉得这个问题的答案是正确的 所以我接受了 因此 让我比在原来的问题中更详细地阐述 我正在努力得到这个
  • 如何意图进入特定的选项卡片段?

    有两个活动 MainActivity 和 LoginActivity 我也有一个Fragment 名称是TabFragment 带有TabLayout 它的父级是MainActivity 我如何从我的登录活动中意图进入特定的 TabFrag