【问题标题】:how to set images from horizontal scrollview into the viewpager in android?如何将水平滚动视图中的图像设置到android中的viewpager中?
【发布时间】:2014-11-20 13:23:22
【问题描述】:

我编写了一个程序,在该程序中我显示图像的网格视图,当我单击图像时,会打开另一个活动,其中有一个 viewpager 可以滑动我的图像,并且在同一页面的底部还有一个水平滚动视图具有在我的 gridview 中看到的相同图像,直到这里一切正常,是的,所有图像都从我的 sdcard 中读取 现在,当我单击水平滚动视图中的图像时,在我的 viewpager 中显示的图像应该会发生变化,例如 s4 的画廊,所以我不知道该怎么做,需要一些帮助

我的代码:在我的 imageview.onclick 中,我已经用我的 viewpager 设置了当前的 Items 位置,但它不起作用

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;
}

}

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

【问题讨论】:

    标签: android gridview


    【解决方案1】:

    在下面的代码中,id似乎与viewpager的索引不同

     imageView.setOnClickListener(new View.OnClickListener() {
    
        @Override
        public void onClick(View v) {
            int id = v.getId();
            viewPager.setCurrentItem(id);
    
        }
    
    });
    

    或许您可以使用 arrayList 来放置所有图像并根据 arrayList 顺序创建水平滚动视图和 viewpager。所以,当你点击图片时,你会得到 Arraylist 的索引,然后你传递给 viewpager,它会设置它的当前片段,因为顺序总是一样的。

    【讨论】:

    • 是的,先生,但是您能根据我的程序为我提供解决方案吗?先生,我没有使用 fragmentpageradapter,我使用了 pageradapter,所以我的程序中没有片段
    • 没问题,你不需要片段,虽然 viewpager 是基于片段的。所以你必须做的只是订购图像的位置,你知道的。我需要将数据按照arraylist的顺序放入viewpager和横向scrollview中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多