Android使用Spinner实现下拉列表

实现效果

Android使用Spinner实现下拉列表

 

activity_main.xml

Android使用Spinner实现下拉列表

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

    

    <TextView 

        android:id="@+id/textView"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:textSize="25sp"

        android:textColor="#ff0000"

        android:text="文字"

        />

    

    <Spinner 

        android:id="@+id/spinner"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        />



</LinearLayout>

 

MainActivity.java

 

import java.util.ArrayList;

import java.util.List;



import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemSelectedListener;

import android.widget.ArrayAdapter;

import android.widget.Spinner;

import android.widget.TextView;



public class MainActivity extends ActionBarActivity implements OnItemSelectedListener{

    private Spinner spinner;

    private TextView textView;

    private List<String> list;

    private ArrayAdapter<String> adapter;

    

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        

        spinner = (Spinner) findViewById(R.id.spinner);

        textView = (TextView) findViewById(R.id.textView);

        textView.setText("您选择的城市为北京");

        

        //1.设置数据源

        list  = new ArrayList<String>();

        list.add("北京");

        list.add("上海");

        list.add("广州");

        list.add("深圳");

        

        //2.新建ArrayAdapter(数组适配器)

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);

        

        //3.adapter设置下拉列表样式

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        

        //4.spinner加载适配器和监听器

        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(this);

    }



    @Override

    public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {

        String cityName = adapter.getItem(position);

        //String cityName = list.get(position);

        textView.setText("您选择的城市为"+cityName);

    }



    @Override

    public void onNothingSelected(AdapterView<?> parent) {

        

    }



}

 

使用SimpleAdapter实现文图效果

Android使用Spinner实现下拉列表

使用自定义的下拉style

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent" >

    

    <ImageView 

        android:id="@+id/image"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginLeft="10dp"

        android:layout_alignParentLeft="true"

        android:layout_centerVertical="true"

        android:src="@drawable/ic_launcher"

        />

    

    <TextView 

        android:id="@+id/text"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerVertical="true"

        android:layout_marginLeft="20dp"

        android:layout_toRightOf="@id/image"

        android:text="None"

        />



</RelativeLayout>
import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;



import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemSelectedListener;

import android.widget.ArrayAdapter;

import android.widget.SimpleAdapter;

import android.widget.Spinner;

import android.widget.TextView;



public class MainActivity extends ActionBarActivity implements OnItemSelectedListener{

    private Spinner spinner;

    private TextView textView;

    private List<Map<String, Object>> datalist;

    private SimpleAdapter adapter;

    private int[] icon = {

            R.drawable.ic_launcher, 

            R.drawable.ic_launcher, 

            R.drawable.ic_launcher,

            R.drawable.ic_launcher};

    private String[] icon_name = {

            "北京",

            "广州", 

            "上海",

            "深圳"};

    

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        

        spinner = (Spinner) findViewById(R.id.spinner);

        textView = (TextView) findViewById(R.id.textView);

        textView.setText("您选择的城市为北京");

        

        //1.设置数据源

        datalist = new ArrayList<Map<String,Object>>();

        

        //2.新建SimpleAdapter(数组适配器)

        adapter = new SimpleAdapter(this, getData(), R.layout.item, new String[]{"image", "text"}, new int[]{R.id.image, R.id.text});

        

        //3.adapter设置下拉列表样式

        adapter.setDropDownViewResource(R.layout.item);

        

        //4.spinner加载适配器和监听器

        spinner.setAdapter(adapter);

        spinner.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("image",     icon[i]);

            map.put("text", icon_name[i]);

            datalist.add(map);

        }

        return datalist;

    }

 

你可能感兴趣的:(android)