使用片段内的视图寻呼机进行图像滑动

时间:2017-08-17 06:18:32

标签: android android-fragments fragmentstatepageradapter

我正在尝试在片段内创建一个滑动图像。我使用view pager创建了选项卡式活动,这很好用。在其中一个标签中,我正在尝试创建图像滑动。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view= inflater.inflate(R.layout.fragment_collection_fragment1, container, false);
    ViewPager viewPager= (ViewPager)view.findViewById(R.id.collection1_viewPager);

    ImageSwipeAdapter imageSwipeAdapter= new ImageSwipeAdapter(getActivity().getSupportFragmentManager());
    viewPager.setAdapter(imageSwipeAdapter);
    return view;
}

部署后,应用程序停止工作。

在一些互联网内容中,提到使用

getChildFragmentManager()

但它也没有用。代码有什么问题?

图像滑动的适配器代码

public class ImageSwipeAdapter extends FragmentStatePagerAdapter {
public ImageSwipeAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    Fragment f= new ImageSwipe();
    Bundle args= new Bundle();
    args.putInt("count", position + 1);
    f.setArguments(args);
    return f;
}

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

}

片段XML -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com">

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="500dp"
    android:id="@+id/collection1_viewPager">

</android.support.v4.view.ViewPager>

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginStart="160dp"
    android:layout_marginTop="203dp"
    android:text="TextView" />

0 个答案:

没有答案
相关问题