Implement onClickListener method in a Fragment

Asked

Viewed 24 times

0

I am developing, in Android Studio, an application about music. In this application, I have two Fragments: Home Fragment and Genres Fragment.
Inside Genres Fragment, I have 4 Imagebutton and I want to add an event, that is, when you click a button, open another Fragment.

Therefore, I already have the code of the onClickListener method inside the Java file of that Fragment. I also have a function with the Switch operator, but I don’t know what to put inside the case of each button. I’ve been looking for a solution to this for days.... I’ve tried with replace() and it hasn’t. Besides, this is a project for the school and it’s a little urgent

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_genres, container, false);

    ImageButton rapBtn = (ImageButton)v.findViewById(R.id.RapButton);
    ImageButton popBtn = (ImageButton)v.findViewById(R.id.PopButton);
    ImageButton edmBtn = (ImageButton)v.findViewById(R.id.EDMButton);
    ImageButton rockBtn = (ImageButton)v.findViewById(R.id.RockButton);
    rapBtn.setOnClickListener(this);
    popBtn.setOnClickListener(this);
    edmBtn.setOnClickListener(this);
    rockBtn.setOnClickListener(this);
    return v;
}

@Override
public void onClick(View v) {
    switch (v.getId())
    {
        case R.id.PopButton:
            // O que ponho aqui?

            break;
        case R.id.EDMButton:
            break;

        case R.id.RockButton:
            break;

        case R.id.RapButton:
            break;
    }
}
  • @ramaral I put an image of the code in question

  • Want to open a Fragment in each case?

  • @ramaral I want to know the code to put in each case so I can open a Fragment when I click a button. I’ve tried several hypotheses, but without success

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.