Practical code Segment 2: practical code segment
1. Simple ViewHolder writing for ListView
/*** Search for controls based on the specified ID in the specified View (built-in ViewHolder cache) ** @ param convertView * container * @ param id * Control ID * @ return found control */@ 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 ;}Usage:
@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. Set Activity transparency
<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>
Note: AppBaseTheme is generally the android: theme specified by your application. What is theme here? Otherwise, the Internal Spatial styles of the Activity may be different.
Purpose: it is used to simulate the Dialog effect. For example, if the Dialog cannot be used in the Service, the Activity can be used to simulate the Dialog effect.
3,
/*** Get the package name set of all installed and started applications on the device ** @ param context * context object * @ return the set of Package Names meeting the conditions */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 ;}Using getInstalledPackages will return many system applications that cannot be started or even have no icons.