开发者

onClick inside fragment called on Activity

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 are开发者_如何学Go called on the Activity rather the fragment from the android system - this makes encapsulating a bit clumsy. Is there a way to have the reflection stuff from onClick to call on the fragment? The only solution to this I see at the moment is not to use onClick in the xml and set click-listeners inside the fragment via code.


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.


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.


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;
    }


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;
    }
}


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜