Even include with button on different viewpager screens

Asked

Viewed 38 times

1

I have a include with a button, and I use it has 2 screens, inside a viewPager, it happens that when I do the click system, it clicks the include of the last screen, and not the screen 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;
            }
        });
  • 1

    Have you tried using the variable btn_prox locally rather than globally?

  • @Valdeirpsr worked, thank you

No answers

Browser other questions tagged

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