如何在滚动后停止Android gridview显示相同的项目

时间:2016-04-23 20:55:39

标签: android gridview

enter image description here我会直言不讳。每当我滚动时,我的Gridview有时会显示相同的项目两次或三次。

如果您愿意,可以跳过此段落的背景故事。我最近从viewpager切换到gridview以减少我的应用程序的大小,我认为它看起来更好。我尽管每次滚动时都会偶尔显示两张相同的图像。我决定从textview和两个imageViews转换为textview。 这是为电视制作并显示横幅,徽标或图标(取决于开发人员包括的内容)。我一直在尝试很多不同的事情,并且知道我的代码现在很乱,但请耐心等待。在我忘记包装是否有横幅或徽标之前,我将其用作textView backGround并将textView文本留空。如果它只有一个图标我将文本设置为标题并使用textview setCompoundDrawables方法。

我的适配器

public class GundamAdapter extends ArrayAdapter<AppInfo> {
Context mContext;
ArrayList<AppInfo> list;
final int maxMemory = (int) (Runtime.getRuntime().maxMemory()/ 1024);
final int cacheSize = maxMemory / 8;
private LruCache<String, Drawable> mMemoryCache;

int mResource;
LayoutInflater inflater;
ViewHolder holder;


public GundamAdapter(Context context, int resource, ArrayList<AppInfo> objects) {
    super(context, resource, objects);
    mContext = context;
    list = objects;
    mResource = resource;
    mMemoryCache = new LruCache<String, Drawable>(cacheSize) {

    };
    inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final AppInfo info = list.get(position);
    final String pName = info.getPackageName();
    View v = convertView;
    if (v == null) {
        holder = new ViewHolder();
        v = inflater.inflate(mResource, parent, false);
        holder.name = (CustomTextView) v.findViewById(R.id.title);
        v.setTag(holder);
    } else {
        holder = (ViewHolder) v.getTag();
    }
    if (info.getHasBanner()) {
        new GridViewBitmapAsyncTask(holder.name, info).execute(pName);
    } else {
        holder.name.setText(info.getTitle());
        holder.name.setBackgroundColor(Color.parseColor("#009688"));

        info.getIcon().setBounds(0, 0, 100, 100);
        holder.name.setCompoundDrawables(info.getIcon(), null, null, null);
    }


    return v;
}

public void addBitmapToMemoryCache(String key, Drawable drawable) {
    if (getBitmapFromMemCache(key) == null) {
        mMemoryCache.put(key, drawable);
    }
}

public Drawable getBitmapFromMemCache(String key) {
    Log.d("get bitmap", "got it");
    return mMemoryCache.get(key);
}


public class GridViewBitmapAsyncTask extends AsyncTask<String, Void, Drawable> {
    CustomTextView tempImage;
    AppInfo appInfo;

    public GridViewBitmapAsyncTask(CustomTextView imageView, AppInfo appyInfo) {
        tempImage = imageView;
        appInfo = appyInfo;
    }

    @Override
    protected Drawable doInBackground(String... params) {
        final String packageName = params[0];
        Drawable appIcon = getBitmapFromMemCache(packageName);
        if (appIcon == null) {
            appIcon = appInfo.getIcon();
            appIcon.setBounds(0, 0, 80, 80);
            Bitmap bitmap = ((BitmapDrawable) appIcon).getBitmap();
            addBitmapToMemoryCache(packageName, appIcon);
            Log.d("hello", "added to bitmapcache");

        }


        return appIcon;
    }

    @Override
    protected void onPostExecute(Drawable drawable) {
        tempImage.setBackground(drawable);


    }
}

static class ViewHolder {
    CustomTextView name;
}

}

mainActivity

  private List<ResolveInfo> getResolvedApps(Context context,  PackageManager manager) {

    Intent leanIntent = new Intent(Intent.ACTION_MAIN);
    leanIntent.addCategory(Intent.CATEGORY_LEANBACK_LAUNCHER);

    Intent regularIntent = new Intent(Intent.ACTION_MAIN);
    regularIntent.addCategory(Intent.CATEGORY_LAUNCHER);

    List<ResolveInfo> rapps = manager.queryIntentActivities(leanIntent, PackageManager.GET_META_DATA);
    List<ResolveInfo> reggies = manager.queryIntentActivities(regularIntent, PackageManager.GET_META_DATA);

    ArrayList<ResolveInfo> allApps = new ArrayList<>();

    for (ResolveInfo rInfo : rapps) {
        final String lbPackageName = rInfo.activityInfo.packageName;
        allApps.add(rInfo);
        for (ResolveInfo regApps : reggies) {
            final String regPackageName = regApps.activityInfo.packageName;
            if ((lbPackageName.equals(regPackageName) == false) && (allApps.contains(regApps) == false) && (manager.getLeanbackLaunchIntentForPackage(regPackageName) == null)) {
                allApps.add(regApps);
            }
        }
        Collections.sort(allApps, new ResolveInfo.DisplayNameComparator(manager));
    }
    return allApps;
}

[![enter image description here][1]][1] public class LoadTheList extends AsyncTask<String, Void, Boolean>{

    @Override
    protected Boolean doInBackground(String... params) {
        PackageManager manager = getPackageManager();
        List<ResolveInfo> rApps = getResolvedApps(MainActivity.this, manager);

        if (rApps != null) {

            mApplications.clear();
            for (ResolveInfo packageInfo : rApps) {
                AppInfo application = new AppInfo();
                application.setActivityInfo(packageInfo.activityInfo);
                application.setTitle(packageInfo.loadLabel(manager));
                application.setPackageName(packageInfo.activityInfo.packageName);
                if (application.getIcon() == null && packageInfo.activityInfo.getBannerResource() != 0) {
                    application.setHasBanner(true);
                    application.setIcon(packageInfo.activityInfo.loadBanner(manager));
                }

                if (application.getIcon() == null && packageInfo.activityInfo.getLogoResource() != 0) {
                    application.setHasBanner(true);
                    application.setIcon(packageInfo.activityInfo.loadLogo(manager));
                }

                if (application.getIcon() == null) {
                    application.setHasBanner(false);
                    application.setIcon(packageInfo.activityInfo.loadIcon(manager));
                }

                mApplications.add(application);
                if (application.getTitle().equals("Google Play services")) {
                    mApplications.remove(application);
                }

            }
        }


        return true;
    }

    @Override
    protected void onPostExecute(Boolean aBoolean) {
        adapter.notifyDataSetChanged();
     //   super.onPostExecute(aBoolean);
    }
}

0 个答案:

没有答案