2013-06-12 91 views
0

我有一個圖標選擇器,可讓用戶選擇要在主屏幕上更改的圖標。需要爲單個類中的多個gridviews設置多個onclicklisteners

我想瓜分我的GridView的,所以我可以把它們進行分類,即應用程序,遊戲文件夾等

我能夠得到正確的觀點,正確地顯示出來,但onClick的監聽器不能正常工作。當圖標被點擊時,它不會帶回圖標。

此外,它似乎我可能過度工作呢?我真的必須爲gridviews設置2個適配器嗎?在我的logcat上,我得到了一大堆dalvikvm GC_FOR_ALLOC釋放了......當這個類啓動時發生的線條。

這是我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); 
        } 
       } 
      } 
     } 

    } 
} 

這是我iconpicker佈局

<?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有一個適配器

回答

0

讓IDE自己創建函數。 看起來像你自己寫的,因爲我看到你沒有@Override註釋。

嘗試刪除你的onItemClick函數,並讓eclipse/intellij /無論它爲你做什麼。 你應該註釋看到:

@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... 
     } 
    }); 

對於第二個問題,看來這個系統使用當您加載並設置圖像資源時,會產生大量內存。我建議你解碼位圖並調整它們以適應屏幕。

祝你好運

+0

我更新了我原來的帖子與另一個onItemClick方法,我應該使用。我想這就是你說的爲了節省內存?使用這個函數使用重寫符號,我得到[ShortcutIconResource res =((IconsAdapter)adapterView.getAdapter())。getResource(position);]的錯誤;]圖標說明IconsAdapter2不能轉換爲IconsAdapter。這是我的問題。因爲我有2個gridviews我創建了2個適配器。如何在onClickListener上實現它們? – Bignadad

+0

這是另一個問題:)你只需要爲g和g2創建一個BaseAdapter。併爲它們使用.setAdapter(new IconAdapter(..)) –

+0

問題是private void loadIcons()方法從每個gridview的不同數組中加載圖標。我怎麼能實現一個loadicons()方法爲每個gridview在同一個適配器? – Bignadad

相關問題