Fragment containing onclick
public class PreparacaoFragment extends Fragment implements View.OnClickListener {
public PreparacaoFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_preparacao, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
int[] BUTTON_IDS = {
R.id.btndia1,
R.id.btndia2,
R.id.btndia3,
R.id.btndia4,
R.id.btndia5,
R.id.btndia6,
R.id.btn_exercicios
};
List<FancyButton> buttons = new ArrayList<>();
for(int id : BUTTON_IDS) {
FancyButton button = view.findViewById(id);
button.setOnClickListener(this);
if (id != R.id.btn_exercicios){
button.setEnabled(false);
buttons.add(button);
}else{
button.setEnabled(true);
buttons.add(button);
}
}
for (int i = 0; i < 4 ; i++) {
buttons.get(i).setEnabled(true);
}
}
@Override
public void onClick(View view) {
DiaFragment fragmentdiapreparacao = new DiaFragment();
Bundle args = new Bundle();
int[] layouts;
switch (view.getId()){
case R.id.btndia1:
//SETAR LAYOUTS
layouts = new int[]{R.layout.dia1s1, R.layout.dia1s2, R.layout.dias1s3};
args.putIntArray("layouts",layouts);
break;
case R.id.btndia2:
//SETAR LAYOUTS
layouts = new int[]{R.layout.dia2s1};
args.putIntArray("layouts",layouts);
break;
case R.id.btndia3:
//SETAR LAYOUTS
layouts = new int[]{R.layout.dia3s1};
args.putIntArray("layouts",layouts);
break;
case R.id.btndia4:
//SETAR LAYOUTS
layouts = new int[]{R.layout.dia4s1};
args.putIntArray("layouts",layouts);
break;
case R.id.btndia5:
//SETAR LAYOUTS
layouts = new int[]{R.layout.dia5s1};
args.putIntArray("layouts",layouts);
break;
case R.id.btndia6:
layouts = new int[]{R.layout.dia6s1,R.layout.dia6s2};
args.putIntArray("layouts",layouts);
break;
case R.id.btn_exercicios:
Toast.makeText(getContext(), "Atividades", Toast.LENGTH_SHORT).show();
break;
}
fragmentdiapreparacao.setArguments(args);
if (!fragmentdiapreparacao.getArguments().isEmpty()){
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.contentmain, fragmentdiapreparacao);
ft.commit();
}
}
}
Final Fragment:
public class DiaFragment extends Fragment {
private static final String TAG = "igr";
private CustomViewPager viewPager;
private PlayPauseView btn_audio;
private FancyButton btn_prox;
int posicaolayoutaudio;
private int[] mlayouts;
private int[] mlayoutslayout;
int layoutaudioposicao;
private ViewGroup containerdia;
private boolean finalizar;
MediaPlayer mPlayer;
String url = "http://doupenglish.com.br/admin/audios/01%20Trilha%201.mp3";
public DiaFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mlayouts = getArguments().getIntArray("layouts");
mlayoutslayout = getArguments().getIntArray("layoutslayout");
return inflater.inflate(R.layout.diafragment, container, false);
}
@SuppressLint("ClickableViewAccessibility")
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
viewPager = view.findViewById(R.id.viewpagerdias);
viewPager.setPagingEnabled(false);
viewPager.setOffscreenPageLimit(1);
viewPager.setAdapter(new PagerAdapter() {
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view;
LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(mlayouts[position], null);
//botão proximo
btn_prox = view.findViewById(R.id.btn_prox);
btn_prox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mudarpagina();
}
});
if (mlayouts.length == 1){
botaofinalizar();
}
btn_audio = view.findViewById(R.id.btn_audio);
final PulsatorLayout pulsator = view.findViewById(R.id.pulsator);
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
btn_audio.toggle();
pulsator.stop();
}
});
try{
mPlayer.setDataSource(url);
mPlayer.prepare(); // might take long! (for buffering, etc)
} catch (IOException e) {
e.printStackTrace();
}
if (btn_audio != null){
btn_audio.bringToFront();
btn_audio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btn_audio.toggle();
if (mPlayer.isPlaying()){
pulsator.stop();
mPlayer.pause();
}else{
mPlayer.start();
pulsator.start();
}
}
});
}
container.addView(view);
return view;
}
@Override
public int getCount() {
return mlayouts.length;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
});
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
//Log.d(TAG, "onPageSelected: "+position);
//Log.d(TAG, "onPageSelected: "+mlayouts.length);
if (mlayouts.length-1 == position ) {
botaofinalizar();
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void botaofinalizar() {
btn_prox.setText(getString(R.string.terminar));
btn_prox.setIconResource("");
btn_prox.setBackgroundColor(Color.parseColor("#3b5998"));
finalizar = true;
}
private void mudarpagina() {
if (finalizar){
Log.d(TAG, "onClick: Entrou no finalizar");
Fragment fragment = new PreparacaoFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.contentmain, fragment);
ft.commit();
}else if (viewPager.getCurrentItem() != mlayouts.length){
viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
}
}
@Override
public void onDestroy() {
if (mPlayer != null) mPlayer.release();
super.onDestroy();
}
}