Android:有关==下拉菜单导航==的学习
因为先前的学习都没想着记录自己的学习历程,所以该博客才那么迟才开始写。
内容: ==下拉菜单导航==
学习网站:
Spinner提供了从一个数据集合中快速选择一项值的办法。默认情况下Spinner显示的是当前选择的值,点击Spinner会弹出一个包含所有可选值的==dropdown菜单==,从该菜单中可以为Spinner选择一个新值。
阶段一:最简单的Spinner
1、在布局文件中添加Spinner控件
//AppBarLayout和Toolbar是对布局页面顶部的条进行操作 //该部分才是下拉菜单的本体 //从这行可以看到资源都是从@array来的,因此需要一个这样的存放资源的xml文件
- Beijing
- Guangzhou
- Shanghai
效果:
但是这个终究只是效果,若要对选择进行回应就需要更加复杂的步骤来完善。一般情况下我们是需要响应Spinner选择事件的,可以通过==OnItemSelectedListener==的回调方法实现
MainActivity:
package com.example.dropdownlayout;import android.os.Bundle;import android.support.design.widget.FloatingActionButton;import android.support.design.widget.Snackbar;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.view.View;import android.view.Menu;import android.view.MenuItem;import android.widget.AdapterView;import android.widget.Spinner;import android.widget.SpinnerAdapter;import android.widget.Toast;public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{ private Spinner spinner ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initUI(); } private void initUI(){ spinner = (Spinner)findViewById(R.id.spinner); spinner.setOnItemSelectedListener(this); } @Override public void onItemSelected(AdapterView adapterView, View view, int i, long l) { String[] places = getResources().getStringArray(R.array.places); Toast.makeText(MainActivity.this,"你点击的是:"+places[i],Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView adapterView) { }}
效果:
阶段二:学会设置Spinner的BaseAdapter
通过BaseAdapter可以自定义自己的菜单形式,但菜单的形式需要自己来设计
spinnerview:
main.xml中的spinner要把enteries这一项去掉。
Person.java
package com.example.dropdownlayout;/** * Created by Funny_One on 2017/8/18. */public class Person { public String personName,personAddress; public Person(String personName,String personAddress){ this.personAddress = personAddress; this.personName = personName; } public String getPersonAddress() { return personAddress; } public String getPersonName() { return personName; }}
Myadapter.java
package com.example.dropdownlayout;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;import java.util.List;/** * Created by Funny_One on 2017/8/18. */public class Myadapter extends BaseAdapter { public ListmList; public Context context; public View mView; public Myadapter(Context context,List mList){ this.mList = mList; this.context = context; } @Override public int getCount() { return mList.size(); } @Override public Object getItem(int i) { return mList.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { LayoutInflater layoutInflater = LayoutInflater.from(context); mView = layoutInflater.inflate(R.layout.spinnerview,null); TextView personName = (TextView)mView.findViewById(R.id.personName); TextView personAddress=(TextView)mView.findViewById(R.id.personAddress); personAddress.setText(mList.get(i).getPersonAddress()); personName.setText(mList.get(i).getPersonName()); return mView; }}
MainActivity:
package com.example.dropdownlayout;import android.app.Activity;import android.os.Bundle;import android.support.design.widget.FloatingActionButton;import android.support.design.widget.Snackbar;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.view.View;import android.view.Menu;import android.view.MenuItem;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Spinner;import android.widget.SpinnerAdapter;import android.widget.Toast;import java.util.ArrayList;import java.util.List;public class MainActivity extends Activity { private Spinner spinner ; private Listpersons; private Myadapter myadapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); spinner = (Spinner)findViewById(R.id.spinner); persons = new ArrayList (); persons.add(new Person(" 袁逸灏 "," 广东 ")); persons.add(new Person(" 春旺 "," 云南 ")); persons.add(new Person(" 刘先润 "," 重庆 ")); persons.add(new Person(" 刘诚昊 "," 湖南 ")); myadapter = new Myadapter(this,persons); spinner.setAdapter(myadapter); }}
效果: