我正在尝试从用户选择的所选图像中获取图像URL,以便我可以将该字符串/ URL传递给我已设置的另一个片段,以便可以通过图像视图完全查看图像。
我怎样才能做到这一点?感谢。
这是SampleGridViewAdapter类:
inal class SampleGridViewAdapter extends BaseAdapter {
private final Context context;
public final List<String> urls = new ArrayList<String>();
public SampleGridViewAdapter(Context context) {
this.context = context;
// Ensure we get a different ordering of images on each run.
Collections.addAll(urls, Data.URLS);
Collections.shuffle(urls);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SquaredImageView view = (SquaredImageView) convertView;
if (view == null) {
view = new SquaredImageView(context);
view.setScaleType(CENTER_CROP);
}
// Get the image URL for the current position.
String url = getItem(position);
// Trigger the download of the URL asynchronously into the image view.
Picasso.with(context) //
.load(url) //
.placeholder(R.drawable.placeholder) //
.error(R.drawable.error) //
.fit() //
.into(view);
return view;
}
@Override
public int getCount() {
return urls.size();
}
@Override
public String getItem(int position) {
return urls.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
}
这是我的SampleGridViewActivity:
public class SampleGridViewActivity extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sample_gridview_activity,
container, false);
GridView Grid = (GridView) view.findViewById(R.id.grid_view);
Grid.setAdapter(new SampleGridViewAdapter(getActivity()));
Grid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long urls) {
// TODO Auto-generated method stub
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
Fragment openImage = new ImageViewPager();
transaction.replace(R.id.content_frame, openImage);
transaction.addToBackStack(null);
transaction.commit();
}
});
return view;
}
}
以下是我要将字符串发送到的类:
public class ImageViewer extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.imageviewpager, container, false);
ImageView IMGView = (ImageView) view.findViewById(R.id.imageView1);
Picasso.with(getActivity()).load("Pass URL into HERE").into(IMGView);
return view;
}
}
答案 0 :(得分:1)
您应该使用Argument
的{{1}}功能。
创建Fragments
时,您可以将Fragment
作为参数传递
URL
然后当你的Fragment openImage = new ImageViewPager();
Bundle b = new Bundle();
b.putString("url", DATA.URLs.get(position));
openImage.setArguments(b);
transaction.replace(R.id.content_frame, openImage);
被夸大时,你可以得到那个参数字符串
Fragment