有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在不点击的情况下显示片段

我有一个带有两个选项卡的Tablelayout,在我编写的代码中,当我单击选项卡时,它们会显示出来。现在它是这样工作的:我打开活动,在单击TabLayout标题之前,不会显示TabLayout中的片段。我如何使其在打开activity-fragment时已经可见



public class DetailActivity extends AppCompatActivity {

    FrameLayout simpleFrameLayout;
    TabLayout tabLayout;
    ImageView onBackPressed;

    Button subsButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);


        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }


        subsButton = findViewById(R.id.subsButton);

        subsButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                if (subsButton.getText().equals("Подписаться")) {

                    subsButton.setText("Отписаться");
                    subsButton.setBackgroundColor(subsButton.getContext().getResources().getColor(R.color.colorGrey));


                } else if (subsButton.getText().equals("Отписаться")) {
                    subsButton.setText("Подписаться");
                    subsButton.setBackgroundColor(subsButton.getContext().getResources().getColor(R.color.colorPrimary));

                }

            }
        });


        simpleFrameLayout = (FrameLayout) findViewById(R.id.simpleFrameLayout);
        tabLayout = (TabLayout) findViewById(R.id.simpleTabLayout);

        onBackPressed = (ImageView) findViewById(R.id.onBackPressed);

        tabLayout.selectTab(tabLayout.getTabAt(0));

        TabLayout.Tab firstTab = tabLayout.newTab();
        firstTab.setText("Chelsea");
        tabLayout.addTab(firstTab);


        TabLayout.Tab secondTab = tabLayout.newTab();
        secondTab.setText("Manchester City");
        tabLayout.addTab(secondTab);


        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {


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

                Fragment fragment = null;
                switch (tab.getPosition()) {
                    case 0:
                        fragment = new FragmentHisOne();
                        break;
                    case 1:
                        fragment = new FragmentHisTwo();
                        break;
                    default:
                        fragment = new FragmentHisOne();
                        break;
                }
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                ft.replace(R.id.simpleFrameLayout, fragment);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                ft.commit();

            }

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


            }

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

            }
        });
    }


    public void onBackPressed(View view) {
        Intent intent = new Intent(view.getContext(), MainActivity.class);
        view.getContext().startActivity(intent);
    }




共 (2) 个答案

  1. # 1 楼答案

    您选择该选项卡之前甚至没有添加它。 表格布局。选择tab(tabLayout.getTabAt(0));添加选项卡后移动此代码

  2. # 2 楼答案

    事实上,在创建TabLayout选项卡之前,我没有预先创建默认片段。我的解决方案:

    FrameLayout simpleFrameLayout;
        TabLayout tabLayout;
        ImageView onBackPressed;
    
        Button subsButton;
    
        Fragment fragment = null;
        FragmentManager fragmentManager;
        FragmentTransaction fragmentTransaction;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_detail);
    
    
            if (getSupportActionBar() != null) {
                getSupportActionBar().hide();
            }
    
    
            subsButton = findViewById(R.id.subsButton);
    
            subsButton.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View view) {
                    if (subsButton.getText().equals("Подписаться")) {
    
                        subsButton.setText("Отписаться");
                        subsButton.setBackgroundColor(subsButton.getContext().getResources().getColor(R.color.colorGrey));
    
    
                    } else if (subsButton.getText().equals("Отписаться")) {
                        subsButton.setText("Подписаться");
                        subsButton.setBackgroundColor(subsButton.getContext().getResources().getColor(R.color.colorPrimary));
    
                    }
    
                }
            });
    
    
            simpleFrameLayout = (FrameLayout) findViewById(R.id.simpleFrameLayout);
            tabLayout = (TabLayout) findViewById(R.id.simpleTabLayout);
    
            onBackPressed = (ImageView) findViewById(R.id.onBackPressed);
    
            tabLayout.selectTab(tabLayout.getTabAt(0));
    
            TabLayout.Tab firstTab = tabLayout.newTab();
            firstTab.setText("Chelsea");
            tabLayout.addTab(firstTab);
    
    
            TabLayout.Tab secondTab = tabLayout.newTab();
            secondTab.setText("Manchester City");
            tabLayout.addTab(secondTab);
    
            fragment = new FragmentHisOne();
            fragmentManager = getSupportFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.simpleFrameLayout, fragment);
            fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            fragmentTransaction.commit();
    
            tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    
    
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
    
                    Fragment fragment = null;
                    switch (tab.getPosition()) {
                        case 0:
                            fragment = new FragmentHisOne();
                            break;
                        case 1:
                            fragment = new FragmentHisTwo();
                            break;
                        default:
                            fragment = new FragmentHisOne();
                            break;
                    }
                    FragmentManager fm = getSupportFragmentManager();
                    FragmentTransaction ft = fm.beginTransaction();
                    ft.replace(R.id.simpleFrameLayout, fragment);
                    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    ft.commit();
    
                }
    
                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
    
    
                }
    
                @Override
                public void onTabReselected(TabLayout.Tab tab) {
    
                }
            });
        }
    
    
        public void onBackPressed(View view) {
            Intent intent = new Intent(view.getContext(), MainActivity.class);
            view.getContext().startActivity(intent);
        }
    
        public void subsButton(View view) {
            //Button.setBackground
        }