如何将图像从水平scrollview设置到android中的viewpager?

时间:2014-11-20 13:23:22

标签: android gridview

我写了一个程序,其中我正在显示图像的网格视图,当我点击图像时,另一个活动打开,其中有一个viewpager来滑动我的图像,同一页面底部还有一个水平滚动视图有我在gridview中看到的相同图像,直到这里一切正常,所有图像都是从我的SD卡中读取的 现在,当我点击水平滚动视图中的图像时,我的viewpager中显示的图像应该更改,例如,如s4的图库,所以我没有得到如何做,需要一些帮助

我的代码:在我的imageview.onclick中,我已经使用我的viewpager设置了当前的项目位置,但它无法正常工作

public class FullScreenViewActivity extends Activity {

private Utils utils;
private FullScreenImageAdapter adapter;
private ViewPager viewPager;
LinearLayout myGallery;
ImageView iView;
int i;
String path;
int id;
File[] files;
HorizontalScrollView scrollView;
int position;

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

    viewPager = (ViewPager) findViewById(R.id.pager);
    myGallery = (LinearLayout) findViewById(R.id.mygallery);
    scrollView = (HorizontalScrollView) findViewById(R.id.horizontal1);
    scrollView.setBackground(getResources().getDrawable(R.drawable.border));
    String ExternalStorageDirectoryPath = Environment
            .getExternalStorageDirectory().getAbsolutePath();

    String targetPath = ExternalStorageDirectoryPath + "/Pictures/raw";
    Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG)
            .show();
    File targetDirector = new File(targetPath);

    files = targetDirector.listFiles();

    for (File file : files) {

        myGallery.addView(insertPhoto(file.getAbsolutePath()));

    }

    utils = new Utils(getApplicationContext());

    Intent i = getIntent();
    position = i.getIntExtra("position", 0);

    adapter = new FullScreenImageAdapter(FullScreenViewActivity.this,
            utils.getFilePaths());

    viewPager.setAdapter(adapter);

    // displaying selected image first
    viewPager.setCurrentItem(position);
}

private View insertPhoto(final String path) {

    final Bitmap bm = decodeSampledBitmapFromUri(path, 220, 220);

    LinearLayout layout = new LinearLayout(getApplicationContext());
    layout.setLayoutParams(new LayoutParams(250, 250));
    layout.setGravity(Gravity.CENTER);

    ImageView imageView = new ImageView(getApplicationContext());
    imageView.setLayoutParams(new LayoutParams(220, 220));
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setImageBitmap(bm);

    // imageView.setId(i);
    // iView.setId(i);
    // viewPager.setId(i);

    imageView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            int id = v.getId();
            viewPager.setCurrentItem(id);

        }

    });

    layout.addView(imageView);

    return layout;
}

public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth,
        int reqHeight) {

    Bitmap bm = null;

    // First decode with inJustDecodeBounds=true to check dimensions final
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    bm = BitmapFactory.decodeFile(path, options);

    return bm;
}

}

任何建议都会有很大帮助 感谢你

1 个答案:

答案 0 :(得分:2)

在以下代码中,似乎id与viewpager

的索引不同
 imageView.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        int id = v.getId();
        viewPager.setCurrentItem(id);

    }

});

您可以使用arrayList放置所有图像,并根据arrayList顺序创建水平scrollview和viewpager。因此,当您单击图像时,您将获得Arraylist的索引,然后传递给viewpager,它将设置其当前片段,因为顺序将始终相同。