http://blog.csdn.net/a_fyf/article/details/51674790
懒得搬,直接搬连接。
运行显示下拉列表,点击可展开,点击子项目会将子项目显示到横线上,并且在TextView中显示选择的城市。
1.activity_main.xml页面添加Spinner控件。
activity_main.xml页面:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity" >
- <TextView
- android:id="@+id/tvShow"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="20sp" />
- <Spinner
- android:id="@+id/spDwon"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_below="@+id/tvShow"
- android:layout_marginTop="36dp" />
- </RelativeLayout>
2.MainActivity.java页面进行数据源的定义和适配器的配置。
MainActivity.java页面:
- package com.example.downshow;
- import java.util.ArrayList;
- import java.util.List;
- import android.os.Bundle;
- import android.app.Activity;
- import android.util.Log;
- import android.view.Menu;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Spinner;
- import android.widget.TextView;
- import android.widget.AdapterView.OnItemSelectedListener;
- public class MainActivity extends Activity implements OnItemSelectedListener{
- private TextView tvShow;
- private Spinner spDown;
- private List<String> list;
- private ArrayAdapter<String> adapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- tvShow=(TextView) findViewById(R.id.tvShow);
- spDown=(Spinner) findViewById(R.id.spDwon);
- tvShow.setText("您选择的城市是北京");
- /*设置数据源*/
- list=new ArrayList<String>();
- list.add("北京");
- list.add("上海");
- list.add("广州");
- list.add("深圳");
- /*新建适配器*/
- adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,list);
- /*adapter设置一个下拉列表样式,参数为系统子布局*/
- adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
- /*spDown加载适配器*/
- spDown.setAdapter(adapter);
- /*soDown的监听器*/
- spDown.setOnItemSelectedListener(this);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public void onItemSelected(AdapterView<?> parent, View view, int position,
- long id) {
- String cityName=adapter.getItem(position); //获取选中的那一项
- tvShow.setText("您选择的城市是"+cityName);
- }
- @Override
- public void onNothingSelected(AdapterView<?> arg0) {
- }
- }
3.运行就可以显示文字的下拉列表了,但是还可以自定义下拉列表样式。
图文列表
目标效果:
1.新建item.xml页面作为子布局。
item.xml页面:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <ImageView
- android:id="@+id/ivFruit"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/ic_launcher" />
- <TextView
- android:id="@+id/tvFruit"
- android:layout_gravity="center"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="TextView" />
- </LinearLayout>
2.新建一个SecondActivity.java页面和activity_second.xml页面,作为显示图文下拉列表的页面。
activity_second.xml页面:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".SecondActivity" >
- <TextView
- android:id="@+id/tvShow"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello_world" />
- <Spinner
- android:id="@+id/spDown"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_below="@+id/textView1"
- android:layout_marginTop="35dp" />
- </RelativeLayout>
3.SecondActivity.java页面设置适配器添加数据源。
SecondActivity.java页面:
- package com.example.downshow;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemSelectedListener;
- import android.widget.ImageView;
- import android.widget.SimpleAdapter;
- import android.widget.Spinner;
- import android.widget.TextView;
- public class SecondActivity extends Activity implements OnItemSelectedListener{
- private TextView tvShow;
- private Spinner spDown;
- private SimpleAdapter adapter;
- private List<Map<String, Object>> dataList;
- private int[] icon = { R.drawable.apple_pic, R.drawable.banana_pic,
- R.drawable.cherry_pic, R.drawable.grape_pic, R.drawable.mango_pic,
- R.drawable.orange_pic, R.drawable.pear_pic,
- R.drawable.pineapple_pic, R.drawable.strawberry_pic,
- R.drawable.watermelon_pic };
- private String[] iconName = { "苹果", "香蕉", "樱桃", "葡萄", "芒果", "橘子", "梨",
- "菠萝", "草莓", "西瓜" };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_second);
- tvShow = (TextView) findViewById(R.id.tvShow);
- spDown = (Spinner) findViewById(R.id.spDown);
- tvShow.setText("您选择的是苹果");
- /*创建数据源*/
- dataList = new ArrayList<Map<String, Object>>();
- /*创建简单适配器*/
- adapter = new SimpleAdapter(this, getData(), R.layout.item,
- new String[] { "ivFruit", "tvFruit" }, new int[] { R.id.ivFruit,
- R.id.tvFruit });
- /*adapter设置一个下拉列表样式,参数为自己定义的子布局*/
- adapter.setDropDownViewResource(R.layout.item);
- /*spDown加载适配器*/
- spDown.setAdapter(adapter);
- /*spDown设置监听事件*/
- spDown.setOnItemSelectedListener(this);
- }
- /*设置数据源*/
- private List<Map<String, Object>> getData() {
- for(int i=0;i<icon.length;i++){ //循环添加图片文字信息
- Map<String,Object> map=new HashMap<String, Object>();
- map.put("ivFruit", icon[i]);
- map.put("tvFruit",iconName[i]);
- dataList.add(map);
- }
- return dataList;
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.second, menu);
- return true;
- }
- @Override
- public void onItemSelected(AdapterView<?> parent, View view, int position,
- long id) {
- tvShow.setText("您选择的是"+adapter.getItem(position));//更改显示信息
- }
- @Override
- public void onNothingSelected(AdapterView<?> arg0) {
- // TODO Auto-generated method stub
- }
- }
4.运行就可以显示图文的目标效果了。