The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Neal Nguyen | 1d3165f | 2010-01-12 13:26:10 -0800 | [diff] [blame] | 17 | package android.widget.listview; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | import android.app.Activity; |
| 20 | import android.os.Bundle; |
Evan Rosky | 1d65c76 | 2017-11-09 15:13:31 -0800 | [diff] [blame] | 21 | import android.util.InternalSelectionView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 36 | public ListView getLeftListView() { |
| 37 | return mLeftListView; |
| 38 | } |
| 39 | |
| 40 | public ListView getRightListView() { |
| 41 | return mRightListView; |
| 42 | } |
| 43 | |
| 44 | public InternalSelectionView getLeftIsv() { |
| 45 | return (InternalSelectionView) |
| 46 | ((ViewGroup) mLeftListView.getChildAt(0)).getChildAt(0); |
| 47 | } |
| 48 | |
| 49 | public InternalSelectionView getLeftMiddleIsv() { |
| 50 | return (InternalSelectionView) |
| 51 | ((ViewGroup) mLeftListView.getChildAt(0)).getChildAt(1); |
| 52 | } |
| 53 | |
| 54 | public InternalSelectionView getRightMiddleIsv() { |
| 55 | return (InternalSelectionView) |
| 56 | ((ViewGroup) mRightListView.getChildAt(0)).getChildAt(0); |
| 57 | } |
| 58 | |
| 59 | public InternalSelectionView getRightIsv() { |
| 60 | return (InternalSelectionView) |
| 61 | ((ViewGroup) mRightListView.getChildAt(0)).getChildAt(1); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | protected void onCreate(Bundle savedInstanceState) { |
| 66 | super.onCreate(savedInstanceState); |
| 67 | |
Andrii Kulian | e57f2dc | 2020-01-26 20:59:07 -0800 | [diff] [blame] | 68 | final int desiredHeight = |
| 69 | (int) (0.8 * getWindowManager().getCurrentWindowMetrics().getSize().getHeight()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | |
| 71 | mLeftListView = new ListView(this); |
| 72 | mLeftListView.setAdapter(new AdjacentISVAdapter(desiredHeight)); |
| 73 | mLeftListView.setItemsCanFocus(true); |
| 74 | |
| 75 | |
| 76 | mRightListView = new ListView(this); |
| 77 | mRightListView.setAdapter(new AdjacentISVAdapter(desiredHeight)); |
| 78 | mRightListView.setItemsCanFocus(true); |
| 79 | |
| 80 | |
| 81 | |
| 82 | setContentView(combineAdjacent(mLeftListView, mRightListView)); |
Evan Rosky | 1d65c76 | 2017-11-09 15:13:31 -0800 | [diff] [blame] | 83 | getWindow().getDecorView().restoreDefaultFocus(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | private static View combineAdjacent(View... views) { |
| 87 | if (views.length < 2) { |
| 88 | throw new IllegalArgumentException("you should pass at least 2 views in"); |
| 89 | } |
| 90 | |
| 91 | final LinearLayout ll = new LinearLayout(views[0].getContext()); |
| 92 | ll.setOrientation(LinearLayout.HORIZONTAL); |
| 93 | final LinearLayout.LayoutParams lp = |
| 94 | new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f); |
| 95 | |
| 96 | for (View view : views) { |
| 97 | ll.addView(view, lp); |
| 98 | } |
| 99 | return ll; |
| 100 | } |
| 101 | |
| 102 | static class AdjacentISVAdapter extends BaseAdapter { |
| 103 | |
| 104 | private final int mItemHeight; |
| 105 | |
| 106 | AdjacentISVAdapter(int itemHeight) { |
| 107 | mItemHeight = itemHeight; |
| 108 | } |
| 109 | |
| 110 | public int getCount() { |
| 111 | return 1; |
| 112 | } |
| 113 | |
| 114 | public Object getItem(int position) { |
| 115 | return position; |
| 116 | } |
| 117 | |
| 118 | public long getItemId(int position) { |
| 119 | return position; |
| 120 | } |
| 121 | |
| 122 | public View getView(int position, View convertView, ViewGroup parent) { |
| 123 | final InternalSelectionView isvLeft = new InternalSelectionView( |
| 124 | parent.getContext(), 5, "isv left"); |
| 125 | isvLeft.setDesiredHeight(mItemHeight); |
| 126 | final InternalSelectionView isvRight = new InternalSelectionView( |
| 127 | parent.getContext(), 5, "isv right"); |
| 128 | isvRight.setDesiredHeight(mItemHeight); |
| 129 | return combineAdjacent(isvLeft, isvRight); |
| 130 | } |
| 131 | } |
| 132 | } |