1、ListView使用ViewHolder极简写法
/** * 在指定View中依据指定ID寻找控件(内置ViewHolder缓存) * * @param convertView * 容器 * @param id * 控件ID * @return 找到的控件 */ @SuppressWarnings("unchecked") public static <T extends View> T getAdapterView(View convertView, int id) { SparseArray<View> viewHolder = (SparseArray<View>) convertView.getTag(); if (viewHolder == null) { viewHolder = new SparseArray<View>(); convertView.setTag(viewHolder); } View childView = viewHolder.get(id); if (childView == null) { childView = convertView.findViewById(id); viewHolder.put(id, childView); } return (T) childView; }使用方法:
@Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_feed_item, parent, false); } ImageView thumnailView = getAdapterView(convertView, R.id.video_thumbnail); ImageView avatarView = getAdapterView(convertView, R.id.user_avatar); ImageView appIconView = getAdapterView(convertView, R.id.app_icon);
2、设置Activity透明
<style name="TransparentActivity" parent="AppBaseTheme"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:colorBackgroundCacheHint">@null</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowNoTitle">true</item> <item name="android:windowContentOverlay">@null</item> </style>
说明:AppBaseTheme通常是你application指定的android:theme是啥这里就是啥。否则Activity内部的空间风格可能不一致。
用途:用于模拟Dialog效果。比方再Service中没法用Dialog,就能够用Activity来模拟
3、
/** * 获取设备上全部已安装并能够启动的应用的包名集合 * * @param context * 上下文对象 * @return 满足条件的包名的集合 */ public static List<String> getAllCanOpenApp(Context context) { Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); List<ResolveInfo> activities = context.getPackageManager() .queryIntentActivities(intent, 0); if (DataUtil.isEmpty(activities)) { return null; } List<String> list = new ArrayList<String>(); String packName; for (ResolveInfo info : activities) { packName=info.activityInfo.packageName; if(DataUtil.isEmpty(packName)){ continue; } list.add(packName); } return list; }使用getInstalledPackages会返回非常多无法启动甚至没有图标的系统应用。