onClick inside fragment called on Activity

后端 未结 6 916
北恋
北恋 2020-12-02 22:14

i am encapsulating stuff into a fragment at the moment and run into a problem that is hard to google. Inside my fragment are some buttons with onClick attributes but they a

相关标签:
6条回答
  • 2020-12-02 22:53

    The problem is that when layout's are inflated it is still the hosting Activity that is receiving the button clicks, not the individual Fragments.

    I prefer using the following solution for handling onClick events. This works for Activity and Fragments as well.

    public class StartFragment extends Fragment implements OnClickListener{
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
    
            View v = inflater.inflate(R.layout.fragment_start, container, false);
    
            Button b = (Button) v.findViewById(R.id.StartButton);
            b.setOnClickListener(this);
            return v;
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.StartButton:
    
                ...
    
                break;
            }
        }
    }
    

    Then problem is gone.

    0 讨论(0)
  • 2020-12-02 23:07

    You could have the listener that is being called in the activity forward the call onto a listener in the fragment. You should have a reference to the fragment inside of the FragmentActivity to pass the call on. You will have to cast to call the method or have your fragment implement an interface you define. I know that isn't the best solution but it will work. You could also use the tag of a button to specify the method name to call if you wanted. Hope this helps a bit.

    0 讨论(0)
  • 2020-12-02 23:08

    ISSUE:

    1.In XML onClick attribute will call activity's public method.

    2.Fragment's public method not called.

    3.Fragment reusability.

    SOLUTION:

    In Fragment's layout add this to the View.

    android:onClick="onFragmentViewClick"
    

    In each activity the fragment may belong..

    public void onFragmentViewClick(View v) {
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.container);
        if (fragment != null && fragment.isVisible()) {
            if (fragment instanceof SomeFragment) {
                ((SomeFragment) fragment).onViewClicked(v);
            }
        }
    }
    

    In the Fragment include this method..

    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.view_id:
                Log.e("onViewClicked", "yehhh!!");
                break;
        }
    }
    
    0 讨论(0)
  • 2020-12-02 23:11

    I spoke to some googlers @ #adl2011 - they recognize the problem and perhaps there will be a fix of that in the future. Until then - one should use .setOnClick in the Fragment.

    0 讨论(0)
  • 2020-12-02 23:11

    Try this...

     @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView =  inflater.inflate(R.layout.activity_ipadditional_users, container, false);
    
        FloatingActionButton button7 = (FloatingActionButton) rootView.findViewById(R.id.fab_ip_additional_user);
        button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), IPAdditionalUsersEdit.class);
                startActivity(intent);
            }
        });
    
        return rootView;
    
    0 讨论(0)
  • 2020-12-02 23:18

    It works for me

    Add import:

        import android.view.View.OnClickListener;
    

    Fragment.java

        ...
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
    
           rootView = (ViewGroup) inflater.inflate(R.layout.fragment, container, false);
    
           Button mButton = (Button) rootView.findViewById(R.id.button);
           mButton.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
    
              }
           });
           return rootView;
        }
    
    0 讨论(0)
提交回复
热议问题