0


i have a GridView with a photos.

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;
    }

In requestPath() set the ImageAdapter with the url of the images.
In onItemClick I would like to get the image in the grid
This is 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();
    }

}

I would like to display the images in another fragment when I click on gridview.
How do I move an image from one fragment to another?

  • Check this [answer](https://stackoverflow.com/a/12739968/5343320). – huk Jul 28 '17 at 19:20
  • How do I get the imageview when I click on an image? – Ibernato933 Jul 28 '17 at 19:23
  • You don't have to get the imageview. You can get the location on clicking imageview from getvView method. Implement onclicklistner to imageview in your getview. This will give you the position and by position, you can get the get the value like: url + path.get(position).getPath() and then pass it to your fragment and apply same logic to load image as you used in getview using glide. – huk Jul 28 '17 at 19:26
  • Can you post the modified getView method please? – Ibernato933 Jul 28 '17 at 19:37

1 Answers1

0

in ImageAdapter, implment a custome callback like below:

private ImageViewClickListener mImageViewClickListener;

public interface ImageViewClickListener {
    void onImageClicked(int position);
}

public void setImageViewClickListener (ViewClickListener viewClickListener) {
    mImageViewClickListener = viewClickListener;
}

Now, in your activity, implement ImageViewClickListener interface and call imageAdapter.setImageViewClickListener(this) and implement below code in getview:

imageView.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
       mImageViewClickListener.onImageClicked(position);
    }
});
huk
  • 220
  • 3
  • 11