Home | History | Annotate | Download | only in listview
      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.util.InternalSelectionView;
     20 
     21 import android.app.Activity;
     22 import android.os.Bundle;
     23 import android.view.View;
     24 import android.view.ViewGroup;
     25 import android.widget.BaseAdapter;
     26 import android.widget.LinearLayout;
     27 import android.widget.ListView;
     28 
     29 /**
     30  * Most bodacious scenario yet!
     31  */
     32 public class AdjacentListsWithAdjacentISVsInside extends Activity {
     33 
     34     private ListView mLeftListView;
     35     private ListView mRightListView;
     36 
     37     public ListView getLeftListView() {
     38         return mLeftListView;
     39     }
     40 
     41     public ListView getRightListView() {
     42         return mRightListView;
     43     }
     44 
     45     public InternalSelectionView getLeftIsv() {
     46         return (InternalSelectionView)
     47                 ((ViewGroup) mLeftListView.getChildAt(0)).getChildAt(0);
     48     }
     49 
     50     public InternalSelectionView getLeftMiddleIsv() {
     51         return (InternalSelectionView)
     52                 ((ViewGroup) mLeftListView.getChildAt(0)).getChildAt(1);
     53     }
     54 
     55     public InternalSelectionView getRightMiddleIsv() {
     56         return (InternalSelectionView)
     57                 ((ViewGroup) mRightListView.getChildAt(0)).getChildAt(0);
     58     }
     59 
     60     public InternalSelectionView getRightIsv() {
     61         return (InternalSelectionView)
     62                 ((ViewGroup) mRightListView.getChildAt(0)).getChildAt(1);
     63     }
     64 
     65     @Override
     66     protected void onCreate(Bundle savedInstanceState) {
     67         super.onCreate(savedInstanceState);
     68 
     69         final int desiredHeight = (int) (0.8 * getWindowManager().getDefaultDisplay().getHeight());
     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));
     83     }
     84 
     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 
    105         AdjacentISVAdapter(int itemHeight) {
    106             mItemHeight = itemHeight;
    107         }
    108 
    109         public int getCount() {
    110             return 1;
    111         }
    112 
    113         public Object getItem(int position) {
    114             return position;
    115         }
    116 
    117         public long getItemId(int position) {
    118             return position;
    119         }
    120 
    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