先上张效果图:
严格来说,这并不是一个下拉列表,只不过实现的效果很像是下拉列表
实现原理:
该下拉列表其实是一个AlertDialog.Builder,给它设置了一个以LinearLayout布局的View。该View内使用了一个GridView组件,
代码:
package yyy.testandroid9;
private Intent intent;
private int index = 0;
private SharedPreferences preferences;
private File file;
private Spinner spinner;
private GridView gridView;
private LinearLayout layout;
private Builder builder;
private AlertDialog dialog;
private MyAdapter adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutInflater inflater = LayoutInflater.from(this);
layout = (LinearLayout) inflater.inflate(R.layout.bank_grid, null);
gridView = (GridView) layout.findViewById(R.id.grid);
adapter = new MyAdapter(this);
gridView.setAdapter(adapter);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
textView = (TextView) findViewById(R.id.textview);
builder = new Builder(TestAndroid9Activity.this);
builder.setView(layout);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(dialog == null){
dialog = builder.show();
}
dialog.show();
}
});
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
button1.setText(getResources().getStringArray(R.array.city)[position]);
dialog.dismiss();
adapter.map.put(index, false);
adapter.map.put(position, true);
index = position;
adapter.notifyDataSetChanged();
}
});
}
private class MyAdapter extends BaseAdapter{
private Context context;
private String[] citys;
private LayoutInflater inflater;
public HashMap<Integer, Boolean> map;
public MyAdapter(Context context) {
super();
this.context = context;
citys = context.getResources().getStringArray(R.array.city);
inflater = LayoutInflater.from(context);
map = new HashMap<Integer, Boolean>();
for(int i=0;i<citys.length;i++){
map.put(i, false);
}
}
public int getCount() {
// TODO Auto-generated method stub
return citys.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
if(view == null){
view = inflater.inflate(R.layout.bank_item, null);
}
TextView textView = (TextView) view.findViewById(R.id.list_text);
RadioButton radioButton = (RadioButton) view.findViewById(R.id.radiobutton);
radioButton.setChecked(map.get(position));
textView.setText(citys[position]);
return view;
}
}
bank_grid.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="20dp"
android:background="#ffffff"
android:id="@+id/grid_layout">
<GridView
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:verticalSpacing="20px"
android:horizontalSpacing="10px"
android:numColumns="4"
android:scrollbars="vertical"
android:layout_margin="10dp"/>
</LinearLayout>
bank_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
>
<RadioButton
android:id="@+id/radiobutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
/>
<TextView
android:id="@+id/list_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_gravity="center_vertical"/>
</LinearLayout>
分享到:
相关推荐
本文实例讲述了Android编程实现多列显示的下拉列表框Spinner功能。分享给大家供大家参考,具体如下: 先上张效果图: 严格来说,这并不是一个下拉列表,只不过实现的效果很像是下拉列表 实现原理: 该下拉列表其实...
android studio,demo,Spinner下拉菜单多选的实现,android studio3.0.1的环境下使用。
android自定义Spinner下拉菜单(下拉列表框)样式
Android-react-native-loading-spinner-overlay.zip,反应本机加载微调器覆盖,安卓系统是谷歌在2008年设计和制造的。操作系统主要写在爪哇,C和C 的核心组件。它是在linux内核之上构建的,具有安全性优势。
该源代码展示了如何去实现下拉列表框的效果,控件Spinner
Android 省市区三级连动--spinner.rar
Android 省市区三级连动--spinner.zip
使用Spinner实现下拉列表,包括自定义下拉样式。 关键代码地址:http://www.cnblogs.com/huangzx/p/4437096.html
智能家居系统 下拉列表spinner-教案.doc 学习资料 复习资料 教学资源
Android省市区三级连动--spinner.zip
前端开源库-chalk-cli-spinner粉笔cli旋转器,彩色cli旋转器
Android程序研发源码Android 省市区三级连动--spinner.rar
Android高级应用源码-Spinner多级联动.rar
Android 省市区三级连动--spinner.zip项目安卓应用源码下载Android 省市区三级连动--spinner.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
前端项目-angular-image-spinner,用于在带有图像的容器中显示微调器的角度指令。
NiceSpinner is a re-implementation of the default Android's spinner, with a nice arrow animation and a different way to display its content. It follows the material design guidelines, and it is ...