An incomprehensible problem. When you open the Activity loses the letter A and the letter B.

Activity class

public class AlphabetActivity extends AppCompatActivity { ImageButton btnBack; ImageButton btnPrev; ImageButton btnNext; ViewPager mViewPager; List<Letter> data = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_alphabet); AlphabetPagerAdapter mTextPagerAdapter = new AlphabetPagerAdapter( getSupportFragmentManager(), getData()); mViewPager = (ViewPager) findViewById(R.id.alphabetPager); mViewPager.setAdapter(mTextPagerAdapter); } private List<Letter> getData() { List<Letter> data = new ArrayList<>(); data.add(new Letter("А", "bukva_1_big.png", "bukva_1_little.png", "Арбуз", "bukva_1.mp3")); data.add(new Letter("Б", "bukva_2_big.png", "bukva_2_little.png", "Банан", "bukva_2.mp3")); data.add(new Letter("В", "bukva_3_big.png", "bukva_3_little.png", "Волк", "bukva_3.mp3")); return data; } } 

Adapter class

 public class AlphabetPagerAdapter extends FragmentStatePagerAdapter { List<Letter> data; public AlphabetPagerAdapter(FragmentManager fm, List<Letter> data) { super(fm); this.data = data; } @Override public Fragment getItem(int i) { Fragment fragment = new AlphabetFragment(); Bundle args = new Bundle(); args.putString(AlphabetFragment.ARG_LETTER, data.get(i).getLetter()); args.putString(AlphabetFragment.ARG_IMAGE_BIG, data.get(i).getImageBig()); args.putString(AlphabetFragment.ARG_IMAGE_LITTLE, data.get(i).getImageLittle()); args.putString(AlphabetFragment.ARG_DESCRIPTION, data.get(i).getDescription()); args.putString(AlphabetFragment.ARG_AUDIO, data.get(i).getAudio()); args.putInt(AlphabetFragment.ARG_POSITION, i); fragment.setArguments(args); return fragment; } @Override public int getCount() { return data.size(); } @Override public CharSequence getPageTitle(int position) { return "Item " + (position + 1); } } 

Fragment class

 public class AlphabetFragment extends Fragment { public static final String ARG_LETTER = "letter"; public static final String ARG_DESCRIPTION = "description"; public static final String ARG_IMAGE_BIG = "imageBig"; public static final String ARG_IMAGE_LITTLE = "imageLittle"; public static final String ARG_AUDIO = "audio"; public static final String ARG_POSITION = "item_position"; String mSound; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate( R.layout.alphabet_fragment, container, false); Bundle args = getArguments(); ImageView imgLetter = (ImageView) rootView.findViewById(R.id.imgLetter); imgLetter.setImageBitmap(getBitmapFromAssets(args.getString(ARG_IMAGE_BIG))); mSound = args.getString(ARG_AUDIO); play(mSound); imgLetter.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { play(mSound); return false; } }); return rootView; } private Bitmap getBitmapFromAssets(String filename) { AssetManager assetManager = this.getContext().getAssets(); Bitmap bitmap = null; try { InputStream in = assetManager.open("img/alphabet/" + filename); bitmap = BitmapFactory.decodeStream(in); return bitmap; } catch (IOException ex) { ex.printStackTrace(); } return bitmap; } void play(String filename) { try { AssetFileDescriptor afd = this.getContext().getAssets().openFd("sound/alphabet/" + filename); MediaPlayer mp = new MediaPlayer(); mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); mp.setLooping(false); mp.prepare(); mp.start(); mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mediaPlayer) { mediaPlayer.release(); } }); } catch (IOException ex) { ex.printStackTrace(); } } } 

Where did I make a mistake? Half an hour trying to figure out

    1 answer 1

    In the onCreateView method onCreateView you call play(mSound);

    ViewPager is designed so that it prepares images of neighboring pages in advance, so you can hear the sounds of alternately prepared Fragment .

    Moreover, if you had chosen the Fragment with the letter Б in advance, the order of sounding would be Б - Б - А Because the selected Fragment would be created first, then the next in the list from the first, and then the previous in the list from the first.

    In the previous answer, I said that using the Listener in ViewPager will help you a lot. If you want to play a letter on the screen when switching Fragment -a, then set the playback method to this Listener :

     viewPager.addOnPageChangeListener(new SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { play(mSound); // адаптируйте под свой код } }; 
    • And where to call the play () method? And there is such a problem, I think everything is because of the same. I switched to the letter B and go back (using your method for the Back button) and he throws it to the letter I - Christina
    • with the letter B then I am my cant, I corrected the code in the condition to the button back in the answer, now there is if (previousPosition> = 0), but there was just “more” - Artilirium
    • And how to solve the problem with audio playback twice? - Christina
    • I understand addOnPageChangeListener should be added to the AlphabetActivity itself. And then how now to pull out the mSound variable? After all, the value in it is written in the fragment - Kristina
    • mSound = mTextPagerAdapter.data.get (mViewPager.getCurrentItem ()). getAudio (); So? Where getAudio () method from the auxiliary class Letter - Cristina