Android中使用Spinner实现下拉列表功能

所属分类: 软件编程 / Android 阅读数: 897
收藏 0 赞 0 分享

Spinner

  Spinner是一个列表选择框,会在用户选择后,展示一个列表供用户进行选择。Spinner是ViewGroup的间接子类,它和其他的Android控件一样,数据需要使用Adapter进行封装。

1,Demo展示图片

这里写图片描述

2,布局代码

//(layout)activity_main
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context="com.test.spinner.MainActivity">
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal">
  <TextView
   android:textSize="20sp"
   android:layout_margin="10dp"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="今年的常规赛MVP:"/>
  <TextView
   android:id="@+id/text"
   android:textSize="20sp"
   android:layout_marginTop="10dp"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"/>
 </LinearLayout>
 <Spinner
  android:visibility="gone"
  android:id="@+id/spinner"
  android:layout_marginLeft="10dp"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
 </Spinner>
</LinearLayout>
-------------------------------------------------------------------
//(layout)item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical">
 <ImageView
  android:id="@+id/imageView"
  android:layout_width="80dp"
  android:layout_height="80dp"/>
 <TextView
  android:id="@+id/textView"
  android:layout_marginLeft="20dp"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
</LinearLayout>

3,Activity代码

//MainActivity
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.test.spinner.R.id.simpleAdapter;
public class MainActivity extends AppCompatActivity {
 private Context mContext = MainActivity.this;
 private Spinner mSpinner;
 private ArrayAdapter<String> mArrayAdapter;
 private TextView mTextView;
 private SimpleAdapter mSimpleAdapter;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  initView();
  initData();
 }
 private void initView(){
  mSpinner = (Spinner) findViewById(R.id.spinner);
  mTextView = (TextView) findViewById(R.id.text);
 }
 private void initData(){
  // 设置数据集
  List<String> list = new ArrayList<>();
  list.add("哈登");
  list.add("莱昂纳德");
  list.add("詹姆斯");
  list.add("威斯布鲁克");
  List<Map<String ,Object>> maps = new ArrayList<>();
  int[] icon = {R.mipmap.pic1 , R.mipmap.pic2, R.mipmap.pic3, R.mipmap.pic4};
  String[] iconName = {"哈登" , "莱昂纳德" , "詹姆斯" , "威斯布鲁克"};
  // 设置适配器
  mArrayAdapter = new ArrayAdapter<>(mContext ,
    android.R.layout.simple_spinner_item , list);
  mArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  mSimpleAdapter = new SimpleAdapter(mContext,
    maps , R.layout.item , new String[]{"image" , "text"} , new int[]{R.id.imageView ,R.id.textView});
  for(int i = 0 ; i < icon.length ; i++){
   Map<String ,Object> map = new HashMap<>();
   map.put("image", icon[i]);
   map.put("text" , iconName[i]);
   maps.add(map);
  }
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.main , menu);
  return true;
 }
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()){
   case R.id.arrayAdapter:
    mSpinner.setVisibility(View.VISIBLE);
    mSpinner.setAdapter(mArrayAdapter);
    // 设置监听器
    mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
     @Override
     public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
      mTextView.setText(mArrayAdapter.getItem(position));
     }
     @Override
     public void onNothingSelected(AdapterView<?> adapterView) {
     }
    });
    break;
   case simpleAdapter:
    mSpinner.setVisibility(View.VISIBLE);
    mSpinner.setAdapter(mSimpleAdapter);
    // 设置监听器
    mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
     @Override
     public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
      mTextView.setText(mSimpleAdapter.getItem(position)+"");
     }
     @Override
     public void onNothingSelected(AdapterView<?> adapterView) {
     }
    });
    break;
   default:
    break;
  }
  return super.onOptionsItemSelected(item);
 }
}

以上所述是小编给大家介绍的Android中使用Spinner实现下拉列表功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

Android 自定义球型水波纹带圆弧进度效果(实例代码)

最近小编接到一个这样的需求,需要实现一个圆形水波纹,带进度,两层水波纹需要渐变显示,且外围有一个圆弧进度。今天小编给大家分享实例代码,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

Flutter 实现下拉刷新上拉加载的示例代码

这篇文章主要介绍了Flutter 实现下拉刷新上拉加载的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Windows实现Flutter环境搭建及配置这一篇就够了

这篇文章主要介绍了Windows实现Flutter环境搭建及配置这一篇就够了,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android利用碎片fragment实现底部标题栏(Github模板开源)

Fragment可以作为Activity的组成部分,一个Activity可以有多个Fragment,这篇文章主要介绍了Android利用碎片fragment实现底部标题栏(Github模板开源),需要的朋友可以参考下
收藏 0 赞 0 分享

android studio 的下拉菜单Spinner使用详解

这篇文章主要介绍了android studio 的下拉菜单Spinner使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

解析Android 8.1平台SystemUI 导航栏加载流程

这篇文章主要介绍了Android 8.1平台SystemUI 导航栏加载流程,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信录音功能

这篇文章主要为大家详细介绍了Android仿微信录音功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿微信键盘切换效果

这篇文章主要为大家详细介绍了Android仿微信键盘切换效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android超清晰6.0权限申请AndPermission

这篇文章主要介绍了Android超清晰6.0权限申请AndPermission,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿微信录制语音功能

这篇文章主要介绍了Android仿微信录制语音功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多