我搜索了Google,但還沒有找到與此相關的任何內容。如何在RecyclerView中顯示已安裝的應用程序?
我正在使用Android Studio創建我的第一個Material App。現在我想在RecyclerView中顯示我手機上安裝的所有應用程序。這可能嗎?
我搜索了Google,但還沒有找到與此相關的任何內容。如何在RecyclerView中顯示已安裝的應用程序?
我正在使用Android Studio創建我的第一個Material App。現在我想在RecyclerView中顯示我手機上安裝的所有應用程序。這可能嗎?
模型存包信息:
class PInfo {
private String appname = "";
private String pname = "";
private String versionName = "";
private int versionCode = 0;
private Drawable icon;
public getAppName(){
return appname;
}
}
CustomAdapter設置應用程序名稱的值,你也可以通過添加額外的代碼顯示圖標等。通過調用getInstalledApps將返回你PInfo類對象,通過它你可以獲得包信息。它需要一個參數,如果你想獲得系統的其他應用程序發送虛假(從這個site代碼參考)
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.Viewhold> {
ArrayList<PInfo> appn;
public CustomAdapter() {
appn=p.getInstalledApps(true);
}
@Override
public int getItemCount() {
return appn.size();
}
@Override
public void onBindViewHolder(Viewhold holder, int pos) {
for(int i=0;i<appn.size();i++{
holder.appname.setText(appn.get(i).pname);
}
}
@Override
public ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.yourlayout, viewGroup, false);
return new ContactViewHolder(itemView);
}
private ArrayList<PInfo> getPackages() {
ArrayList<PInfo> apps = getInstalledApps(false); /* false = no system packages */
final int max = apps.size();
for (int i=0; i<max; i++) {
apps.get(i).prettyPrint();
}
return apps;
}
private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {
ArrayList<PInfo> res = new ArrayList<PInfo>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
for(int i=0;i<packs.size();i++) {
PackageInfo p = packs.get(i);
if ((!getSysPackages) && (p.versionName == null)) {
continue ;
}
PInfo newInfo = new PInfo();
newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
res.add(newInfo);
}
return res;
}
public static class ContactViewHolder extends RecyclerView.ViewHolder{
TextView appname;
public ViewHolder(View v) {
super(v);
appname = (TextView) v.findViewById(R.id.appname);
}
}
}
yourlayout.xml
<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="12sp" />
傳遞true
只需在您的主要活動中調用以下適配器:
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new CustomAdapter();
mRecyclerView.setAdapter(mAdapter);
親臨現場瞭解Recyclerview http://www.vogella.com/tutorials/AndroidRecyclerView/article.html
感謝您的詳細信息。非常感謝您的幫助!快速的問題,我應該有一個mainactivity來調用適配器,並且還有一個單獨的PInfo類和CustomAdapter類的活動? – user1353517 2015-03-02 17:52:21
是更好的將它們放在單獨的文件中......但是它可能會將它們放在同一個文件中 – Psypher 2015-03-02 18:15:49
儘管這很有幫助,但是,你的代碼是一團糟...... – TheWanderer 2017-08-06 19:09:25
使用'PackageManager',以獲取有關安裝的應用程序的信息,並以此作爲你的'RecyclerView'模型數據。 – CommonsWare 2015-03-02 16:28:51