我有一個GridView與照片。
從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()中設置的ImageAdapter與圖像的URL。
在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控件。
如何將圖像從一個片段移動到另一個片段?
檢查此[回答](https://stackoverflow.com/a/12739968/5343320)。 – huk
當我點擊圖像時,如何獲取圖像視圖? – Ibernato933
您不必獲取imageview。您可以通過getvView方法獲取點擊imageview的位置。在你的getview中實現onclicklistner到imageview。這會給你的位置和位置,你可以得到像這樣的值:url + path.get(position).getPath(),然後傳遞給你的片段,並應用相同的邏輯來加載圖像像你在getview中使用使用滑翔。 – huk