在来自另一个片段的片段的选项卡视图中设置选项卡位置

时间:2018-07-17 06:51:59

标签: android android-fragments android-viewpager android-tablayout

我使用Tab片段中有两个Home。当Home打开时,我叫我的ActivityTwo片段。在Home Fragment中,我有一个FloatingButton,当单击时,我转到Information片段。当在Information Fragment中单击按钮时,我将进入Home Fragment,我需要打开Second Tab。如何实现此目的?< br /> 这是用于设置TabView的代码

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
            View v=inflater.inflate(R.layout.choose_tab, container, false);
        //-----------------------------------------------------------------------------------
            tabLayout=(TabLayout)v.findViewById(R.id.simpleTabLayout);
            viewPager=(ViewPager)v.findViewById(R.id.simpleViewPager);
             viewPager.setOffscreenPageLimit(2);
            tabLayout.addTab(tabLayout.newTab().setText("One"));
            tabLayout.addTab(tabLayout.newTab().setText("Two"));
          PagerAdapter adapter=new PagerAdapter(getActivity().getSupportFragmentManager(),tabLayout.getTabCount());
            //PagerAdapter adapter=new PagerAdapter(getChildFragmentManager(),tabLayout.getTabCount());
            viewPager.setAdapter(adapter);
            tabLayout.addOnTabSelectedListener(this);
            viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
            return v;
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());
    }

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

    @Override
    public void onTabReselected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());
    }  

这是用于替换Home片段

的代码
freeTable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("Confirmation.");
                builder.setMessage("Are you sure to Free this Person?");
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        alertDialog.dismiss();
                        freeSinglePerson();
                        alertMsg.successMessage(infoTable,"Person Free Successfully.");
                        Home home=new Home();
                        tx = fm.beginTransaction();
                        tx.replace(R.id.frame,home);
                        vp.setCurrentItem(1);
                        tx.commitAllowingStateLoss();

                    }
                });  

当我的Home片段被替换后,我想设置“第二个标签”。如何实现?

1 个答案:

答案 0 :(得分:0)

尝试以下代码从“活动”中获取标签布局

public void validateToken(HttpServletRequest request) {
    Cookie cookie =  WebUtils.getCookie(request, "TOKEN_NAME");
    if(cookie == null) {
        throw new SecurityException("JWT token missing");
    }
    String token  = cookie.getValue();  // JWT Token

    Claims claims =   TokenHelper.getAllClaimsFromToken(token); // claims will be null if Token is invalid

}

然后设置标签位置

public TabLayout yourPublicMethodToGetTabLayout(){ // inside activity
 return tabLayout;
}
TabLayout tabview=    ((YourActivityClassName)getActivity()).yourPublicMethodToGetTabLayout(); //inside fragment