我有一张带有照片的GridView。
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_me, container, false);
TextView t = (TextView) view.findViewById(R.id.textName);
name = MySharedPreferences.getInstance(getActivity()).getName();
t.setText(name);
gridView = (GridView) view.findViewById(R.id.gridPhoto);
img = new ImageAdapter(getActivity(), path);
gridView.setAdapter(img);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//code
}
});
requestPath();
return view;
}
在requestPath()中,使用图像的url设置ImageAdapter。
在onItemClick我想在网格中获取图像
这是ImageAdapter
public class ImageAdapter extends BaseAdapter {
private Context context;
private final LayoutInflater inflater;
private List<PathImage> path;
private String username = null;
public ImageAdapter(Context c, List<PathImage> path){
context = c;
this.path = path;
inflater = LayoutInflater.from(context);
}
public ImageAdapter(Context c, List<PathImage> path, String username){
context = c;
this.path = path;
inflater = LayoutInflater.from(context);
this.username = username;
}
@Override
public int getCount() {
return path.size();
}
@Override
public Object getItem(int position) {
return path.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ImageView imageView;
if (v == null) {
v = inflater.inflate(R.layout.gridview_item, parent, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
}
imageView = (ImageView) v.getTag(R.id.picture);
String url = "";
if(username == null) {
url = "http://example.com/image/" + MySharedPreferences.getInstance(context).getUsername() + "/";
}else{
url = "http://example.com/image/" + username + "/";
}
Glide.with(context).load(url + path.get(position).getPath()).into(imageView);
return v;
}
public void updatePath(List<PathImage> p){
path = p;
this.notifyDataSetChanged();
}
}
当我点击gridview时,我想在另一个片段中显示图像。
如何将图像从一个片段移动到另一个片段?
答案 0 :(得分:0)
,实现如下所示的客户回调:
private ImageViewClickListener mImageViewClickListener;
public interface ImageViewClickListener {
void onImageClicked(int position);
}
public void setImageViewClickListener (ViewClickListener viewClickListener) {
mImageViewClickListener = viewClickListener;
}
现在,在您的活动中,实现ImageViewClickListener接口并调用imageAdapter.setImageViewClickListener(this)并在getview中实现以下代码:
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mImageViewClickListener.onImageClicked(position);
}
});