1 package com.android.contacts.common.util;
2 
3 
4 import com.android.contacts.common.R;
5 
6 import android.content.res.Resources;
7 import android.view.View;
8 import android.widget.ListView;
9 
10 /**
11  * Utilities for configuring ListViews with a card background.
12  */
13 public class ContactListViewUtils {
14 
15     // These two constants will help add more padding for the text inside the card.
16     private static final double TEXT_LEFT_PADDING_TO_CARD_PADDING_RATIO = 1.1;
17 
addPaddingToView(ListView listView, int parentWidth, int listSpaceWeight, int listViewWeight)18     private static void addPaddingToView(ListView listView, int parentWidth,
19             int listSpaceWeight, int listViewWeight)
20     {
21         if (listSpaceWeight > 0 && listViewWeight > 0) {
22             double paddingPercent = (double) listSpaceWeight / (double)
23                     (listSpaceWeight * 2 + listViewWeight);
24             listView.setPadding(
25                     (int) (parentWidth * paddingPercent * TEXT_LEFT_PADDING_TO_CARD_PADDING_RATIO),
26                     listView.getPaddingTop(),
27                     (int) (parentWidth * paddingPercent * TEXT_LEFT_PADDING_TO_CARD_PADDING_RATIO),
28                     listView.getPaddingBottom());
29             // The EdgeEffect and ScrollBar need to span to the edge of the ListView's padding.
30             listView.setClipToPadding(false);
31             listView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
32         }
33     }
34 
35     /**
36      * Add padding to {@param listView} if this configuration has set both space weight and
37      * view weight on the layout. Use this util method instead of defining the padding in the
38      * layout file so that the {@param listView}'s padding can be set proportional to the card
39      * padding.
40      *
41      * @param resources
42      * @param listView ListView that we add padding to
43      * @param rootLayout layout that contains ListView and R.id.list_card
44      */
applyCardPaddingToView(Resources resources, final ListView listView, final View rootLayout)45     public static void applyCardPaddingToView(Resources resources,
46             final ListView listView, final View rootLayout) {
47         // Set a padding on the list view so it appears in the center of the card
48         // in the layout if required.
49         final int listSpaceWeight = resources.getInteger(
50                 R.integer.contact_list_space_layout_weight);
51         final int listViewWeight = resources.getInteger(
52                 R.integer.contact_list_card_layout_weight);
53         if (listSpaceWeight > 0 && listViewWeight > 0) {
54             rootLayout.setBackgroundResource(0);
55             // Set the card view visible
56             View mCardView = rootLayout.findViewById(R.id.list_card);
57             if (mCardView == null) {
58                 throw new RuntimeException(
59                         "Your content must have a list card view who can be turned visible " +
60                                 "whenever it is necessary.");
61             }
62             mCardView.setVisibility(View.VISIBLE);
63             // Add extra padding to the list view to make them appear in the center of the card.
64             // In order to avoid jumping, we skip drawing the next frame of the ListView.
65             SchedulingUtils.doOnPreDraw(listView, /* drawNextFrame = */ false, new Runnable() {
66                 @Override
67                 public void run() {
68                     // Use the rootLayout.getWidth() instead of listView.getWidth() since
69                     // we sometimes hide the listView until we finish loading data. This would
70                     // result in incorrect padding.
71                     ContactListViewUtils.addPaddingToView(
72                             listView, rootLayout.getWidth(),  listSpaceWeight, listViewWeight);
73                 }
74             });
75         }
76     }
77 }
78