0

我有一个图标选择器,可让用户选择要在主屏幕上更改的图标。

我想划分我的网格视图,以便我可以将它们分类,即应用程序、游戏文件夹等。

我能够获得正确的视图并正确显示它们,但 onclick 侦听器不起作用。单击图标时,它不会带回图标。

另外,看来我可能工作过度了?我真的需要为 gridviews 设置 2 个适配器吗?在我的 logcat 上,我得到了大量的 dalvikvm GC_FOR_ALLOC freed ....... 此类启动时发生的行。

这是我的icons2类

package bigdx.clean.multi;

import java.util.ArrayList;

import bigdx.clean.multi.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class Icons2 extends Activity implements AdapterView.OnItemClickListener {

    public Uri CONTENT_URI;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int iconSize=getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
        setContentView(R.layout.icon_picker);

        ExpandableHeightGridView g=(ExpandableHeightGridView) findViewById(R.id.icon_grid_folders);
        g.setExpanded(true);
        g.setOnItemClickListener(this);
        g.setAdapter(new IconsAdapter(this, iconSize));
        g.setOnItemClickListener(this);

        ExpandableHeightGridView g2=(ExpandableHeightGridView) findViewById(R.id.icon_grid_apps);
        g2.setExpanded(true);
        g2.setOnItemClickListener(this);
        g2.setAdapter(new IconsAdapter2(this, iconSize));
        g2.setOnItemClickListener(this);

        this.CONTENT_URI = Uri.parse("content://" + IconsProvider.class.getCanonicalName());

    }
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

                String str = adapterView.getItemAtPosition(position).toString();
                setResult(-1, new Intent(null, Uri.withAppendedPath(this.CONTENT_URI, str)));
                finish();
              }

    class IconsAdapter extends BaseAdapter{
        private Context mContext;
        private int mIconSize;
        private ArrayList<Integer> mThumbs;

        public IconsAdapter(Context mContext, int iconsize) {
            this.mContext = mContext;
            this.mIconSize = iconsize;
                  loadIcons();
                }

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

        @Override
        public Object getItem(int position) {
            return mThumbs.get(position);
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(mIconSize, mIconSize));
            } else {
                imageView = (ImageView) convertView;
            }
            imageView.setImageResource(mThumbs.get(position));
            return imageView;
        }

        private void loadIcons() {
            mThumbs = new ArrayList<Integer>();

            final Resources resources = getResources();
            final String packageName = getApplication().getPackageName();

            addIcons(resources, packageName, R.array.icon_pack_folders);
        }
        private void addIcons(Resources resources, String packageName, int list) {
            final String[] extras = resources.getStringArray(list);
            for (String extra : extras) {
                int res = resources.getIdentifier(extra, "drawable", packageName);
                if (res != 0) {
                    final int thumbRes = resources.getIdentifier(extra,"drawable", packageName);
                    if (thumbRes != 0) {
                        mThumbs.add(thumbRes);
                    }
                }
            }
        }

    }
    class IconsAdapter2 extends BaseAdapter{
        private Context mContext;
        private int mIconSize;
        private ArrayList<Integer> mThumbs;

        public IconsAdapter2(Context mContext, int iconsize) {
            this.mContext = mContext;
            this.mIconSize = iconsize;
                  loadIcons();
                }

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

        @Override
        public Object getItem(int position) {
            return mThumbs.get(position);
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(mIconSize, mIconSize));
            } else {
                imageView = (ImageView) convertView;
            }
            imageView.setImageResource(mThumbs.get(position));
            return imageView;
        }

        private void loadIcons() {
            mThumbs = new ArrayList<Integer>();

            final Resources resources = getResources();
            final String packageName = getApplication().getPackageName();

            addIcons(resources, packageName, R.array.icon_pack_apps);
        }
        private void addIcons(Resources resources, String packageName, int list) {
            final String[] extras = resources.getStringArray(list);
            for (String extra : extras) {
                int res = resources.getIdentifier(extra, "drawable", packageName);
                if (res != 0) {
                    final int thumbRes = resources.getIdentifier(extra,"drawable", packageName);
                    if (thumbRes != 0) {
                        mThumbs.add(thumbRes);
                    }
                }
            }
        }

    }
}

这是我的图标选择器布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >
    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/back_dark" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:paddingTop="75dp" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_weight="0"
                android:background="@drawable/text_divider"
                android:paddingLeft="10dp"
                android:text="@string/icon_folders"
                android:textColor="@color/divider"
                android:textSize="24sp" />

            <bigdx.clean.multi.ExpandableHeightGridView
                android:id="@+id/icon_grid_folders"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textView1"
                android:gravity="center"
                android:horizontalSpacing="2dp"
                android:isScrollContainer="false"
                android:listSelector="@drawable/grid_selector"
                android:numColumns="4"
                android:stretchMode="columnWidth"
                android:verticalSpacing="20dp" />

            <TextView
                android:id="@+id/TextView01"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/icon_grid_folders"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_weight="0"
                android:background="@drawable/text_divider"
                android:paddingLeft="10dp"
                android:text="@string/icon_apps"
                android:textColor="@color/divider"
                android:textSize="24sp" />

            <bigdx.clean.multi.ExpandableHeightGridView
                android:id="@+id/icon_grid_apps"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/TextView01"
                android:gravity="center"
                android:horizontalSpacing="2dp"
                android:isScrollContainer="false"
                android:listSelector="@drawable/grid_selector"
                android:numColumns="4"
                android:stretchMode="columnWidth"
                android:verticalSpacing="20dp" />
        </RelativeLayout>
    </ScrollView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/semiblack"
            android:scaleType="center"
            android:src="@drawable/theme_header2" />

    </LinearLayout>

</RelativeLayout>

更新了 icons2 类

package bigdx.clean.multi;

import java.util.ArrayList;

import bigdx.clean.multi.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class Icons extends Activity {
    private static final String ACTION_ADW_PICK_ICON="org.adw.launcher.icons.ACTION_PICK_ICON";
    private static final String ACTION_ADW_PICK_RESOURCE="org.adw.launcher.icons.ACTION_PICK_ICON_RESOURCE";
    private boolean mPickerMode=false;
    private boolean mResourceMode=false;
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int iconSize=getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
        setContentView(R.layout.icon_picker);

        if(getIntent().getAction().equals(ACTION_ADW_PICK_ICON)){
            mPickerMode=true;
        }
        if(getIntent().hasExtra(ACTION_ADW_PICK_RESOURCE)){
            mResourceMode=true;
        }

        ExpandableHeightGridView g=(ExpandableHeightGridView) findViewById(R.id.icon_grid_folders);
        ExpandableHeightGridView g2=(ExpandableHeightGridView) findViewById(R.id.icon_grid_apps);
        g.setExpanded(true);
        g2.setExpanded(true);
        g.setAdapter(new IconsAdapter(this, iconSize, g.getId()));
        g2.setAdapter(new IconsAdapter(this, iconSize, g2.getId()));

        g.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                if(mPickerMode){
                    Intent intent=new Intent();
                    if(!mResourceMode){
                        Bitmap bitmap=null;
                        try{
                            bitmap=(Bitmap) adapterView.getAdapter().getItem(position);
                        }catch (Exception e) {
                        }
                        if(bitmap!=null){
                            intent.putExtra("icon",bitmap);
                            setResult(RESULT_OK, intent);
                        }else{
                            setResult(RESULT_CANCELED, intent);
                        }
                    }else{
                        ShortcutIconResource res=((IconsAdapter)adapterView.getAdapter()).getResource(position);
                        if(res!=null){
                            intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, res);
                            setResult(RESULT_OK, intent);
                        }else{
                            setResult(RESULT_CANCELED, intent);
                        }
                    }
                    finish();
                }
            }
        });

        g2.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                if(mPickerMode){
                    Intent intent=new Intent();
                    if(!mResourceMode){
                        Bitmap bitmap=null;
                        try{
                            bitmap=(Bitmap) adapterView.getAdapter().getItem(position);
                        }catch (Exception e) {
                        }
                        if(bitmap!=null){
                            intent.putExtra("icon",bitmap);
                            setResult(RESULT_OK, intent);
                        }else{
                            setResult(RESULT_CANCELED, intent);
                        }
                    }else{
                        ShortcutIconResource res=((IconsAdapter)adapterView.getAdapter()).getResource(position);
                        if(res!=null){
                            intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, res);
                            setResult(RESULT_OK, intent);
                        }else{
                            setResult(RESULT_CANCELED, intent);
                        }
                    }
                    finish();
                }
            }
        });

    }
    private class IconsAdapter extends BaseAdapter{
        private Context mContext;
        private int mIconSize;
        private int mG;

        public IconsAdapter(Context mContext, int iconsize, int g) {
            super();
            this.mContext = mContext;
            this.mIconSize = iconsize;
            this.mG = g;
            loadIcons();
        }

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

        public ShortcutIconResource getResource(int position){
            return ShortcutIconResource.fromContext(Icons.this,
                    mThumbs.get(position));
        }
        @Override
        public Object getItem(int position) {
            Options opts=new BitmapFactory.Options();
            opts.inPreferredConfig=Bitmap.Config.ARGB_8888;
            return BitmapFactory.decodeResource(mContext.getResources(), mThumbs.get(position), opts);
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(mIconSize, mIconSize));
            } else {
                imageView = (ImageView) convertView;
            }
            imageView.setImageResource(mThumbs.get(position));
            return imageView;
        }

        private ArrayList<Integer> mThumbs;
        ////////////////////////////////////////////////
        private void loadIcons() {
            mThumbs = new ArrayList<Integer>();

            final Resources resources = getResources();
            final String packageName = getApplication().getPackageName();

            switch (mG){
            case R.id.icon_grid_folders:
                addIcons(resources, packageName, R.array.icon_pack_folders);
            case R.id.icon_grid_apps:
                addIcons(resources, packageName, R.array.icon_pack_apps);
            }


        }
        private void addIcons(Resources resources, String packageName, int list) {
            final String[] extras = resources.getStringArray(list);
            for (String extra : extras) {
                int res = resources.getIdentifier(extra, "drawable", packageName);
                if (res != 0) {
                    final int thumbRes = resources.getIdentifier(extra,"drawable", packageName);
                    if (thumbRes != 0) {
                        mThumbs.add(thumbRes);
                    }
                }
            }
        }

    }

}

我有可点击的图标,但需要使用一个适配器设置每个 gridview

4

1 回答 1

0

让 IDE 自己创建函数。看起来你是自己写的,因为我看到你没有 @Override 注释。

尝试删除你的 onItemClick 函数,让 eclipse/intellij/whatever 为你做这件事。你应该看到它带有注释:

@Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }

如果这仍然不起作用(看不出为什么不),请匿名尝试:

g.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            // your logic here...
        }
    });

对于第二个问题,似乎系统在加载和设置图像资源时使用了大量内存。我建议您解码位图并调整它们的大小以适合屏幕。

祝你好运

于 2013-06-12T18:25:15.293 回答