2017-07-28 136 views
0


我有一個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控件。
如何將圖像從一個片段移動到另一個片段?

+0

檢查此[回答](https://stackoverflow.com/a/12739968/5343320)。 – huk

+0

當我點擊圖像時,如何獲取圖像視圖? – Ibernato933

+0

您不必獲取imageview。您可以通過getvView方法獲取點擊imageview的位置。在你的getview中實現onclicklistner到imageview。這會給你的位置和位置,你可以得到像這樣的值:url + path.get(position).getPath(),然後傳遞給你的片段,並應用相同的邏輯來加載圖像像你在getview中使用使用滑翔。 – huk

回答

0

在ImageAdapter,implment的是定製回調象下面這樣:現在

private ImageViewClickListener mImageViewClickListener; 

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

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

,在您的活動,實現ImageViewClickListener接口並調用imageAdapter.setImageViewClickListener(本)和getview執行下面的代碼:

imageView.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     mImageViewClickListener.onImageClicked(position); 
    } 
}); 
+0

好吧,但在活動中,我如何通過位置獲得圖像的路徑?當您將數組列表傳遞給適配器時,我只在imageAdapter – Ibernato933

+0

中有路徑,使其範圍變爲全局並使用位置訪問路徑。 – huk

+0

好的,謝謝你=) – Ibernato933