0
votes

this is my third day learning android apps. Currently using, android studio, my question pertains to the ViewPager class. Below is the code for my fragment and viewPager class.

ViewPager and Adapter Class:

package com.syedabdullah.syed.quran_memorization_application;

import android.content.pm.ActivityInfo; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.Window;

public class ViewPagerActivity extends FragmentActivity {

ViewPager myPager;
PagerAdapter myAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Remove Title bar
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    //force portrait orientation. (No landscape orientation).
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.activity_view_pager);

    myPager = (ViewPager) findViewById(R.id.pager);
    myAdapter = new adapterClass(getSupportFragmentManager());
    myPager.setAdapter(myAdapter);
}

/*
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_view_pager, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
} */

/*Nested class needed for custom Adapter */
private class adapterClass extends FragmentStatePagerAdapter {

    int[] images = new int[3];
    images[0] = R.drawable._2;

    String extract = "R.drawable._";



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

    @Override
    public Fragment getItem(int position) {
        //initialiseArray(images);
        return viewPageFragment.newInstance(position, images[position]);
    }

    @Override
    public int getCount() {
        return images.length;
    }

    private int[] initialiseArray(int[] array) {
        for (int i = 2; i <= array.length + 1; i++) {
            extract += i;
            array[i-2] = Integer.parseInt(extract);
        }
        return array;
    }
}

}

Fragment Class:

package com.syedabdullah.syed.quran_memorization_application;

/** * Created by syed on 12/25/2014. */

import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView;

/** * A placeholder fragment containing a simple view. */

public class viewPageFragment extends Fragment {

public viewPageFragment() {
}

public static viewPageFragment newInstance(int position, int imageId) {
    viewPageFragment myFrag = new viewPageFragment();
    Bundle myBundle = new Bundle();

    myBundle.putInt("imagePosition", position);
    myBundle.putInt("imageId", imageId);
    myFrag.setArguments(myBundle);

    return myFrag;
}

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

    int imageId = getArguments().getInt("imageId");
    ImageView myImageView = (ImageView) rootView.findViewById(R.id.imageView);
    myImageView.setImageResource(imageId);

    return rootView;
} }

Currently my initialiseArray doesn't work. If I were to initialise my array as follows:

int[] images = {R.drawable._2, R.drawable._3, R.drawable._4};

it would display the appropriate images in my ViewPager. However, I have more than 500 images, and if I do this, it would take forever. That is why I was wondering if there was an easier way to insert these images. All of my images are named in increasing order. so my first image is named ._2 all the way up to ._550 or something.

1 more question. if we can make an array like

int[] images = {R.drawable._2, R.drawable._3, R.drawable._4};

then how come this does not work?

int[] images = new int[3]; images[0] = R.drawable._2;

Can someone please explain? thanks.

1
The only problem is that I need all my images to cover the whole page, dunno if gallery can do it (only been doing android for 3 days. But I might check it out. ) - Abdullah
Quick question then, my app would require me to individually pinpoint certain positions inside these images. Is that possible in Gallery too? - Abdullah

1 Answers

0
votes

I figured out a way to efficiently add huge amounts of pages to ViewPager. Just posting an answer to any other new comer who might be facing a similar issue. What I was trying to do was add hundreds of images inside the application memory, which is very very very inefficient. The way its suppose to be done is you have to download the images (using AsynTask or DownloadManger) and store them on external storage, and then from there you are suppose to extract the images. Hope this helps.