Home | History | Annotate | Download | only in listview
      1 /*
      2  * Copyright (C) 2007 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.ListScenario;
     20 import android.view.KeyEvent;
     21 import android.view.View;
     22 import android.os.Bundle;
     23 import android.widget.LinearLayout;
     24 import android.widget.Button;
     25 
     26 /**
     27  * List of 1,000 items used to test calls to setSelection() in touch mode.
     28  * Pressing the S key will call setSelection(0) on the list.
     29  */
     30 public class ListSetSelection extends ListScenario {
     31     private Button mButton;
     32 
     33     @Override
     34     protected void init(Params params) {
     35         params.setStackFromBottom(false)
     36                 .setStartingSelectionPosition(-1)
     37                 .setNumItems(1000)
     38                 .setItemScreenSizeFactor(0.22);
     39     }
     40 
     41     @Override
     42     protected void onCreate(Bundle icicle) {
     43         super.onCreate(icicle);
     44 
     45         mButton = new Button(this);
     46         mButton.setText("setSelection(0)");
     47         mButton.setOnClickListener(new View.OnClickListener() {
     48             public void onClick(View v) {
     49                 getListView().setSelection(0);
     50             }
     51         });
     52 
     53         getListViewContainer().addView(mButton, new LinearLayout.LayoutParams(
     54                 LinearLayout.LayoutParams.MATCH_PARENT,
     55                 LinearLayout.LayoutParams.WRAP_CONTENT
     56         ));
     57     }
     58 
     59     public Button getButton() {
     60         return mButton;
     61     }
     62 
     63     @Override
     64     public boolean dispatchKeyEvent(KeyEvent event) {
     65         if (event.getKeyCode() == KeyEvent.KEYCODE_S) {
     66             getListView().setSelection(0);
     67             return true;
     68         }
     69 
     70         return super.dispatchKeyEvent(event);
     71     }
     72 }
     73