ViewPager没有显示其他图像,也没有显示

时间:2014-06-27 23:36:55

标签: android adapter

是我的应用程序的简单viewpager,头痛。帮助将是apreciated。它只是在xml中带来了第一个默认图像,并没有带来其他图像,当我翻转它时它不会移动第二个图像。我使用的是小图片,而不是xml中提到的像素...正如你所看到的那样,我带上了ic_launcher,它确实会影响?....需要睡眠。对你们来说,轻松的声誉是我的应用程序的最后阶段。

我的ImageAdapter:

    package google.Bi;

import java.util.ArrayList;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class ImageAdapter extends PagerAdapter {
Context context;
int[] GalImages;

ImageAdapter(Context context, int[] GalImages) {
    this.context = context;
    this.GalImages = GalImages;
}

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

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

@Override
public Object instantiateItem(ViewGroup container, int position) {
    ImageView imageView = new ImageView(context);
    int padding = context.getResources().getDimensionPixelSize(
            R.dimen.activity_vertical_margin);
    imageView.setPadding(padding, padding, padding, padding);
    imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    imageView.setImageResource(GalImages[position]);
    container.addView(imageView);
    return imageView;
}

@Override  
public void destroyItem(View collection, int position, Object view) {  
    ((ViewPager) collection).removeView((LinearLayout) view);  
}  
}

我加载适配器的活动:

package google.Bi;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;

public class startactivivityAdapter extends Activity {
 //   Button b;
private int[] GalImages = new int[] { R.drawable.alta, R.drawable.baja,
        R.drawable.ic_launcher, R.drawable.ic_drawer, R.drawable.ic_action_good };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pageviewer);

    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    ImageAdapter adapter = new ImageAdapter(this, GalImages);
    viewPager.setAdapter(adapter);
    viewPager.setOnPageChangeListener(new MyPageChangeListener());
     //       b = (Button) findViewById(R.id.btn);
    /*b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
                   int id=ListOfID[indexOfImage];
                   WallpaperManager myWallpaperManager=WallpaperManager.getInstance(getApplicationContext());
                            myWallpaperManager.setResource(id);
        }
    }); */
}

private int indexOfImage = 0;

private class MyPageChangeListener extends
        ViewPager.SimpleOnPageChangeListener {
    @Override
    public void onPageSelected(int position) {
        indexOfImage = position;
    }
  }
}

我的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >





      <LinearLayout 
        android:id="@+id/layoutPager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:gravity="center_vertical|center_horizontal">

        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:visibility="gone"
            android:layout_width="350dp"
            android:layout_height="450dp"/>

        <ImageView
            android:id="@+id/mainImage"
            android:layout_width="200dp"
            android:layout_height="322dp"
            android:src="@drawable/main" />

    </LinearLayout>

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

更改以下内容

container.addView(imageView, 0);

container.addView(imageView);

0表示容器内的索引位置。因此,您实际上总是说第一个子视图设置为新图像。

destroyItem()需要删除您在instantiateItem()

中所做的观点

在xml中,您将ViewPager的可见性设置为gone。你有没有看到它?