0

I am trying to create an intro slider screen using fragments and ViewPager. I want to display multiple layouts in onCreateView method but don't know how to do it.

Any help please?

Main activity

public class MainActivity extends AppCompatActivity {
    public ViewPager pager;
    public The_fragment_adapter the_fragment_adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pager = (ViewPager) findViewById(R.id.pager);
        the_fragment_adapter = new The_fragment_adapter(getSupportFragmentManager());
        pager.setAdapter(the_fragment_adapter);
    }
}

Fragment Class

public class The_fragment extends Fragment {

    public The_fragment(){
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View my_view=inflater.inflate(R.layout.theui,container,false);
        return my_view;
    }
}

Adapter View

public class The_fragment_adapter extends FragmentStatePagerAdapter {

    public int[] the_layouts = {R.layout.page1,R.layout.page2};

    public The_fragment_adapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return new The_fragment();
    }

    @Override
    public int getCount() {
        return the_layouts.length;
    }
}
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
Junaid
  • 39
  • 7

1 Answers1

0

A ViewPager is for paging multiple fragments, not layouts. The getItem() method should return the fragment you want for the position you want.

Create a second fragment with the layout you want:

public Fragment The_second_fragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View my_view=inflater.inflate(R.layout.page2,container,false);
        return my_view;
    }
}

Then change your adapter methods:

@Override
public Fragment getItem(int position) {
    switch 0:
        return new The_fragment();
    switch 1:
        return new The_second_fragment();
}

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

See the excellent tutorial in the Android Developer's Guide. And, in the future if you follow the Java naming conventions you are more likely to get a better answer as other programmers will be able to understand your code more easily.

David Rawson
  • 20,912
  • 7
  • 88
  • 124
  • Thanks a lot.Well that's done.Now i just want to kill those fragments after using them.I am trying to use finish(); method but that's not working. – Junaid Dec 06 '16 at 02:48
  • You don't need to kill the fragments manually. They will be killed when your activity is destroyed. – David Rawson Dec 06 '16 at 02:50
  • I tried to put another activity and access it through the button on the second fragment.When i am on that activity and pressses the back button,it takes me back to the fragments – Junaid Dec 06 '16 at 03:04
  • Glad you got the multiple fragments working! I hope you can use one of the solutions [in this answer](https://stackoverflow.com/questions/8631095/android-preventing-going-back-to-the-previous-activity) to solve your second question. Good luck! – David Rawson Dec 06 '16 at 03:08