-2

I wanted to have 80 pages in my app, that look like the following code. I can go from one page to the next over two buttons. Because I don't want to create 80 activities, I have to connect the 80 pages in one activity. How does that work?

 package com.example.xxx;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;

public class PictureOne extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pictureone);}

public void Picture0 (View view){
    Intent i = new Intent(this, PageZero.class);             
        startActivity(i);}}

public void Picture2 (View view){
    Intent i = new Intent(this, PageTwo.class);             
        startActivity(i);}}

XML:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >



    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerInParent="true"
        android:layout_marginLeft="14dp"
        android:src="@drawable/pic1" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:src="@drawable/left" 
        android:onClick="Picture0"/>

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:src="@drawable/right" 
        android:onClick="Picture2"/>

</RelativeLayout>
Knacks
  • 37
  • 6
  • 2
    You can use viewPager. `http://developer.android.com/reference/android/support/v4/view/ViewPager.html` – Anatol Sep 09 '14 at 20:26

1 Answers1

0

80 pages -> 80 activities, this is a really bad idea because it'll load ressources for nothing. What I suggest you is to take a look a the ViewPager widget (wich, with the swipe gesture, is really better in the idea of "Pages") and use a custom adapter to create programmatically wich page in a single class.

I usally create an Object class with contains every attributes I need and implement Fragment. Like this

public class QuestionStack extends Fragment {

    private String textQuestionStack;

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

        TextView messageTextView = (TextView) v.findViewById(R.id.title);
        messageTextView.setText(textQuestionStack);
        return v;
    }
}

Then my adapter :

public class QuestionStackAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;

    public QuestionStackAdapter (FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

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

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

And to viewpager object inside the activity class :

List<Fragment> fList = new ArrayList<Fragment>();
private QuestionStackAdapter pagerAdapter;
private ViewPager pager;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pagerAdapter = new QuestionStackAdapter(getSupportFragmentManager(), fList);

        // init Views
        pager = (ViewPager) findViewById(R.id.pagerQuestionStack);
        pager.setAdapter(pagerAdapter);
}

Now you souhld have something to try :)

marshallino16
  • 2,645
  • 15
  • 29