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