0

i have a FragmentActivity with three Fragment and want to handle setOnClickListener in each of fragment with Different action, but Button return action for last fragment.

now , how possible to make Different action for button in distinct fragment?

in FragmentActivity :

    @Override
    public Fragment getItem(int position) {
        FrameLayout frameLayout=reg_next;
        switch (position) {
        case 0: // Fragment # 0 - This will show FirstFragment
            return o1.newInstance(0, "1",vpPager,ButtonNext);
        case 1: // Fragment # 0 - This will show FirstFragment different title
            return o2.newInstance(1, "2",vpPager,ButtonNext);
        case 2: // Fragment # 0 - This will show FirstFragment different title
            return o3.newInstance(2, "3");   
        default:
            return null;
        }
    }

in Fragments:

{
 ...
  ButtonNext.setOnClickListener(this);
 ...
}

In Fragment 1:

    @Override
public void onClick(View v) {
    Toast.makeText(getActivity(), "Fragment Number  1",0).show();

}

In Fragment 2:

    @Override
public void onClick(View v) {
    Toast.makeText(getActivity(), "Fragment Number 2",0).show();

}

But always return Fragment Number 2 setOnClickListener event ,Even when current fragment is first fragment (Fragment 1)

there are any way for multi-handling in distinct fragment?

Saeid
  • 2,261
  • 4
  • 27
  • 59

1 Answers1

1

In your case, I would do this:

  1. Define onClick listener in your activity, not in your fragments. This will act like a dispatcher.
  2. Set different ID to every button (in order to be able to distinguish them).
  3. Bind a variable to indicate the current fragment. Every time you load a fragment (I suppose in your getItem, but I'm not sure), set your variable to something which identifies the fragment (for instance .class, or a String value).
  4. With if/else select the action for the button depending on your "bind variable", and call a method declared in the fragment which handles the event.(for instance: buttonFragment3Clicked(View v)), declared in your third fragment.

I hope this helps!! :)

Juan Aguilar Guisado
  • 1,687
  • 1
  • 12
  • 21
  • i have just one button , that i want this button return different action in each fragment, i not have several button! – Saeid Oct 13 '14 at 21:53
  • So.. I didn't understand the question... is it a ViewPager? It's difficult for me to understand that the same button is in three fragments at the same time.. even in a ViewPager with three "independent" fragments. I suppose that the three fragments were shown at the same time. Please, give me more clues ;) – Juan Aguilar Guisado Oct 13 '14 at 22:02
  • OK , see :i have a ViewPager that load fragments and i put a button below ViewPager, OK? - now i want , when click On Button , for example if current fragment is fragment1 return a value and if is second fragment return another value - need more details? – Saeid Oct 13 '14 at 22:08
  • Okey, I think I got it. I'll definitely edit the answer. – Juan Aguilar Guisado Oct 13 '14 at 22:11
  • Take a look. Let me know if I missed something please. – Juan Aguilar Guisado Oct 13 '14 at 22:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62996/discussion-between-saeid-and-juan-aguilar-guisado). – Saeid Oct 13 '14 at 22:21
  • your answer not change yet. – Saeid Oct 13 '14 at 22:22