无法在viewpager pageadapter中调用instantiateitems方法

时间:2017-06-29 20:38:43

标签: android android-viewpager android-pageradapter imageswitcher

尝试在Android活动中创建图像滑块时,我无法调用instantiateitem方法。没有显示任何错误,但图像不可见,这里是适配器代码:

public class CustomSwipeAdapter extends PagerAdapter
{

    ImageView imageView;

    private int[] image_resources={R.drawable.bus,R.drawable.cp,R.drawable.ty};
    private Context ctx;
    private LayoutInflater layoutInflater;
    @Override
    public int getCount() {
        return 0;
    }
    public CustomSwipeAdapter(Context ctx)
    {
        Log.d("**************","**************5");
        this.ctx=ctx;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view==(LinearLayout)object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position)
    {
        Log.d("**************","**************7");
        layoutInflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view=layoutInflater.inflate(R.layout.swipe_layout,null);
         imageView=(ImageView)view.findViewById(R.id.image_view1);
        imageView.setImageResource(image_resources[position]);
        ViewPager vp=(ViewPager)container;
        vp.addView(view,0);
        //container.addView(view);
        Log.d("**************","**************8");
        return view;

    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object)
    {
        ViewPager vp =(ViewPager)container;
        View view=(View)object;
        vp.removeView(view);
        //container.removeView((LinearLayout)object);
    }
}

1 个答案:

答案 0 :(得分:0)

你在getCount()中返回0。返回要显示的图像数量的长度,如下所示:

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