插入浮动操作按钮如何扩展片段

时间:2017-03-03 11:20:45

标签: java android

public class frag_aide extends Fragment {

    FloatingActionButton fab_profile,fab_face,fab_twi ;
    Animation FabOpen,FabClose,FabClockwise,FabantiClockwise;

    boolean isOpen=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fab_plus =(FloatingActionButton)findViewById(fab_plus);
        fab_nav=(FloatingActionButton)findViewById(fab_nav);
        fab_fb =(FloatingActionButton)findViewById(fab_fb);
        fab_twitte=(FloatingActionButton)findViewById(fab_twitte);

        FabOpen= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fab_open);
        FabClose= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fab_close);
        FabClockwise= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotation_clock);
        FabantiClockwise= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotation_anticlock);

        fab_twi.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {

                if (isOpen) {
                    fab_profile.startAnimation(FabClose);
                    fab_face.startAnimation(FabClose);
                    fab_twi.startAnimation(FabantiClockwise);
                    fab_profile.setClickable(false);
                    fab_face.setClickable(false);
                    isOpen = false;
                } else {
                    fab_profile.startAnimation(FabOpen);
                    fab_face.startAnimation(FabOpen);
                    fab_twi.startAnimation(FabClockwise);
                    fab_profile.setClickable(true);
                    fab_face.setClickable(true);
                    isOpen = true;
                }

            }
        });
    }
}

我在setContentViewfindViewByIdgetApplicationContext中遇到了问题。如何使用' extends Fragment'?

在片段中插入浮动操作按钮

4 个答案:

答案 0 :(得分:0)

fab_twitte=(FloatingActionButton)findViewById(fab_twitte); 

你在这里设置fab_twitte,但添加一个fab_twi.setOnClickListener的监听器fab_twi应该是fab_twitte。

主要问题是您为片段创建视图的方式。我确定你读过片段的生命周期,片段附着在片段上并与相应的活动分开。

不要在onCreate中夸大你的布局,使用onViewCreated,因为这相当于onCreate活动。使用片段中的onCreate来初始化不绑定布局的东西。

这是一个更详细的解释.Android片段有自己的生命周期非常类似于android活动。本节简要介绍其生命周期的不同阶段。 onAttach()片段实例与活动实例相关联。片段和活动未完全初始化。通常,您会在此方法中获得对使用该片段进行进一步初始化工作的活动的引用。

onCreate()系统在创建片段时调用此方法。您应该在片段暂停或停止时初始化要保留的片段的基本组件,然后重新开始。

onCreateView()当片段第一次绘制其用户界面时,系统会调用此回调。要为片段绘制UI,必须从此方法返回View组件,该组件是片段布局的根。如果片段没有提供UI,则可以返回null。

onActivityCreated()创建主机活动时,在onCreateView()方法之后调用onActivityCreated()。已创建活动和片段实例以及活动的视图层次结构。此时,可以使用findViewById()方法访问视图。例。在此方法中,您可以实例化需要Context对象的对象

onStart()一旦片段可见,就会调用onStart()方法。

onResume()片段变为活动状态。

onPause()系统将此方法称为用户离开片段的第一个指示。通常,您应该提交应该在当前用户会话之外保留的任何更改。

onStop()通过调用onStop()

来停止片段

onDestroyView()片段视图将在调用此方法后销毁

调用onDestroy()onDestroy()调用最终清理片段的状态,但不保证Android平台调用。

so just change from onCreate to onViewCreated ,plus fix the changes i stated in the aforementioned.

****也不使用getApplicationContext,使用getActivity()返回它所附加的当前活动的上下文。简单来说,getApplicationContext返回整个应用程序的上下文(所有活动都在里面运行的过程)的)。如果您需要与整个应用程序的生命周期相关联的上下文,而不仅仅是当前的Activity,请使用此代替当前的Activity上下文。所以不要不使用它。

答案 1 :(得分:0)

public class frag_aide extends Fragment {

        FloatingActionButton fab_nav,fab_fb,fab_twitte, fab_plus ;
        Animation FabOpen,FabClose,FabClockwise,FabantiClockwise;

        boolean isOpen=false;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            onCreateView(R.layout.aide);
            fab_plus = (FloatingActionButton) findViewById(fab_plus);
            fab_nav = (FloatingActionButton) findViewById(fab_nav);
            fab_fb = (FloatingActionButton) findViewById(fab_fb);
            fab_twitte = (FloatingActionButton) findViewById(fab_twitte);

            FabOpen = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fab_open);
            FabClose = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fab_close);
            FabClockwise = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotation_clock);
            FabantiClockwise = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotation_anticlock);

            fab_plus.setOnClickListener(new View.OnClickListener() {


                @Override
                public void onClick(View v) {

                    if (isOpen) {
                        fab_nav.startAnimation(FabClose);
                        fab_fb.startAnimation(FabClose);
                        fab_twitte.startAnimation(FabClose);
                        fab_plus.startAnimation(FabantiClockwise);
                        fab_nav.setClickable(false);
                        fab_fb.setClickable(false);
                        fab_twitte.setClickable(false);
                        isOpen = false;
                    } else {
                        fab_nav.startAnimation(FabOpen);
                        fab_fb.startAnimation(FabOpen);
                        fab_twitte.startAnimation(FabOpen);
                        fab_plus.startAnimation(FabClockwise);
                        fab_twitte.setClickable(true);
                        fab_nav.setClickable(true);
                        fab_fb.setClickable(true);
                        isOpen = true;
                    }

                }
            });
        }
}

答案 2 :(得分:0)

 @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //For you, you have nothing to do here
    }

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

         fab_plus = (FloatingActionButton) rootView.findViewById(fab_plus);
         fab_nav = (FloatingActionButton) rootView.findViewById(fab_nav);
         fab_fb = (FloatingActionButton) rootView.findViewById(fab_fb);
         fab_twitte = (FloatingActionButton) rootView.findViewById(fab_twitte);

        return rootView;
    }

@Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        FabOpen = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fab_open);
        FabClose = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fab_close);
        FabClockwise = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotation_clock);
        FabantiClockwise = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotation_anticlock);

        fab_plus.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {

                if (isOpen) {
                    fab_nav.startAnimation(FabClose);
                    fab_fb.startAnimation(FabClose);
                    fab_twitte.startAnimation(FabClose);
                    fab_plus.startAnimation(FabantiClockwise);
                    fab_nav.setClickable(false);
                    fab_fb.setClickable(false);
                    fab_twitte.setClickable(false);
                    isOpen = false;
                } else {
                    fab_nav.startAnimation(FabOpen);
                    fab_fb.startAnimation(FabOpen);
                    fab_twitte.startAnimation(FabOpen);
                    fab_plus.startAnimation(FabClockwise);
                    fab_twitte.setClickable(true);
                    fab_nav.setClickable(true);
                    fab_fb.setClickable(true);
                    isOpen = true;
                }

            }
        });
    }

答案 3 :(得分:0)

public class frag_aide extends Fragment {

    FloatingActionButton fab_nav, fab_fb, fab_twitte, fab_plus;
    Animation FabOpen, FabClose, FabClockwise, FabantiClockwise;

    boolean isOpen = false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //For you, you have nothing to do here
    }

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

        fab_plus = (FloatingActionButton) rootView.findViewById(fab_plus);
        fab_nav = (FloatingActionButton) rootView.findViewById(fab_nav);
        fab_fb = (FloatingActionButton) rootView.findViewById(fab_fb);
        fab_twitte = (FloatingActionButton) rootView.findViewById(fab_twitte);

        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        FabOpen = AnimationUtils.loadAnimation(getActivity(), R.anim.fab_open);
        FabClose = AnimationUtils.loadAnimation(getActivity(), R.anim.fab_close);
        FabClockwise = AnimationUtils.loadAnimation(getActivity(), R.anim.rotation_clock);
        FabantiClockwise = AnimationUtils.loadAnimation(getActivity(), R.anim.rotation_anticlock);
        fab_plus.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (isOpen) {
                    fab_nav.startAnimation(FabClose);
                    fab_fb.startAnimation(FabClose);
                    fab_twitte.startAnimation(FabClose);
                    fab_plus.startAnimation(FabantiClockwise);
                    fab_nav.setClickable(false);
                    fab_fb.setClickable(false);
                    fab_twitte.setClickable(false);
                    isOpen = false;
                } else {
                    fab_nav.startAnimation(FabOpen);
                    fab_fb.startAnimation(FabOpen);
                    fab_twitte.startAnimation(FabOpen);
                    fab_plus.startAnimation(FabClockwise);
                    fab_twitte.setClickable(true);
                    fab_nav.setClickable(true);
                    fab_fb.setClickable(true);
                    isOpen = true;
                }

            }
        });
    }
}