本文介绍了如何意图进入特定的选项卡片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

T有两个活动:MainActivity和LoginActivity.我也有一个Fragment,名称是TabFragment(带有TabLayout),其父级是MainActivity.如何从我的登录活动中查看特定的TabFragment.

T have two Activities: MainActivity and LoginActivity. I have one Fragment too, the name is TabFragment (with a TabLayout), its parent is MainActivity. How can I intent into the specific TabFragment from my Login Activity.

这是我的主要活动:

 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);

    }

这是我的Tab片段:

  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 中,通过Intent将制表符片段(FragmentOne, FragmentTwo, FragmentThree) id作为0, 1, 2传递.

  1. From your LoginActivity, pass tabs fragment (FragmentOne, FragmentTwo, FragmentThree) id as 0, 1, 2 through Intent.

Intent intent  = new Intent(LoginActivity.this, MainActivity.class);
intent.putExtra("FRAGMENT_ID", 0);
startActivity(intent);

  • 在您的 MainActivity 中,从Intent

    // Inside OnCreate()
    int fragmentId = getIntent().getIntExtra("FRAGMENT_ID", 0);
    

  • MainActivity 中,使用FragmentManager显示TabFragment,然后使用Bundle

  • From MainActivity, show TabFragment using FragmentManager and pass 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();
    

  • TabFragment 中,从Bundle获取目标片段(FragmentOne, FragmentTwo, FragmentThree) id,最后使用ViewPager.setCurrentItem(POSITION)方法显示所需的片段.

  • In TabFragment, get target fragment (FragmentOne, FragmentTwo, FragmentThree) id from Bundle and finally show desired fragment using ViewPager.setCurrentItem(POSITION) method.

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

  • 希望这会有所帮助〜

    这篇关于如何意图进入特定的选项卡片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    06-19 22:07