如何在pageradapter中添加imageview和按钮

时间:2013-12-05 19:46:25

标签: android android-viewpager

我有寻呼机适配器,我使用imageview显示在寻呼机,但我也想在寻呼机上图像和按钮...如何实现,请帮助我。我是android的新手......

我的寻呼机类代码如下:

public class FullScreenImageActivity extends Activity {
private static int NUM_VIEWS = 5;

private MyPagerAdapter adapter;
private ViewPager pager;
int gotbiscuit;
public String TAG = "hello";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fullscreen_view);

    Intent i = getIntent();
    int gotbiscuit = i.getExtras().getInt("position");

    adapter = new MyPagerAdapter(this);
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);

}

public class MyPagerAdapter extends PagerAdapter {
    public Integer[] images1 = { R.drawable.i_3, R.drawable.i_4,
            R.drawable.i_6 };
    public Integer[] images2 = { R.drawable.i_8, R.drawable.i_9,
            R.drawable.i_10 };

    public Context mContext;

    public MyPagerAdapter(Context context) {
        super();
        this.mContext = context;
        // TODO Auto-generated constructor stub
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return images1.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        // TODO Auto-generated method stub
        return view == ((ImageView) object);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object view) {
        // TODO Auto-generated method stub
        ((ViewPager) container).removeView((ImageView) view);
    }

    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        // TODO Auto-generated method stub

        ImageView view1 = new ImageView(FullScreenImageActivity.this);

        view1.setScaleType(ScaleType.CENTER_CROP);

        switch (gotbiscuit) {

        case 0:
            view1.setImageResource(images1[position]);
            break;
        case 1:
            view1.setImageResource(images2[position]);
            break;
        }
        view1.setAdjustViewBounds(true);

        ((ViewPager) container).addView(view1, 0);

        return view1;

    }
}

}

1 个答案:

答案 0 :(得分:0)

  1. 使用parentlayout和图像和按钮或
  2. 内部扩展xml资源
  3. 动态创建parentlayout(f.e。线性布局)和addView(图像)和(按钮)
  4. 充气:

        LayoutInflater inflater = LayoutInflater.from(container.getContext());
        View view = inflater.inflate(R.layout.pager,null);
        ((ViewPager) container).addView(view,0);
        return view;