简体   繁体   English

为什么 instantiateItem 不调用并且 ViewPager 不工作?

[英]Why instantiateItem doesn't call and ViewPager doesn't work?

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/ .我使用这篇文章http://www.androidbegin.com/tutorial/android-viewpager-gallery-images-and-texts-tutorial/

This is my xml with viewpager: main.xml这是我的 xml 和 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这是我的 xml,带有图片: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这是我的适配器: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)这是我的活动:OneActivity.java(mArrays 充满图像)

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. instanceiteItem 不调用。

PS.Difference between my app and PS.我的应用程序和

article I have a few activity(and my arraylist initialized by items) Thank you文章我有一些活动(和我的 arraylist 由项目初始化)谢谢

#instantiateItem() is called whenever the ViewPager needs a new View.每当 ViewPager 需要新视图时,就会调用#instantiateItem() Initially, it'll call #instantiateItem() twice for the View in the center and view to the right if the adapter's getCount() > 0 .最初,如果适配器的getCount() > 0 ,它将为中心的 View 调用两次#instantiateItem()并向右查看。 That's why in your #setArray() method you need to call #notifyDataSetChanged() every time you change the array.这就是为什么在您的#setArray()方法中每次更改数组时都需要调用#notifyDataSetChanged()的原因。 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.如果您的数组在#onCreate()为空,那么#getCount() == 0这样 ViewPager 就会认为它是空的并且不会打扰。

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.我遇到了同样的问题 - 没有调用instantiateItem () 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:

viewPager.setAdapter(adapter)

For modern versions (2020+) where the error message is对于错误消息为的现代版本 (2020+)

Binary XML file line #(lineNumber): Error inflating class android.viewpager.widget.ViewPager at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)二进制 XML 文件行 #(lineNumber):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817) 处膨胀类 android.viewpager.widget.ViewPager 时出错

you might want to update你可能想要更新

android.support.v4.view.ViewPager to android.support.v4.view.ViewPager 到

androidx.viewpager.widget.ViewPager 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.当列表不为空时,它可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM