【问题标题】:Start a tab fragments from an activity with the tabs layout从具有选项卡布局的活动中启动选项卡片段
【发布时间】:2018-05-31 22:11:37
【问题描述】:

我有一个标签片段应用程序。问题是当我进入我的应用程序内的一个活动并想要返回我的选项卡布局时,它返回时没有选项卡行布局。我只能看到特定的片段页面,而没有上面的行(选项卡的行),这使我能够在选项卡之间导航。 你知道我能做些什么来解决它吗?我怎样才能看到标签的行呢? 感谢您的帮助,

这是我想从活动中看到的第一个标签:

public class Tab1MyProfile extends Fragment  {

   @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.tab1_my_profile, container, false);
        return rootView;
    }
}

这是包含返回片段选项卡的代码的活动:

public class GameLive extends AppCompatActivity implements RecognitionListener {
    private Intent intent;
    private Button mStopButton;

    public void stopGame (View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Are you sure you want to finish the game?")
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        FragmentManager fragmentManager = getSupportFragmentManager();
                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                        Tab1MyProfile fragment = new Tab1MyProfile();
                        fragmentTransaction.replace(android.R.id.content, fragment);
                        fragmentTransaction.commit();
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    }


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game_live);
        mStopButton = (Button) findViewById(R.id.btnEndGame);
        mStopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stopGame(view);
            }
        });

    }

}

这是包含所有选项卡片段的活动:

public class StartActivity extends AppCompatActivity {

    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;

    @TargetApi(Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_start, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    Tab1MyProfile tab1=new Tab1MyProfile();
                    return tab1;
                case 1:
                    Tab2StartGame tab2=new Tab2StartGame();
                    return tab2;
                case 2:
                    Tab3StatsArea tab3=new Tab3StatsArea();
                    return tab3;
                case 3:
                    Tab4Settings tab4=new Tab4Settings();
                    return tab4;
            }
            return null;
        }

        @Override
        public int getCount() {
            // Show 4 total pages.
            return 4;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "My profile";
                case 1:
                    return "Start Game";
                case 2:
                    return "Stats Area";
                case 3:
                    return "Settings";
            }
            return null;
        }
    }


}

再次感谢!

【问题讨论】:

    标签: android android-fragments tabs android-tabs


    【解决方案1】:

    如果将DialogInterface.OnClickListeneronClick 方法中的所有内容替换为GameLive.this.finish(); 会怎样。这将在单击时关闭 GameLive 活动并返回到上一个活动,该活动应该包含缺少的导航栏。

    【讨论】:

    • 这是个好主意,但是在我要关闭的活动和包含缺少导航栏的片段之间有一个活动。你知道如何一次关闭两个活动吗?
    • 您确定要同时关闭这两个活动吗?如果您从用户的角度考虑,这可能会导致混乱。如果你真的想操纵活动堆栈,有办法。例如,查看 Intent 标志。
    • 感谢您的帮助!我决定听取您的建议,只关闭一项活动:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    • 1970-01-01
    相关资源
    最近更新 更多