public class ViewPagerAdpater extends PagerAdapter { private List<View> views; public ViewPagerAdpater(List<View> views) { this.views = views; } /** * 判断是否显示的是同一张图片 */ @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } /** * 向viewgroup中添加对象 */ @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(views.get(position)); return views.get(position); } /** * 移除不显示的图片 */ @Override public void destroyItem(ViewGroup container, int position, Object object) { Log.e("destroyitem", position + ""); container.removeView(views.get(position)); } /** * 要显示图片的数量 */ @Override public int getCount() { // TODO Auto-generated method stub return views.size(); }}//报的错java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.//下面附上主程序,解释一下就是想做一个引导页,可总是只能显示一张图片,滑动屏幕就报错求大神解答!!public class MainActivity extends Activity implements OnPageChangeListener,OnClickListener{private ViewPager vp_views;private ViewPagerAdpater viewPagerAdpater;private List<View> views;private int[] imageresources={R.drawable.photo_4,R.drawable.photo_5,R.drawable.photo_6,R.drawable.photo_7};private ImageView[] paints;private LinearLayout ll_paints; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initviewPager(); initpaints(); vp_views.setOnPageChangeListener(this); } public void initpaints() { ll_paints=(LinearLayout) findViewById(R.id.ll_layout); paints=new ImageView[ll_paints.getChildCount()]; for (int i = 0; i < ll_paints.getChildCount(); i++) { paints[i]=((ImageView)ll_paints.getChildAt(i)); paints[i].setTag(i); paints[i].setImageResource(R.drawable.gc_step_unfocus); paints[i].setOnClickListener(this); } paints[0].setImageResource(R.drawable.gc_step_focused); } public void initviewPager() { vp_views=(ViewPager) findViewById(R.id.vp_views); views=new ArrayList<View>(); LayoutParams params=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); ImageView imageView=new ImageView(this); for(int i:imageresources){ imageView.setImageResource(i); Log.e("imageView", i+""); Log.e("imageView's parent", imageView.getParent()+""); imageView.setLayoutParams(params); views.add(imageView); } viewPagerAdpater=new ViewPagerAdpater(views); vp_views.setAdapter(viewPagerAdpater); } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageSelected(int arg0) { for (int i = 0; i < ll_paints.getChildCount(); i++) { paints[i]=((ImageView)ll_paints.getChildAt(i)); paints[i].setTag(i); paints[i].setImageResource(R.drawable.gc_step_unfocus); } paints[arg0].setImageResource(R.drawable.gc_step_focused); } @Override public void onClick(View v) { int i=(Integer) v.getTag(); vp_views.setCurrentItem(i); }}
添加回答
举报
0/150
提交
取消