为什么片段不显示数据?

时间:2016-11-03 21:40:03

标签: android android-fragments android-viewpager fragmentstatepageradapter

我仍然处理Android问题而且我遇到了这个问题。

我想将一个片段ID(例如,从方法Fragment getItem(positon)中的int postion)传递给片段,这样它的值可以确定数组中的特定行 这是一些代码:

// collecting data to pass to fragment
        SoundArrayParcel arrayToSend = new SoundArrayParcel();
        arrayToSend.setInt(arrayImgSet1);

        bundleFrag.putParcelable("dataResId", arrayToSend);

        // Instantiate a ViewPager and a PagerAdapter.
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager(), bundleFrag);
        mPager.setAdapter(mPagerAdapter); ....

.... 这里我通过使用getItem()的int位置和getCurrentItem()传递片段Id的值(以测试我将使用其中一个或另一个实现的结果)

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {

    private final Bundle fragmentBundle;

    public ScreenSlidePagerAdapter(FragmentManager fm, Bundle data) {
        super(fm);

        fragmentBundle = data;

    }

    @Override
    public Fragment getItem(int position) {

        // Initialising new fragment
        final SoundFragAdapter f = new SoundFragAdapter();
        // bundle array position

        bundleFrag.putInt("fragIntIdP", position);
        bundleFrag.putInt("fragIntIdCI", mPager.getCurrentItem());
        Log.i("SoundActivity", "int position is : " + position);
        Log.i("SoundActivity", "Value currentItem is : "+ mPager.getCurrentItem());

        // passing data to fragment
        f.setArguments(fragmentBundle);
        return f;
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }
}

...(NUM_PAGES = 6)

这是SoundfragAdapter代码:

public class SoundFragAdapter extends Fragment {
private int fragPosition;
private MediaPlayer mp;
private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
        // Now that the sound file has finished playing, release the media player resources.
        releaseMediaPlayer();
    }
};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // get data from bundle passed by activity
    Bundle argsIn = getArguments();
    SoundArrayParcel arrayToReceive = (SoundArrayParcel) argsIn.getParcelable("dataResId");
    final int[][] arrayResId = arrayToReceive.getInt();
// get int fragment Id to use in array
    fragPosition = argsIn.getInt("fragIntIdP");


    // inflate the fragment
    final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.sounds_frag, container, false);


    // info array row def : img src; sound src; voice_src; toast_msg_src
    // fragIntIdP int value from bundle equal to frag position
    // fragIntIdCI int value frm bundle equal to currentItem frag positon

    Log.i("Rootview", "fragPosition is : " + fragPosition);

    // Find sound img src
    ImageView imageSound = (ImageView) rootView.findViewById(R.id.image_for_sound);
    //set img for corresponding fragment Id
    imageSound.setImageResource(arrayResId[fragPosition][0]);
    // Once the Image is clicked, the sound is played
    imageSound.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View rootview) {
            // Start playback.
            mp = MediaPlayer.create(getActivity(), arrayResId[fragPosition][1]);

            mp.start();
            // Once Sound has stopped release MediaPlayer resources
            mp.setOnCompletionListener(mCompletionListener);


            // Little msg confirming end of sound
            Toast.makeText(rootView.getContext(), arrayResId[fragPosition][3], Toast.LENGTH_SHORT).show();

        }
    });


    // Find voice img
    ImageView imageVoice = (ImageView) rootView.findViewById(R.id.image_for_voice);
    imageVoice.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View rootview) {
            // Start playback.
            mp = MediaPlayer.create(getActivity(), arrayResId[fragPosition][2]);

            mp.start();
            // Once Sound has stopped release MediaPlayer resources
            mp.setOnCompletionListener(mCompletionListener);


            // Little msg confirming end of sound
            Toast.makeText(rootView.getContext(), arrayResId[fragPosition][3], Toast.LENGTH_SHORT).show();

        }
    });


    return rootView;
}

问题是前两个滑动屏幕(片段Id 0和1)显示相同的图像。我知道前两个片段是为了性能目的而创建的,但是数组只接收第二个屏幕(片段)的值 ....日志中的以下摘录显示了这一点:

I/SoundActivity: int position is : 0
I/SoundActivity: Value currentItem is : 0
I/SoundActivity: int position is : 1
I/SoundActivity: Value currentItem is : 0
I/Rootview: fragPosition is : 1

那么如何才能得到第一个片段(Id:0)来显示数组的第一个图像而不是第二个图像(好像它(ViewPager?)完全跳过这个数据)....我真的已经没有了想法...而且我似乎做了什么让应用程序崩溃。

感谢您的任何建议!

0 个答案:

没有答案