I try to make simple application with sliding images. I use this article http://www.androidbegin.com/tutorial/android-viewpager-gallery-images-and-texts-tutorial/ .
This is my xml with viewpager: main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<android.support.v4.view.ViewPager
android:id="@+id/pages"
android:layout_height="match_parent"
android:layout_width="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
this is my xml with image: item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/image"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
this is my adapter: ViewerPagerAdapter.java
public class ViewerPagerAdapter extends PagerAdapter {
private Context mContext;
private ArrayList<CueModel> mArray;
private LayoutInflater mInflater;
private ImageView mImage;
public ViewerPagerAdapter(Context _context){
mContext = _context;
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void setArray(ArrayList<Bitmap> arr){
mArray = arr;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mInflater.inflate(R.layout.item, container, false);
mImage = (ImageView)itemView.findViewById(R.id.image);
mImage.setImageBitmap(mArray.get(position));
container.addView(itemView);
return itemView;
}
@Override
public int getCount() {
return mArray.size();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0.equals(arg1);
}
this is my activity: OneActivity.java (mArrays full of image)
public class OneActivity extends Activity {
ArrayList<Bitmap> mArrays;
ViewerPagerAdapter mAdapter;
ViewPager mPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main.xml);
mPager = (ViewPager) findViewById(R.id.pages);
mAdapter = new ViewerPagerAdapter(OneActivity.this);
mAdapter.setArrays(mArrays);
mPager.setAdapter(mAdapter);
}
}
I don't understand what I did wrong. instantiateItem doesn't call.
PS.Difference between my app and
article I have a few activity(and my arraylist initialized by items) Thank you
#instantiateItem()
is called whenever the ViewPager needs a new View. Initially, it'll call #instantiateItem()
twice for the View in the center and view to the right if the adapter's getCount() > 0
. That's why in your #setArray()
method you need to call #notifyDataSetChanged()
every time you change the array. You also need to call it every time you add or remove items from the list. If your array is empty in #onCreate()
, then #getCount() == 0
so the ViewPager thinks it's empty and won't bother.
I know this is very old post but I am answering this becoz someone would get benefited. I got the same issue - instantiateItem ()
was not getting called. After trying so many thing I manage to finally found the mistake I did in my code and it was a silly one I forgot to set adapter to viewpager:
viewPager.setAdapter(adapter)
For modern versions (2020+) where the error message is
Binary XML file line #(lineNumber): Error inflating class android.viewpager.widget.ViewPager at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
you might want to update
android.support.v4.view.ViewPager to
androidx.viewpager.widget.ViewPager
In my case, the size of the list was zero. Therefore, this method was not calling. When the list was not empty, it worked properly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.