Practical code Segment 2: practical code segment

Source: Internet
Author: User

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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.