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);
<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来模拟
/** * 获取设备上所有已安装并可以启动的应用的包名集合 * * @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会返回很多无法启动甚至没有图标的系统应用。