onClick inside fragment called on Activity

2019-01-10 22:06发布

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 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.

6条回答
祖国的老花朵
2楼-- · 2019-01-10 22:41

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.

查看更多
我只想做你的唯一
3楼-- · 2019-01-10 22:42

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;
    }
查看更多
Animai°情兽
4楼-- · 2019-01-10 22:44

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;
查看更多
趁早两清
5楼-- · 2019-01-10 22:45

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.

查看更多
forever°为你锁心
6楼-- · 2019-01-10 22:52

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.

查看更多
该账号已被封号
7楼-- · 2019-01-10 22:55

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;
    }
}
查看更多
登录 后发表回答