1 /* 2 * Copyright (C) 2008 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 android.widget.listview; 18 19 import android.app.Activity; 20 import android.os.Bundle; 21 import android.util.InternalSelectionView; 22 import android.view.View; 23 import android.view.ViewGroup; 24 import android.widget.BaseAdapter; 25 import android.widget.LinearLayout; 26 import android.widget.ListView; 27 import android.window.WindowMetricsHelper; 28 29 /** 30 * Most bodacious scenario yet! 31 */ 32 public class AdjacentListsWithAdjacentISVsInside extends Activity { 33 34 private ListView mLeftListView; 35 private ListView mRightListView; 36 getLeftListView()37 public ListView getLeftListView() { 38 return mLeftListView; 39 } 40 getRightListView()41 public ListView getRightListView() { 42 return mRightListView; 43 } 44 getLeftIsv()45 public InternalSelectionView getLeftIsv() { 46 return (InternalSelectionView) 47 ((ViewGroup) mLeftListView.getChildAt(0)).getChildAt(0); 48 } 49 getLeftMiddleIsv()50 public InternalSelectionView getLeftMiddleIsv() { 51 return (InternalSelectionView) 52 ((ViewGroup) mLeftListView.getChildAt(0)).getChildAt(1); 53 } 54 getRightMiddleIsv()55 public InternalSelectionView getRightMiddleIsv() { 56 return (InternalSelectionView) 57 ((ViewGroup) mRightListView.getChildAt(0)).getChildAt(0); 58 } 59 getRightIsv()60 public InternalSelectionView getRightIsv() { 61 return (InternalSelectionView) 62 ((ViewGroup) mRightListView.getChildAt(0)).getChildAt(1); 63 } 64 65 @Override onCreate(Bundle savedInstanceState)66 protected void onCreate(Bundle savedInstanceState) { 67 super.onCreate(savedInstanceState); 68 69 final int desiredHeight = (int) (0.8 * WindowMetricsHelper 70 .getBoundsExcludingNavigationBarAndCutout( 71 getWindowManager().getCurrentWindowMetrics()).height()); 72 73 mLeftListView = new ListView(this); 74 mLeftListView.setAdapter(new AdjacentISVAdapter(desiredHeight)); 75 mLeftListView.setItemsCanFocus(true); 76 77 78 mRightListView = new ListView(this); 79 mRightListView.setAdapter(new AdjacentISVAdapter(desiredHeight)); 80 mRightListView.setItemsCanFocus(true); 81 82 83 84 setContentView(combineAdjacent(mLeftListView, mRightListView)); 85 getWindow().getDecorView().restoreDefaultFocus(); 86 } 87 combineAdjacent(View... views)88 private static View combineAdjacent(View... views) { 89 if (views.length < 2) { 90 throw new IllegalArgumentException("you should pass at least 2 views in"); 91 } 92 93 final LinearLayout ll = new LinearLayout(views[0].getContext()); 94 ll.setOrientation(LinearLayout.HORIZONTAL); 95 final LinearLayout.LayoutParams lp = 96 new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f); 97 98 for (View view : views) { 99 ll.addView(view, lp); 100 } 101 return ll; 102 } 103 104 static class AdjacentISVAdapter extends BaseAdapter { 105 106 private final int mItemHeight; 107 AdjacentISVAdapter(int itemHeight)108 AdjacentISVAdapter(int itemHeight) { 109 mItemHeight = itemHeight; 110 } 111 getCount()112 public int getCount() { 113 return 1; 114 } 115 getItem(int position)116 public Object getItem(int position) { 117 return position; 118 } 119 getItemId(int position)120 public long getItemId(int position) { 121 return position; 122 } 123 getView(int position, View convertView, ViewGroup parent)124 public View getView(int position, View convertView, ViewGroup parent) { 125 final InternalSelectionView isvLeft = new InternalSelectionView( 126 parent.getContext(), 5, "isv left"); 127 isvLeft.setDesiredHeight(mItemHeight); 128 final InternalSelectionView isvRight = new InternalSelectionView( 129 parent.getContext(), 5, "isv right"); 130 isvRight.setDesiredHeight(mItemHeight); 131 return combineAdjacent(isvLeft, isvRight); 132 } 133 } 134 } 135