0

我无法从资产文件夹加载图像。我想要做的是在用户单击按钮后图像将切换。我的资产文件夹中有 6 张图片,我想知道如何在每次点击后加载新图片。

本质上,我想要一个可以滚动图像的后退和下一个按钮。我想用 android-view Pager 来实现它。这有可能实现吗?有没有在线教程告诉我这个?现在我正在打电话

private int [] images = {R.drawable.airplane, R.drawable.bike,...

但我现在想将所有内容存储在资产文件夹中

public class MainActivity extends Activity {

private int [] images = {R.drawable.airplane, R.drawable.bike, R.drawable.boat,
        R.drawable.bus2,R.drawable.car,R.drawable.train};

private ViewPager mViewPager;
private MyPagerAdapter myPagerAdapter;
private Button nextBtn, preBtn;
private TextView positionText;
private int imageCount =images.length;

//HashMap<Integer, String> map = new HashMap<Integer, String>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    /* Initialize every object */

    //String whatString = map.get(R.drawable.airplane);

    myPagerAdapter = new MyPagerAdapter();

    nextBtn = (Button)findViewById(R.id.nextBtn);
    preBtn = (Button)findViewById(R.id.prevBtn);

    positionText= (TextView)findViewById(R.id.position);

    mViewPager =(ViewPager)findViewById(R.id.viewPager);
    mViewPager.setAdapter(myPagerAdapter);


    /* ViewPager's setCurrentItem(int) method is used to set first image */

    mViewPager.setCurrentItem(0);
    positionText.setText("1 of "+ imageCount);


    /* OnPageChangeListener */
    mViewPager.setOnPageChangeListener(new OnPageChangeListener()
    {

        @Override
        public void onPageSelected(int position) {

            positionText.setText((position+1) +" of "+imageCount);

            if (position==imageCount-1) {
                nextBtn.setEnabled(false);
            }
            else {
                nextBtn.setEnabled(true);
            }
            if (position==0){
                preBtn.setEnabled(false);
            }
            else {
                preBtn.setEnabled(true);
            }
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, 
                int positionOffsetPixels) {

        }

        @Override
        public void onPageScrollStateChanged (int state) {

        }
    });

    /* OnClickListener on buttons to change the image in viewpager */

    nextBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            mViewPager.setCurrentItem(mViewPager.getCurrentItem()+1);
        }


    });

    preBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            mViewPager.setCurrentItem(mViewPager.getCurrentItem()-1);
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

/* Pager Adapter for ViewPager */

private class MyPagerAdapter extends PagerAdapter {

    public MyPagerAdapter() {}


    @Override
    public int getCount() {
        return imageCount;

    }

    @Override
    public View instantiateItem(ViewGroup container, int position){

    ImageView mImageView = new ImageView(MainActivity.this);
    mImageView.setImageResource(images[position]);

    // add ImageView to ViewPager and return it

    container.addView(mImageView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

    return mImageView;

}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {

    container.removeView((View) object);
}


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

}

}

4

0 回答 0