滚动时GridView项重叠

时间:2014-05-29 19:52:28

标签: android gridview universal-image-loader

我在GridView中重叠项目时遇到问题。它们由Universal Image Loader动态加载,问题是当它们中的一些不是正方形时。 向上滚动向下时没有问题: enter image description here

有一个间距。但是当向下滚动时:

enter image description here

如您所见,项目重叠在这里。

我该如何解决?这是我的片段:

import android.app.Fragment;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.util.List;


public class WhatsHotFragment extends Fragment {
    public static final String TAG = WhatsHotFragment.class.getSimpleName();

    private TopPodcastsAdapter adapter = null;
    private GridView podcastsGridView = null;
    private ITunesPodcastService iTunesPodcastService = new ITunesPodcastService();
    private DisplayImageOptions options;
    private LayoutInflater inflater = null;
    private ImageLoader imageLoader = ImageLoader.getInstance();


    public WhatsHotFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.inflater = inflater;
        View rootView = inflater.inflate(R.layout.whats_hot_fragment, container, false);
        podcastsGridView = (GridView) rootView.findViewById(R.id.podcastsGridView);

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_stub)
                .showImageForEmptyUri(R.drawable.ic_empty)
                .showImageOnFail(R.drawable.ic_error)
//                .imageScaleType(ImageScaleType.EXACTLY)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();

        getActivity().setTitle(R.string.whats_hot);

        updateTopPodcasts();

        return rootView;
    }


    private void updateTopPodcasts() {
        new DownloadTopPodcastsTask().execute();
    }


    public class DownloadTopPodcastsTask extends AsyncTask<String, Void, List<Podcast>> {

        @Override
        protected List<Podcast> doInBackground(String... strings) {
            List<String> podcastIds = iTunesPodcastService.getTopPodcastsIds();
            List<Podcast> podcasts = iTunesPodcastService.getPodcastsForIds(podcastIds);
            return podcasts;
        }

        @Override
        protected void onPostExecute(List<Podcast> podcasts) {
            adapter = new TopPodcastsAdapter(getActivity(), podcasts);
            podcastsGridView.setAdapter(adapter);
        }
    }

    static class ViewHolder {
        TextView podcastPrimaryGenreView;
        ImageView podcastImageView;
        TextView podcastTitleView;
        TextView podcastArtistView;
    }

    public class TopPodcastsAdapter extends BaseAdapter {
        private Context context;
        private List<Podcast> podcasts;

        public TopPodcastsAdapter(Context context, List<Podcast> podcasts) {
            this.context = context;
            this.podcasts = podcasts;
        }

        @Override
        public int getCount() {
            return podcasts.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.podcast_card_item, parent, false);
                holder = new ViewHolder();
                assert view != null;
                holder.podcastPrimaryGenreView = (TextView) view.findViewById(R.id.podcastCardPrimaryGenre);
                holder.podcastImageView = (ImageView) view.findViewById(R.id.podcastCardImage);
                holder.podcastTitleView = (TextView) view.findViewById(R.id.podcastCardTitle);
                holder.podcastTitleView.setSelected(true);
                holder.podcastArtistView = (TextView) view.findViewById(R.id.podcastCardArtist);
                holder.podcastArtistView.setSelected(true);
                view.setTag(holder);
            }
            else {
                holder = (ViewHolder) view.getTag();
            }
            holder.podcastPrimaryGenreView.setText(this.podcasts.get(position).getPrimaryGenreName().toUpperCase());
            holder.podcastTitleView.setText(this.podcasts.get(position).getTitle());
            holder.podcastArtistView.setText(this.podcasts.get(position).getArtistName());

            imageLoader.displayImage(this.podcasts.get(position).getArtworkUrl600(), holder.podcastImageView, 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) {
                        }
                    }
            );

            return view;
        }
    }
}

这是我的fragment.xml:

<?xml version="1.0" encoding="utf-8"?>

<GridView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/podcastsGridView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:numColumns="2"
    android:stretchMode="columnWidth"
    android:background="@color/grid_background"
    android:verticalSpacing="8dip"
    android:horizontalSpacing="8dip"
    android:padding="8dip"
    android:clipToPadding="false"/>

这是我的item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/podcastCard"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="@drawable/card_redborder"
    android:orientation="vertical">

    <TextView
        android:id="@+id/podcastCardPrimaryGenre"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        style="@style/CardTextSecondary"/>

    <ImageView
        android:id="@+id/podcastCardImage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerInside"
        android:adjustViewBounds="true"/>

    <TextView
        android:id="@+id/podcastCardTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        style="@style/CardTextPrimary"/>

    <TextView
        android:id="@+id/podcastCardArtist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        style="@style/CardTextSecondary"/>

</LinearLayout>

请帮忙。

0 个答案:

没有答案