1

Edit: Hey guys I have deleted everything because I finally found the answer to my question (basically how to use a scrollview inside ViewPager inside a fragment). Here is my code for everything. I am a total noob so I don't really understand what is going on in my code but here it is:

Here is my pager adapter to make fragments go inside ViewPager(I think that is what happens) I took this code from online from CommonsGuy

import android.app.Fragment;
import android.app.FragmentManager;
import android.support.v13.app.FragmentPagerAdapter;
import java.util.List;


public class PagerAdapter extends FragmentPagerAdapter {

  private List < Fragment > fragments;


  public PagerAdapter(FragmentManager fm, List < Fragment > fragments) {
    super(fm);

    this.fragments = fragments;
  }

  @Override
  public Fragment getItem(int i) {
    return fragments.get(i);
  }

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

Here is my code for the fragment that I will put the ViewPager on

public class RecipeFragment extends Fragment {


  public RecipeFragment() {
    // Required empty public constructor
  }


  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_recipe, container, false);


    List < Fragment > fragments = new Vector < > ();
    fragments.add(Fragment.instantiate(getActivity(), RecipeOverview.class.getName()));
    fragments.add(Fragment.instantiate(getActivity(), RecipeIngredients.class.getName()));
    fragments.add(Fragment.instantiate(getActivity(), RecipeProcedures.class.getName()));
    PagerAdapter adapter = new PagerAdapter(getChildFragmentManager(), fragments);
    ViewPager pager = (ViewPager) view.findViewById(R.id.pager);
    pager.setAdapter(adapter);


    return (view);
  }

}

And below are the xml files for RecipeFragment and the different fragments shown in the ViewPager respectively

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

Layout for mini fragments inside ViewPager. And the .java mini fragment file is just a normal fragment file. I also used nestedScrollView because scrollview say i am stupid person and not working properly.

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".RecipeOverview">


    <android.support.v4.widget.NestedScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_margin="10dp">

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="10dp"
                android:text="Ingredients"
                android:textStyle="bold"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Some Button" />


            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Some Button" />


            <Button
                android:id="@+id/button"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Some Button" />


            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Some Button" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="10dp"
                android:text="Random Text"
                android:textStyle="bold"/>

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="10dp"
                android:text="Fragment1"
                android:textStyle="bold"/>

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="10dp"
                android:text="Random Text"
                android:textStyle="bold"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Some Button" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Some Button" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Some Button" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Some Button" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Some Button" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="10dp"
                android:text="Ingredients"
                android:textStyle="bold"/>

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="10dp"
                android:text="Ingredients"
                android:textStyle="bold"/>

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="10dp"
                android:text="Ingredients"
                android:textStyle="bold"/>

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="10dp"
                android:text="Ingredients"
                android:textStyle="bold"/>

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="10dp"
                android:text="Ingredients"
                android:textStyle="bold"/>


        </LinearLayout>


    </android.support.v4.widget.NestedScrollView>

</android.support.constraint.ConstraintLayout>
Harrison
  • 21
  • 7

2 Answers2

0

The OnClick Methods(toFirst,toSecond) you defined in the Fragment needs to be defined in your Activity(Probably the MainActivity).

glm9637
  • 894
  • 9
  • 20
0

Change these methods

public void toFirst(){
        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        Fragment1Page1 fragment = new Fragment1Page1();
        ft.replace(R.id.contentLayoutFragment1Page1, fragment);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.addToBackStack(null);
        ft.commit();
    }
    public void toSecond(){
        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        Fragment1Page2 fragment = new Fragment1Page2();
        ft.replace(R.id.contentLayoutFragment1Page2, fragment);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.addToBackStack(null);
        ft.commit();
    }

to

public void toFirst(View view){
        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        Fragment1Page1 fragment = new Fragment1Page1();
        ft.replace(R.id.contentLayoutFragment1Page1, fragment);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.addToBackStack(null);
        ft.commit();
    }
    public void toSecond(View view){
        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        Fragment1Page2 fragment = new Fragment1Page2();
        ft.replace(R.id.contentLayoutFragment1Page2, fragment);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.addToBackStack(null);
        ft.commit();
    }

as you are supplying this methods in the xml this will pass the view target to those method , since it is missing from your code you are getting the error as logcat says

Nitin Mesta
  • 1,504
  • 19
  • 32
  • Hello unfortunately this doesn't work. This is what is says in the logcat FATAL EXCEPTION: main Process: com.harrison.recipetest2, PID: 7718 java.lang.IllegalStateException: Could not find method toFirst(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'toFirst' at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) – Harrison Jul 06 '17 at 06:33
  • Can you update the final code of your Fragment class what you have written – Nitin Mesta Jul 06 '17 at 09:43