1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.contacts.common.util; 18 19 import android.content.res.Resources; 20 import android.view.View; 21 import android.widget.ListView; 22 import com.android.contacts.common.R; 23 import com.android.dialer.util.ViewUtil; 24 25 /** Utilities for configuring ListViews with a card background. */ 26 public class ContactListViewUtils { 27 28 // These two constants will help add more padding for the text inside the card. 29 private static final double TEXT_LEFT_PADDING_TO_CARD_PADDING_RATIO = 1.1; 30 31 private static void addPaddingToView( 32 ListView listView, int parentWidth, int listSpaceWeight, int listViewWeight) { 33 if (listSpaceWeight > 0 && listViewWeight > 0) { 34 double paddingPercent = 35 (double) listSpaceWeight / (double) (listSpaceWeight * 2 + listViewWeight); 36 listView.setPadding( 37 (int) (parentWidth * paddingPercent * TEXT_LEFT_PADDING_TO_CARD_PADDING_RATIO), 38 listView.getPaddingTop(), 39 (int) (parentWidth * paddingPercent * TEXT_LEFT_PADDING_TO_CARD_PADDING_RATIO), 40 listView.getPaddingBottom()); 41 // The EdgeEffect and ScrollBar need to span to the edge of the ListView's padding. 42 listView.setClipToPadding(false); 43 listView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY); 44 } 45 } 46 47 /** 48 * Add padding to {@param listView} if this configuration has set both space weight and view 49 * weight on the layout. Use this util method instead of defining the padding in the layout file 50 * so that the {@param listView}'s padding can be set proportional to the card padding. 51 * 52 * @param listView ListView that we add padding to 53 * @param rootLayout layout that contains ListView and R.id.list_card 54 */ 55 public static void applyCardPaddingToView( 56 Resources resources, final ListView listView, final View rootLayout) { 57 // Set a padding on the list view so it appears in the center of the card 58 // in the layout if required. 59 final int listSpaceWeight = resources.getInteger(R.integer.contact_list_space_layout_weight); 60 final int listViewWeight = resources.getInteger(R.integer.contact_list_card_layout_weight); 61 if (listSpaceWeight > 0 && listViewWeight > 0) { 62 rootLayout.setBackgroundResource(0); 63 // Set the card view visible 64 View mCardView = rootLayout.findViewById(R.id.list_card); 65 if (mCardView == null) { 66 throw new RuntimeException( 67 "Your content must have a list card view who can be turned visible " 68 + "whenever it is necessary."); 69 } 70 mCardView.setVisibility(View.VISIBLE); 71 72 // Add extra padding to the list view to make them appear in the center of the card. 73 // In order to avoid jumping, we skip drawing the next frame of the ListView. 74 ViewUtil.doOnPreDraw( 75 listView, 76 false, 77 new Runnable() { 78 @Override 79 public void run() { 80 // Use the rootLayout.getWidth() instead of listView.getWidth() since 81 // we sometimes hide the listView until we finish loading data. This would 82 // result in incorrect padding. 83 ContactListViewUtils.addPaddingToView( 84 listView, rootLayout.getWidth(), listSpaceWeight, listViewWeight); 85 } 86 }); 87 } 88 } 89 } 90