View Pager的活动未被称为Android屏幕幻灯片

时间:2016-08-11 19:26:53

标签: android android-fragments android-activity android-viewpager

最近开始使用Android开发,我正在创建一个屏幕幻灯片,该幻灯片将在选择一个图库(网格视图)后出现。我遵循Android Tutorial here然而我遇到的问题是ViewPager似乎无法工作(登录活动没有打印出来),我在新片段中看到的唯一的事情是一些文字。

下面我已经包含了代码,任何帮助都将不胜感激!

这是来自图库的初始调用,以显示滑块goToGallerySlider()

PhotoGalleryFragment.java

public class PhotoGalleryFragment extends BaseFragment {

    private static final String TAG = PhotoGalleryFragment.class.getSimpleName();

    public static final String EVENT_ID_EXTRA = "EventDetailFragment.EventIdExtra";

    private GridView grid;

    public List<EventPhoto> eventDetailPhotoList;

    @Bind(R.id.photoRoll)
    GridView photoRoll;

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

        View view = inflater.inflate(R.layout.fragment_event_detail_photo_gallery, container, false);

        grid = (GridView) view.findViewById(R.id.photoRoll);

        Context aContext = getActivity().getBaseContext();

        grid.setAdapter(new PhotoGalleryAdapter(aContext, eventDetailPhotoList));

        grid.setOnItemClickListener((parent, v, position, id) ->

                goToGallerySlider(parent, v, position, id));

        return view;
    }

    @Override
    public int getTitleResourceId() {
        return R.string.photo_gallery;
    }

    private void goToGallerySlider(ViewGroup parent, View v, int position, long id) {

        Log.d(TAG, "!!!We are in the goToGallerySlider() call function!");

        GallerySliderFragment sliderFragment = new GallerySliderFragment();
        sliderFragment.eventDetailPhotoList = eventDetailPhotoList;
        getBaseActivity().setFragment(sliderFragment, true);
    }
}

fragment_photo_gallery_slider.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView style="?android:textAppearanceMedium"
        android:id="@+id/textView_in_slider_fragment"
        android:padding="16dp"
        android:lineSpacingMultiplier="1.2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/lorem" />
</RelativeLayout>

activity_photo_gallery_slider.xml

    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v4.view.ViewPager>

GallerySliderFragment.java

public class GallerySliderFragment extends BaseFragment {

    private static final String TAG = GallerySliderFragment.class.getSimpleName();

    public List<EventPhoto> eventDetailPhotoList;

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

        Log.d(TAG, "!!!We are inside the onCreate in GallerySliderFragment");

        ViewGroup view = (ViewGroup) inflater.inflate(R.layout.fragment_event_detail_photo_gallery_slider, container, false);

        ((AngelPointsApplication) getActivity().getApplication()).getComponent().inject(this);
        ButterKnife.bind(this, view);

        return view;
    }

    public int getTitleResourceId() {return R.string.gallery_slider; }
}

PhotoGalleryActivity.java

public class PhotoGalleryActivity extends BaseActivity {

    private static final String TAG = PhotoGalleryFragment.class.getSimpleName();

    private ViewPager mPager;

    private PagerAdapter mPagerAdapter;

    private static final int NUM_PAGES = 5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.d(TAG, "!!!We are in the onCreate in PhotoGalleryActivity");

        setContentView(R.layout.activity_event_detail_photo_gallery_slider);

        mPager = (ViewPager) findViewById(R.id.viewPager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);
    }

    public void onBackPressed() {
        if (mPager.getCurrentItem() == 0) {
            Log.d(TAG, "!!!In PhotoGalleryActivity user pressed BACK Button on 1st Image");
            super.onBackPressed();
        } else {
            Log.d(TAG, "!!!In PhotoGalleryActivity user pressed BACK Button to see previous photo");
            mPager.setCurrentItem(mPager.getCurrentItem() - 1);
        }
    }


    /**
     * A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in
     * sequence.
     */
    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public android.support.v4.app.Fragment getItem(int position) {
            Log.d(TAG, "!!!In PhotoGalleryActivity get next item position");
            return new GallerySliderFragment();
        }

        @Override
        public int getCount() {
            Log.d(TAG, "!!!In PhotoGalleryActivity get count: " + NUM_PAGES);
            return NUM_PAGES;
        }
    }

}

0 个答案:

没有答案