2011-12-07 121 views
0

可能重复:
Is it possible to have a spinner and a list view on the same page?是否有可能在同一个视图上有微调和列表视图?

我想有在视图顶部的微调,然后根据什么用户从选择产生微调下方的列表视图微调,有没有人知道这个很好的教程或有一些代码可能会帮助?

我已经通过扩展ListActivity使用ListView成功了,但在这种情况下,我想让视图中的ListView以外的东西,所以我不知道该怎么办?

回答

1

首先,你可以为您的活动创造的布局,例如(layout_with_spinner):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:orientation="vertical" 
    android:id="@+id/layout1"> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <TextView 
      android:layout_width="40dip" 
      android:layout_height="wrap_content" 
      android:text="some text"/> 
     <Spinner 
      android:layout_width="150dip" 
      android:layout_height="fill_parent"         
      android:id="@+id/spinner1" />       

    </LinearLayout> 
    <ListView 
     android:id="@+id/list_view" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
    </ListView>   
</LinearLayout> 

,然后你可以创建类的活动

public class YourCoolActivity extends Activity 
{ 
    private Spinner mSpinner; 
    private ListView mList;    
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.layout_with_spinner); 
      mSpinner= (Spinner)findViewById(R.id.spinner1); 
      mList = (ListView)findViewById(R.id.list_view); 
      //here create some adapter. 
      mSpinner.setAdapter(yourAdapter); 
      //set listener on select 
      mSpinner.setOnItemSelectedListener(new OnItemSelectedListener()  
    { 
     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, 
       int pos, long id) 
     {       
         //here you can populate list with data 
         //create new list adapter depended on (YourObjectModel)mSpinner.getSelectedItem() 
         // or pos 
         mList.setAdapter(newListAdapter); 

     } 

     @Override 
     public void onNothingSelected(AdapterView<?> arg0) 
     {   
     }   
    }); 

    } 
} 

也许这有助于启动延长。你可以看看这个 ​​来看看一些关于纺纱厂的样品

+0

谢谢,这是完美的让我开始。 – wbarksdale

相关问题