我在发送来自随机函数的当前随机精确位置时遇到问题。我只想发送gridview
中的当前随机图像位置。目前,我在set onitemclicklistener
上使用gridview
,并通过放置多余的内容传递项目点击位置,但是在第二个活动中,当我收到放置多余的位置并通过毕加索将其显示为图像时,则图像位置没有同样,它显示了在字符串数组中给定的序列化字符串数组位置,但我想显示在gridview
中获得的相同随机位置。
这是我的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = findViewById(R.id.albumGrid);
random = new Random(); // or create a static random field...
CustomAdopter customAdopter=new CustomAdopter();
gridView.setAdapter(customAdopter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getApplicationContext(), SwipeView.class);
intent.putExtra("id", position);
startActivity(intent);
}
});
}
private class CustomAdopter extends BaseAdapter {
@Override
public int getCount() {
return ICONS.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.phototem, null);
ImageView imageView = view.findViewById(R.id.cover);
p=random.nextInt(ICONS.length);
Picasso.get().load(ICONS[p]).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(imageView);
return view;
}
}
}
在此活动中显示图像
public class SwipeView extends Activity
{
int positions;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe_view);
// get intent data
Intent i = getIntent();
// Selected image id
positions = i.getExtras().getInt("id");
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(positions);
}
private class ImagePagerAdapter extends PagerAdapter
{
String[] icons =MainActivity.icons ;
@Override
public int getCount()
{
return icons.length;
}
@Override
public boolean isViewFromObject(View view, Object object)
{
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position)
{
Context context = SwipeView.this;
ImageView imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Picasso.get().load(icons[position]).into(imageView);
container.addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object)
{
container.removeView((ImageView) object);
}
}
}