如何正确配置GridView

时间:2015-04-25 18:50:39

标签: java android xml gridview adapter

我正在为使用GridView的android编写小应用程序。此网格显示来自SD卡的图像。将图像填充到网格我使用适配器。我的问题是,我滚动网格适配器从SD卡图像自动加载,我的应用程序在很短的时间内冻结。我如何能够快速重新配置我的GridView或适配器或其他组件以便我的应用程序正常工作。谢谢。enter image description here

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="2"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="3dp"
    android:layout_marginRight="3dp"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" />

更新

public class ImageAdapterFormGIF extends BaseAdapter {

    public static String file_path = "";

    public String[] p = getFileFromFolder();

    Tools t = new Tools();
    private Context mContext;

    public ImageAdapterFormGIF(Context c) {
        mContext = c;
    }

    Tools tools = new Tools();

    public int getCount() {
        return p.length;
    }


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

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter

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

        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams((tools.getWidth(mContext)-48)/2, (tools.getWidth(mContext)-48)/2));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
          //  imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageURI(Uri.parse(new File(p[position]).toString()));
        imageView.setTag(p[position]);

        return imageView;
    }


    private String[] getFileFromFolder() {
        Tools tools = new Tools();
        tools.checkSdcardFolders();
        String path = "/sdcard/xx/gif/";
        File f = new File(path);
        File file[] = f.listFiles();
        String[] s = new String[file.length];
        for (int i = 0; i < file.length; i++) {
            s[i] = "/sdcard/xx/gif/" + file[i].getName();
        }
        return s;
    }

}

0 个答案:

没有答案