I want to use three previously created Activities in a ViewPager (with PagerAdapter) so the user can scroll smoothly horizontally through them.
I followed a tutorial which worked great. The problem is in the tutorial they use TextViews to demonstrate. I already have finished Activities (of which the layouts are in XML files). I want to use these Activities in that slider now but it looks like I can only use Views for that.
I could not figure out how I have to change the code of the classes (from “implements Activity” to “extends View”) that I can use it in the slider.
The my current code looks like this:
public class HorizontalSliderBeispielActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cxt = this;
awesomeAdapter = new AwesomePagerAdapter();
awesomePager = (ViewPager) findViewById(R.id.awesomepager);
awesomePager.setAdapter(awesomeAdapter);
}
...
then the inner class with the PageAdapter:
...
private class AwesomePagerAdapter extends PagerAdapter {
public Object instantiateItem(View collection, int position) {
TextView tv = new TextView(cxt);
tv.setText("Bonjour PAUG " + position);
tv.setTextColor(Color.WHITE);
tv.setTextSize(30);
view_01 = new SubmitCheatInstructions(cxt);
((ViewPager) collection).addView(tv, 0);
((ViewPager) collection).addView(view_01 , 1);
return tv;
}
}
Instead of that TextView “tv” I want to use Activities (i.e. SubmitCheatInstructions). Previously this class looked like:
public class SubmitCheatInstructions implements Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.submitcheat_instructions);
}
}
but as far as I know I have to change it to
public class SubmitCheatInstructions extends View {
????
}
in order to be able to use it for the ViewPager.
My problem now is that I want to load the layout from the layout xml file (submitcheat_instructions.xml) into that view and not do everything in code. I could not figure out how I have to do this.
Thanks for any help.
I followed the same tutorial and like you i was unable to achieve this at first but after some tinkering and trial and error the following code worked like a charm:
Hope this helps, Good luck
Shereef Marzouk