Gridview不滚动:Android

时间:2015-05-14 07:05:01

标签: java android gridview non-scrolling

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:id="@+id/TextViewSelectAlbum"

        android:layout_marginTop="16dp"
        android:textSize="16dp"
        android:textColor="@color/link_text_material_dark"

        android:text="Select Album"

        />

    <Spinner android:id="@+id/SpinnerSelectAlbum"

        android:layout_width="wrap_content"
        android:background="@color/link_text_material_dark"
        android:layout_marginTop="12dp"

        android:popupBackground="@color/material_blue_grey_900"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:enabled="false"

        />

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/TextViewAlbumDescription"
        android:layout_marginTop="16dp"
        android:textSize="14dp"
        android:textColor="@color/link_text_material_dark"

        />

    <GridView
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:columnWidth="80dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="4dp"
        android:horizontalSpacing="4dp"
        android:stretchMode="columnWidth"
        android:gravity="center"
        android:id="@+id/grid">
   </GridView>
</LinearLayout>

这是我用来显示从facebook下载的图像网格的布局,但gridview只显示有限的内容,我无法滚动网格视图。 如果我错过了什么,请帮助我。

下面是getView方法

public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        imageView = new ImageView(context);
        imageView.setLayoutParams(new GridView.LayoutParams(75, 75));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    } else {
        imageView = (ImageView) convertView;
    }

    String pictureUrl = photoItems.get(position).getPictureUrl();
    new DownloadTask(imageView).execute(pictureUrl);

    return imageView;
}

private class DownloadTask extends AsyncTask<String,String,Bitmap> {

    private final WeakReference<ImageView> imageViewReference;

    public DownloadTask(ImageView imageView) {
        imageViewReference = new WeakReference<ImageView>(imageView);
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        try {
            String imageUrl = params[0];
            Log.i("test","imageUrl" + imageUrl);
            Bitmap bitmap = ImageCache.get(imageUrl);
            if (bitmap != null)
                return bitmap;
            bitmap = HttpFetch.fetchBitmap(imageUrl);
            Log.i("test", "bitmap " + bitmap);
            ImageCache.set(imageUrl, bitmap);

            return bitmap;
        }catch (Exception e) {
            return null;
        }
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);

        if(imageViewReference != null) {
            ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                imageView.setImageBitmap(bitmap);
            }
        }

    }
}

0 个答案:

没有答案