Even include with button on different viewpager screens

1

I have an include with a button, and use it has 2 screens, inside a viewPager, it happens that when I do the click listener, it clicks on the include of the last screen, not the screen that I clicked.

Include:          

    <com.ohoussein.playpause.PlayPauseView
        android:id="@+id/btn_audio"
        android:layout_width="85dp"
        android:layout_height="85dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:clickable="true"
        android:foreground="?android:selectableItemBackground"
        app:fill_color="#e1e1e1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:pause_bg="@color/colorPrimaryDark"
        app:play_bg="@color/colorPrimary" android:focusable="true"/>

    <pl.bclogic.pulsator4droid.library.PulsatorLayout
        android:id="@+id/pulsator"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:pulse_color="@color/logoazul"
        app:pulse_count="3"
        app:pulse_duration="8000"
        app:pulse_interpolator="AccelerateDecelerate"
        app:pulse_repeat="0"
        app:pulse_startFromScratch="true" />

</android.support.constraint.ConstraintLayout>

Java:

 viewPager = view.findViewById(R.id.viewpagerdias);
        viewPager.setPagingEnabled(false);
        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                View view;
                view = inflater.inflate(mlayouts[position], null);
                //botão proximo
                btn_prox = view.findViewById(R.id.btn_prox);
                if (btn_prox != null){
                    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);
                if (btn_audio != null){
                    pulsator = view.findViewById(R.id.pulsator);
                    btn_audio.bringToFront();
                    mPlayer = new MediaPlayer();
                    nomeaudio = Uteis.getnomeaudioporunidade(getContext(),unidadeselecionada);

                    if (Uteis.audiosexiste(getContext(),nomeaudio)) {
                        String musicUrl = getContext().getFilesDir().getAbsolutePath()+"/"+nomeaudio;
                        //Log.d(TAG, "musicurl: " + musicUrl);
                        FileInputStream fileInputStream;
                        try {
                            fileInputStream = new FileInputStream(musicUrl);
                            mPlayer.setDataSource(fileInputStream.getFD());
                            fileInputStream.close();
                            mPlayer.prepare();
                            audiopreparado = true;
                            //return  true;
                            btn_audio.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    if (mPlayer.isPlaying()){
                                        btn_audio.toggle();
                                        pulsator.stop();
                                        mPlayer.pause();
                                    }else{
                                        if (audiopreparado){
                                            btn_audio.toggle();
                                            mPlayer.start();
                                            pulsator.start();
                                        }
                                    }
                                }
                            });
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                            //return false;
                            //Toast.makeText(getContext(), "Audio não encontrado", Toast.LENGTH_SHORT).show();
                        } catch (IOException e) {
                            e.printStackTrace();
                            //return false;
                        }
                    } else {
                        Intent intent = new Intent(getContext(), CarregarAudio.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                    }
                }
                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;
            }
        });
    
asked by anonymous 03.01.2018 / 11:29

0 answers