如果单击第二个选项卡,如何显示此Toast消息?

时间:2017-04-01 16:33:24

标签: java android android-fragments tabs toast

我有一个带有三个标签的导航栏,每个标签都是一个片段。 我在我的第二个片段的onCreateView方法中写了这个toast消息:

public class SecondFragment extends Fragment{

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


        Toast.makeText(getContext(), "Test", Toast.LENGTH_LONG).show();

        return rootView;
    }

我想要的是,只有当我单击此选项卡时,才会显示该消息。 但问题是,如果我启动应用程序,消息会直接显示,但为什么?

这是我的MainActivity.java:

public class MainActivity extends AppCompatActivity{
    NavigationView mNavigationView;
    FragmentManager mFragmentManager;
    FragmentTransaction mFragmentTransaction;

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


        /**
         *Setup the DrawerLayout and NavigationView
         */

        mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;

        /**
         * Lets inflate the very first fragment
         * Here , we are inflating the TabFragment as the first Fragment
         */

        mFragmentManager = getSupportFragmentManager();
        mFragmentTransaction = mFragmentManager.beginTransaction();
        mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();

    }


}

并且(我认为这对于这个问题很重要)我的TabFragment:

public class TabFragment extends Fragment {
    public static TabLayout tabLayout;
    public static ViewPager viewPager;
    public static int int_items = 3 ;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {



        /**
         *Inflate tab_layout and setup Views.
         */
            View x =  inflater.inflate(R.layout.tab_layout,null);
            tabLayout = (TabLayout) x.findViewById(R.id.tabs);
            viewPager = (ViewPager) x.findViewById(R.id.viewpager);

        /**
         *Set an Apater for the View Pager
         */
        viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));

        /**
         * Now , this is a workaround ,
         * The setupWithViewPager dose't works without the runnable .
         * Maybe a Support Library Bug .
         */

        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                    tabLayout.setupWithViewPager(viewPager);
                   }
        });

        return x;

    }

    class MyAdapter extends FragmentPagerAdapter {

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

        /**
         * Return fragment with respect to Position .
         */

        @Override
        public Fragment getItem(int position)
        {

            if(position == 0){
                return new FirstFragment();
            }
            if(position == 1){
                return new SecondFragment();
            }
            if(position == 2){
                return new ThirdFragment();
            }

        return null;
        }

        @Override
        public int getCount() {

            return int_items;

        }

        /**
         * This method returns the title of the tab according to the position.
         */

        @Override
        public CharSequence getPageTitle(int position) {

            switch (position){
                case 0 :
                    return "First";
                case 1 :
                    return "Second";
                case 2 :
                    return "Third";
            }
                return null;
        }
    }

}

那么我在哪里只能通过按第二个标签来说明Toast.makeText(getContext(), "Test", Toast.LENGTH_LONG).show();才能显示它?

2 个答案:

答案 0 :(得分:0)

一旦显示吐司,您就可以使用该代码终止吐司,这样当您点击另一个片段时它就不会显示。

试试这个

toast = new Toast(getContext());
toast.cancel(); //this will prevent toast from displaying multiple times

希望它能运作

答案 1 :(得分:0)

等待!如果你有多个标签,那么每个标签必须包含单独的片段,每个片段都有java类和布局文件,然后编码部分将在java文件的onCreate方法内,xml / layout部分将在.xml中文件就是这样。