我正在尝试在片段中添加ViewPager

时间:2019-07-27 21:32:47

标签: android-fragments android-viewpager

我试图在我的唯一片段中添加一个ViewPager(带有pagertabstrip),但是我搞砸了。在我的第二个活动中,带有viewpager的片段是通过onclicklistener触发的,但是当我单击时,什么也没有发生,片段根本无法打开,并且应用程序在几秒钟后崩溃。我已经将FragmentStatePagerAdapter配置为可与viewpager一起使用。

以下代码按此顺序排列:带有onClicklisterner> Fragment.java> Fragment.xml的配方活动。任何帮助将不胜感激。

//Onclicklistener to open Fragment
public class recipe_detail extends AppCompatActivity {



    ImageView mBakingGif;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recipe_detail);

        // clicklistener to open fragment
        mBakingGif.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                ft.add(R.id.frag_container, new VideoFragment());
                ft.commit();
            }
        });


    }

}

//Fragment class and Inner Adapter 
public class VideoFragment extends Fragment {


    private String mRecipeIntro;
    private String mPageTitle;
    private int position;


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


        return inflater.inflate(R.layout.video_fragment, container, false);

    }


    // Any view setup should occur here.  E.g., view lookups and attaching view listeners.
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {

        //Instantiate Cake object
        CakesItem cakesItem = Objects.requireNonNull(getActivity()).getIntent().getParcelableExtra("cake");


        //Create ArrayList to store number of steps in json file (not sure here!)
        final List<VideoFragment> tabs = new ArrayList<>();
        //get size of array to iterate through
        int tabsNo = cakesItem.getmSteps().size();
        for (int i = 0; i < tabsNo; i++) {
            VideoFragment videoFragment = new VideoFragment();
            Bundle bundle = new Bundle();
            videoFragment.setArguments(bundle);
            tabs.add(videoFragment);
        }


        //Setup for the Fragment ViewPager and FragmentStatePagerAdapter
        ViewPager vpPager = view.findViewById(R.id.vpPager);
        MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager(), tabs, mRecipeIntro, mPageTitle);
        vpPager.setAdapter(adapter);
        //end


    }

    //State pager adapter as Inner class.
    public static class MyPagerAdapter extends FragmentStatePagerAdapter {

        private final List<VideoFragment> tabs;
        private final String recipeIntro;
        private final String pageTitle;


        MyPagerAdapter(@NonNull FragmentManager fm, List<VideoFragment> tabs, String recipeIntro, String pageTitle) {
            super(fm);
            this.tabs = tabs;
            this.recipeIntro = recipeIntro;
            this.pageTitle = pageTitle;
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return tabs.get(position);
        }

        @Override
        public int getCount() {
            return tabs.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if (position == 0) {
                return recipeIntro;
            } else {
                return pageTitle + position;
            }
        }

    }


}

<!-- Layout for fragment--> 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/exo_content_frame"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:background="@android:color/darker_gray"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/frag_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/player_view"
        android:layout_width="match_parent"
        android:layout_height="255dp" />


    <TextView
        android:id="@+id/short_description_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/player_view"
        android:layout_marginTop="20dp"
        android:textSize="20sp"
        android:textStyle="bold"
        tools:text="Recipe introduction" />


    <TextView
        android:id="@+id/description_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/short_description_tv"
        android:layout_marginTop="20dp"
        android:textSize="18sp"
        tools:text="Recipe introduction" />


    <Button
        android:id="@+id/previous_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Prev" />

    <Button
        android:id="@+id/next_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:text="Next" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v4.view.ViewPager
            android:id="@+id/vpPager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v4.view.PagerTabStrip
                android:id="@+id/pager_header"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="top"
                android:paddingTop="4dp"
                android:paddingBottom="4dp" />

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


    </LinearLayout>


</RelativeLayout>

0 个答案:

没有答案