我已经在我的应用程序中集成了flickr,但是gridview中的图像在行之间显示了很多空间。我无法找出为什么行之间会出现这么多的空间。

PhotosFragment.java

     public class PhotosFragment extends Fragment {
            private FragmentActivity myContext;
            String FlickrPhotoPath, FlickrPhotoPath2;
            Bitmap bmFlickr, bmFlickr2;
            String FlickrQuery_url = "https://api.flickr.com/services/rest/?method=flickr.photos.search";
            String FlickrQuery_per_page = "&per_page=2";

            String FlickrQuery_nojsoncallback = "&nojsoncallback=1";
            String FlickrQuery_format = "&format=json";
            String FlickrQuer

y_tag = "&tags=";
        String FlickrQuery_key = "&api_key=";
        ArrayList<String> imageUrls = new ArrayList<String>();

        String FlickrApiKey = "xyz";

        @Override
        public void onAttach(Activity activity) {

            if (activity instanceof FragmentActivity) {
                myContext = (FragmentActivity) activity;
            }
            super.onAttach(activity);
        }
        DisplayImageOptions options;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.photos, container, false);
            ImageView img2 = (ImageView) rootView.findViewById(R.id.imageView3);
            ImageView img3 = (ImageView) rootView.findViewById(R.id.imageView1);
            rootView.setVerticalScrollBarEnabled(false);
            options = new DisplayImageOptions.Builder()
                    .showImageOnLoading(R.drawable.icon)
                    .showImageForEmptyUri(R.drawable.icon)
                    .showImageOnFail(R.drawable.icon)
                    .cacheInMemory(true)
                    .cacheOnDisk(true)
                    .considerExifParams(true)
                    .bitmapConfig(Bitmap.Config.RGB_565)
                    .build();

            String searchResult = QueryFlickr("Java");

            String jsonResult = ParseJSON(searchResult);

            String searchResult2 = QueryFlickr("Android");
            String jsonResult2 = ParseJSON2(searchResult2);
            if (bmFlickr != null) {
                img2.setImageBitmap(bmFlickr);

            }
            if (bmFlickr2 != null) {

                img3.setImageBitmap(bmFlickr2);
            }

            GridView grid = (GridView) rootView.findViewById(R.id.gridView);
            ((GridView) grid).setAdapter(new ImageAdapter());

            grid.setFocusable(false);





            GridView grid2 = (GridView) rootView.findViewById(R.id.gridView2);
            grid2.setFocusable(false);
            ((GridView) grid2).setAdapter(new ImageAdapter());


            return rootView;
        }

        public class ImageAdapter extends BaseAdapter {

            private LayoutInflater inflater;

            ImageAdapter() {
                inflater = LayoutInflater.from(getActivity());
            }

            @Override
            public int getCount() {
                return imageUrls.size();
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                final ViewHolder holder;
                View view = convertView;
                if (view == null) {
                    view = inflater.inflate(R.layout.grid_single, parent, false);
                    holder = new ViewHolder();
                    assert view != null;
                    holder.imageView = (ImageView) view.findViewById(R.id.grid_image);
                   // holder.progressBar = (ProgressBar) view.findViewById(R.id.progress);
                    view.setTag(holder);
                } else {
                    holder = (ViewHolder) view.getTag();
                }

                ImageLoader.getInstance()
                        .displayImage(imageUrls.get(position).toString(), holder.imageView, options, new SimpleImageLoadingListener() {
                            @Override
                            public void onLoadingStarted(String imageUri, View view) {

                            }

                            @Override
                            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

                            }

                            @Override
                            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

                            }
                        }, new ImageLoadingProgressListener() {
                            @Override
                            public void onProgressUpdate(String imageUri, View view, int current, int total) {
                          //      holder.progressBar.setProgress(Math.round(100.0f * current / total));
                            }
                        });

                return view;
            }
        }
        static class ViewHolder {
            ImageView imageView;
            ProgressBar progressBar;
        }

        private String QueryFlickr(String q) {

            String qResult = null;

            String qString =
                    FlickrQuery_url
                            + FlickrQuery_per_page
                            + FlickrQuery_nojsoncallback
                            + FlickrQuery_format
                            + FlickrQuery_tag + q
                            + FlickrQuery_key + FlickrApiKey;

            HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(qString);


            try {
                HttpEntity httpEntity = httpClient.execute(httpGet).getEntity();

                if (httpEntity != null) {
                    InputStream inputStream = httpEntity.getContent();
                    Reader in = new InputStreamReader(inputStream);
                    BufferedReader bufferedreader = new BufferedReader(in);
                    StringBuilder stringBuilder = new StringBuilder();

                    String stringReadLine = null;

                    while ((stringReadLine = bufferedreader.readLine()) != null) {
                        stringBuilder.append(stringReadLine + "\n");
                    }

                    qResult = stringBuilder.toString();

                }

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return qResult;
        }

        private String ParseJSON(String json) {

            String jResult = null;
            //  String jResult2 = null;
            bmFlickr = null;

            String flickrId;
            String flickrOwner;
            String flickrSecret;
            String flickrServer;
            String flickrFarm;
            String flickrTitle;

            try {
                JSONObject JsonObject = new JSONObject(json);
                JSONObject Json_photos = JsonObject.getJSONObject("photos");
                JSONArray JsonArray_photo = Json_photos.getJSONArray("photo");

                //We have only one photo in this exercise
                JSONObject FlickrPhoto = JsonArray_photo.getJSONObject(0);

                flickrId = FlickrPhoto.getString("id");
                flickrOwner = FlickrPhoto.getString("owner");
                flickrSecret = FlickrPhoto.getString("secret");
                flickrServer = FlickrPhoto.getString("server");
                flickrFarm = FlickrPhoto.getString("farm");
                flickrTitle = FlickrPhoto.getString("title");

                jResult = "\nid: " + flickrId + "\n"
                        + "owner: " + flickrOwner + "\n"
                        + "secret: " + flickrSecret + "\n"
                        + "server: " + flickrServer + "\n"
                        + "farm: " + flickrFarm + "\n"
                        + "title: " + flickrTitle + "\n";
        /*      jResult2 = "\nid: " + flickrId + "\n"
                        + "owner: " + flickrOwner + "\n"
                        + "secret: " + flickrSecret + "\n"
                        + "server: " + flickrServer + "\n"
                        + "farm: " + flickrFarm + "\n"
                        + "title: " + flickrTitle + "\n"; */

                bmFlickr = LoadPhotoFromFlickr(flickrId, flickrOwner, flickrSecret,
                        flickrServer, flickrFarm, flickrTitle);


            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return jResult;
        }

        private String ParseJSON2(String json) {

            String jResult = null;
            //  String jResult2 = null;
            bmFlickr2 = null;

            String flickrId;
            String flickrOwner;
            String flickrSecret;
            String flickrServer;
            String flickrFarm;
            String flickrTitle;

            try {
                JSONObject JsonObject = new JSONObject(json);
                JSONObject Json_photos = JsonObject.getJSONObject("photos");
                JSONArray JsonArray_photo = Json_photos.getJSONArray("photo");

                //We have only one photo in this exercise
                JSONObject FlickrPhoto = JsonArray_photo.getJSONObject(0);

                flickrId = FlickrPhoto.getString("id");
                flickrOwner = FlickrPhoto.getString("owner");
                flickrSecret = FlickrPhoto.getString("secret");
                flickrServer = FlickrPhoto.getString("server");
                flickrFarm = FlickrPhoto.getString("farm");
                flickrTitle = FlickrPhoto.getString("title");

                jResult = "\nid: " + flickrId + "\n"
                        + "owner: " + flickrOwner + "\n"
                        + "secret: " + flickrSecret + "\n"
                        + "server: " + flickrServer + "\n"
                        + "farm: " + flickrFarm + "\n"
                        + "title: " + flickrTitle + "\n";
        /*      jResult2 = "\nid: " + flickrId + "\n"
                        + "owner: " + flickrOwner + "\n"
                        + "secret: " + flickrSecret + "\n"
                        + "server: " + flickrServer + "\n"
                        + "farm: " + flickrFarm + "\n"
                        + "title: " + flickrTitle + "\n"; */

                bmFlickr2 = LoadPhotoFromFlickr2(flickrId, flickrOwner, flickrSecret,
                        flickrServer, flickrFarm, flickrTitle);


            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return jResult;
        }

        private Bitmap LoadPhotoFromFlickr(
                String id, String owner, String secret,
                String server, String farm, String title) {
            Bitmap bm = null;

            FlickrPhotoPath =
                    "http://farm" + farm + ".static.flickr.com/"
                            + server + "/" + id + "_" + secret + "_m.jpg";
            imageUrls.add(FlickrPhotoPath);
            imageUrls.add(FlickrPhotoPath);
            imageUrls.add(FlickrPhotoPath);
            imageUrls.add(FlickrPhotoPath);
            imageUrls.add(FlickrPhotoPath);
            imageUrls.add(FlickrPhotoPath);
            imageUrls.add(FlickrPhotoPath);
            imageUrls.add(FlickrPhotoPath);

            URL FlickrPhotoUrl = null;

            try {
                FlickrPhotoUrl = new URL(FlickrPhotoPath);

                HttpURLConnection httpConnection
                        = (HttpURLConnection) FlickrPhotoUrl.openConnection();
                httpConnection.setDoInput(true);
                httpConnection.connect();
                InputStream inputStream = httpConnection.getInputStream();
                bm = BitmapFactory.decodeStream(inputStream);

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return bm;
        }

        private Bitmap LoadPhotoFromFlickr2(
                String id, String owner, String secret,
                String server, String farm, String title) {
            Bitmap bm = null;

            FlickrPhotoPath2 =
                    "http://farm" + farm + ".static.flickr.com/"
                            + server + "/" + id + "_" + secret + "_m.jpg";
                    imageUrls.add(FlickrPhotoPath2);
            imageUrls.add(FlickrPhotoPath2);
            imageUrls.add(FlickrPhotoPath2);
            imageUrls.add(FlickrPhotoPath2);
            imageUrls.add(FlickrPhotoPath2);
            imageUrls.add(FlickrPhotoPath2);
            imageUrls.add(FlickrPhotoPath2);
            imageUrls.add(FlickrPhotoPath2);



            URL FlickrPhotoUrl2 = null;

            try {
                FlickrPhotoUrl2 = new URL(FlickrPhotoPath2);

                HttpURLConnection httpConnection
                        = (HttpURLConnection) FlickrPhotoUrl2.openConnection();
                httpConnection.setDoInput(true);
                httpConnection.connect();
                InputStream inputStream = httpConnection.getInputStream();
                bm = BitmapFactory.decodeStream(inputStream);

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return bm;
        }

    }


photos.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF">

    <RelativeLayout
        android:id="@+id/layout4"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"

        >

        <RelativeLayout
            android:id="@+id/layout1"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"

            >

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:adjustViewBounds="true"
                android:cropToPadding="false"
                android:scaleType="fitXY"


                 />


        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/layout2"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/layout1"

            >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="180dp"
                android:layout_height="210dp"
                android:layout_alignParentLeft="true"
                android:adjustViewBounds="true"
                android:cropToPadding="false"
                android:paddingTop="1dp"
                android:scaleType="fitXY"


          />

            <GridView
                android:id="@+id/gridView"
                android:layout_width="180dp"
                android:layout_height="210dp"

                android:layout_alignParentRight="true"
                android:layout_toRightOf="@+id/imageView1"

                android:adjustViewBounds="true"
                android:cropToPadding="false"
                android:numColumns="2"


                android:scaleType="fitXY"

                ></GridView>
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/layout3"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/layout2"
            android:padding="15dp">

            <com.pepup.league.ui.fonts.AvenirNextLtRegular
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="JULY-2014"
                android:textColor="#b2b2b2"

                android:textSize="15sp" />

            <View
                android:id="@+id/line1"
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:layout_centerVertical="true"
                android:layout_marginRight="5dp"

                android:layout_toLeftOf="@+id/textView"
                android:background="#b2b2b2" />

            <View
                android:id="@+id/line2"
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:layout_centerVertical="true"
                android:layout_marginLeft="5dp"

                android:layout_toRightOf="@+id/textView"
                android:background="#b2b2b2" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/layout5"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/layout3">

            <GridView
                android:id="@+id/gridView2"
                android:layout_width="fill_parent"
                android:layout_height="200dp"

                android:adjustViewBounds="true"
                android:cropToPadding="false"
                android:horizontalSpacing="0dp"
                android:numColumns="5"
                android:scaleType="fitXY"
                android:stretchMode="columnWidth"
                android:verticalSpacing="0dp"></GridView>
        </RelativeLayout>
    </RelativeLayout>
</ScrollView>


grid_single.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp">

    <ImageView
        android:id="@+id/grid_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"></ImageView>

</LinearLayout>

最佳答案

这是因为您在android:padding="5dp"的grid_single.xm中使用了LinearLayout。如果要删除空格,则需要像这样使用xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/grid_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"></ImageView>

</LinearLayout>

10-05 21:52