OnClick inside activity snippet

I am encapsulating material in a fragment at the moment and am facing a problem that is difficult for Google. Inside my fragment there are several buttons with onClick attributes, but they are called in Activity more like a fragment from the Android system - this makes encapsulation a little awkward. Is there a way for reflection objects from onClick to call a fragment? The only solution that I see at the moment is not to use onClick in xml and set click listeners inside the fragment using code.

+41
android android-fragments
Sep 27 '11 at 14:11
source share
6 answers

I talked to some googlers @ # adl2011 - they recognize the problem and maybe this will be fixed in the future. Until then - you need to use .setOnClick in the Snippet.

+57
Oct 18 '11 at 13:45
source share

The problem is that when the layout is inflated, it is still a hosting operation that receives the click of a button, not individual fragments.

I prefer to use the following solution to handle onClick events. This also works for Activity and Fragments.

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 the problem disappeared.

+8
Dec 04 '14 at 8:57
source share

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; } 
+5
Aug 01 '13 at 10:50 on
source share

QUESTION:

1. The onClick XML attribute will call the active activity method.

2. The raw public method.

3. Reuse of fragments.

DECISION:

In the Fragment Layout, add it to the view.

 android:onClick="onFragmentViewClick" 

In each activity, a 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 Snippet, enable this method.

 public void onViewClicked(View v) { switch (v.getId()) { case R.id.view_id: Log.e("onViewClicked", "yehhh!!"); break; } } 
+2
Dec 23 '15 at 11:18
source share

You may have a listener that is called in the operation that forwards the call to the listener in the fragment. You must have a link to the fragment inside the FragmentActivity to transfer the call. You will need to make a call to call the method or implement the fragment of the interface that you define. I know this is not the best solution, but it will work. You can also use the button tag to specify the name of the method to call, if you wish. Hope this helps a bit.

0
Sep 27 2018-11-11T00:
source share

Try it...

  @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
Jul 29 '16 at 9:12
source share



All Articles