正如标题所说,我需要在片段中创建自定义应用启动器。我已经将我的代码基于我在这里的几个答案,无论如何,我不能在其上显示任何应用程序。
这是适配器类:
public class ShortcutAdapter extends BaseAdapter {
private Context mContext;
private List<ResolveInfo> appsList;
PackageManager mPackageManager;
public ShortcutAdapter(List<ResolveInfo> appsList, Context mContext) {
this.appsList = appsList;
this.mContext = mContext;
mPackageManager = mContext.getPackageManager();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
ResolveInfo resolveInfo = appsList.get(position);
imageView.setImageDrawable(resolveInfo.loadIcon(mPackageManager));
return imageView;
}
}
这是片段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.settings_layout, container, false);
GridView mGrid = (GridView) view.findViewById(R.id.gridview);
//Load list on adapter
mGrid.setAdapter(new ShortcutAdapter(loadApps(), getContext()));
//Listener gridview items
mGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ResolveInfo checkedResolveInfo = (ResolveInfo) parent.getItemAtPosition(position);
ActivityInfo clickedActivityInfo = checkedResolveInfo.activityInfo;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClassName(
clickedActivityInfo.applicationInfo.packageName,
clickedActivityInfo.name);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
startActivity(intent);
}
});
return view;
}
/*Load apps*/
private List<ResolveInfo> loadApps() {
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> appsList = getActivity().getPackageManager().queryIntentActivities(mainIntent, 0);
return appsList;
}
您可以在几个关于互联网的教程中找到相同的代码,但在我的情况下,我无法在GridView中显示任何应用程序。
EDIT- xml gridview布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="120dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center_horizontal"
android:layout_marginTop="60dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
</LinearLayout>
答案 0 :(得分:1)
以下任何一项都不会显示任何应用图标。
ShortcutAdapter#getCount()
返回0;
@覆盖 public int getCount(){ 返回0; }
(GridView) view.findViewById(R.id.gridview);
GridView的宽度很窄。您可以添加背景并进行检查。
Fragment
添加ViewGroup
,其宽度不正确。您可以添加背景并进行检查。