J'utilise la bibliothèque Cardslib. J'essayais d'ajouter une vignette pour chaque pacakge de son icône. L'icône est type Drawable. Donc je suppose, nous devons utiliser CustomSource pour créer un bitmap de celui-ci. Puis ajoutez-le à la carte.Android Cardslib même vignette apparaît pour tous les articles
Le problème avec le code ci-dessous est que tous les paquets obtiennent la même image de vignette. (L'image du dernier paquet apparaissant sur la liste). Est-ce dû à cardslib les charge en utilisant le AsyncTask
intégré et LRUCache
. Comment résoudre ce problème?
public void listpkg(Context c) {
ArrayList<Card> cards = new ArrayList<Card>();
Card card = new Card(this);
mContext = c;
CardHeader header = new CardHeader(c);
PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
header = new CardHeader(this);
header.setTitle(pm.getApplicationLabel(packageInfo).toString());
card = new Card(this);
card.addCardHeader(header);
card.setTitle("Package: " + packageInfo.packageName);
icon =getPackageManager().getApplicationIcon(packageInfo); //TODO use this icon
tagname = packageInfo.packageName;
// CustomSource --
thumb = new CardThumbnail(c);
thumb.setCustomSource(new CardThumbnail.CustomSource() {
@Override
public String getTag() {
return tagname;
}
@Override
public Bitmap getBitmap() {
PackageManager pm = mContext.getPackageManager();
Bitmap bitmap = null;
try {
bitmap = drawableToBitmap(pm.getApplicationIcon(getTag()));
} catch (PackageManager.NameNotFoundException e) {
}
return bitmap;
}
private Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
});
card.addCardThumbnail(thumb);
// CustomSource --
cards.add(card);
}
CardArrayRecyclerViewAdapter mCardArrayAdapter;
mCardArrayAdapter = new CardArrayRecyclerViewAdapter(this, cards);
//Staggered grid view
CardRecyclerView mRecyclerView = (CardRecyclerView) this.findViewById(R.id.mainListView);
mRecyclerView.setHasFixedSize(false);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//Set the empty view
if (mRecyclerView != null) {
mRecyclerView.setAdapter(mCardArrayAdapter);
}
}
xml
<it.gmariotti.cardslib.library.recyclerview.view.CardRecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:list_card_layout_resourceID="@layout/list_card_thumbnail_layout"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="12dp"
android:id="@+id/mainListView" />
est getTag() retour également de même pour tous les éléments? –
Je pense que j'ai 50 applications ou plus lors de l'impression 'tagname' après la ligne "tagname = packageInfo.packageName;" ce sont des noms différents. Mais bizarrement, avec-dans getTag(): print est apparu seulement deux fois avec le même nom (nom de l'application google-contacts). Est-ce dû au fait que setCustomSource() s'exécute sous Asynctask? et la variable est une variable globale? –
vérifiez la réponse. vous écrasez la variable à chaque itération. –