问题描述
我写了一个程序,其中显示图像的网格视图,当我单击图像时,另一个活动打开,其中有一个视图滑动我的图像,并且同一页面底部还有一个水平滚动视图,具有相同的图像,这是在我的GridView中看到的,直到这里一切工作正常,并且所有的图像正在从我的SD卡读取
现在,当我点击我的水平滚动视图中的图像时,显示的图像在我的viewpager应该改变,例如,像s4的画廊,所以我没有得到如何做到这一点,需要一些帮助
我的代码:在我的imageview.onclick我使用ViewPager设置了当前的Items位置,但它不工作$ b private Utils utils;
私有FullScreenImageAdapter适配器;
private ViewPager viewPager;
LinearLayout myGallery;
ImageView iView;
int i;
字符串路径;
int id;
文件[]文件;
HorizontalScrollView scrollView;
int位置;
@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 =环境
.getExternalStorageDirectory()。getAbsolutePath();
String targetPath = ExternalStorageDirectoryPath +/ Pictures / raw;
Toast.makeText(getApplicationContext(),targetPath,Toast.LENGTH_LONG)
.show();
文件targetDirector =新文件(targetPath);
files = targetDirector.listFiles(); (文件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);
//首先显示所选图像
viewPager.setCurrentItem(position);
$ b $ private查看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(){
$ b $ @Override
public void onClick(View v){
int id = v。 getId();
viewPager.setCurrentItem(id);
}
});
layout.addView(imageView);
返回布局;
$ b $ public Bitmap decodeSampledBitmapFromUri(String path,int reqWidth,
int reqHeight){
位图bm = null;
//首先用inJustDecodeBounds = true解码以检查尺寸final
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path,options);
//计算inSampleSize
options.inSampleSize = calculateInSampleSize(options,reqWidth,
reqHeight);
//用inSampleSize解码位图set
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(path,options);
return bm;
}
}
任何建议都会有很大的帮助
感谢你
似乎在下面的代码中,id不是viewpager的相同索引
imageView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
int id = v.getId();
viewPager.setCurrentItem(id);
});
也许你可以使用arrayList来放置所有的图像,并创建水平滚动视图和viewpager arrayList顺序。所以,当你点击图片时,你将得到Arraylist的索引,然后你传递给viewpager,它将设置它的当前片段,因为顺序总是相同的。
i have written a program in which i am displaying a gridview of images and when i click on the image another activity opens which has a viewpager to slide my images and there is also a horizontal scrollview at the bottom of the same page which has the same images which is seen in my gridview , till here everything works fine , and yea all the images are being read from my sdcardNow when i click on the images inside my horizontal scrollview , the image which is displayed up in my viewpager should change , example , like the gallery of s4 , so i am not getting how to do it , need some help
My Code :Inside my imageview.onclick i have set the current Items position with my viewpager but its not working
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;
}
}
any suggestions will be of great help Thanking You
It seems that in this following code the id is not the same index of the viewpager
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int id = v.getId();
viewPager.setCurrentItem(id);
}
});
Parhaps you can use an arrayList to put all the images and create the horizontal scrollview and the viewpager according to the arrayList order. So, when you click on the image, you will get the index of the Arraylist, then you pass to the viewpager which will set its current fragment because the order will always be the same.
这篇关于如何将图像从水平滚动视图设置到android中的viewpager?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!