版权声明:本文为博主原创文章,未经博主允许不得转载。
前言:这段时间真是忙啊忙啊忙,元旦三天假加了两天班,已经连续六周只放一天了,天天加班到十点多,真是有一口血吐在屏幕上的感觉了,博客也积了好多,今天继续。无论人生有多艰难,梦想这个东西还是要坚持的。
相关文章:
1、《详解Dialog(一)——基础元素构建》
2、《详解Dialog(二)——有关列表的构建》
3、《详解Dialog(三)——自定义对话框视图及参数传递》
上篇给大家讲了Dialog的基本元素的构建方法,今天给大家说说有关列表对话框的构建。本篇讲的所有列表项都是通过系统自带的函数生成的,对于完全自定义对话框的方法,我们会在最后一篇讲。
列表对话框主要有四种:普通列表、单选列表、双选列表、自定义视图的列表,下面我们一个个来分析。
一、普通列表
普通列表的样式是这样的:
要实现这样的列表样式,是通过下面的方法来实现的:
- builder.setItems(CharSequence[] items, DialogInterface.OnClickListener listener)
- builder.setItems(int itemsId, DialogInterface.OnClickListener listener)
- setItems(CharSequence[] items, DialogInterface.OnClickListener listener)
- setItems(int itemsId, DialogInterface.OnClickListener listener)
下面,我们一个个看看他们的具体用法:
1、字符串数组
下面是上图是实现上图效果的代码:- String[] mItems = {"item0", "item1", "itme2", "item3", "itme4", "item5", "item6"};
- AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
- builder.setIcon(R.drawable.ic_launcher);
- builder.setTitle("使用列表字符串");
- builder.setItems(mItems, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(MyActivity.this, "clicked:" + which, Toast.LENGTH_LONG).show();
- }
- });
- builder.create();
- builder.show();
2、字符串资源ID
首先在xml文件夹下建一个字符串资源XML,命名为:array.xml,位置如图:然后定义一段字符串数组:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string-array name="dialog_items">
- <item>王菲</item>
- <item>王力宏</item>
- <item>帅哥</item>
- <item>美女</item>
- </string-array>
- </resources>
- AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
- builder.setIcon(R.drawable.ic_launcher);
- builder.setTitle("使用Resource ID");
- builder.setItems(R.array.dialog_items, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(MyActivity.this, "clicked:" + which, Toast.LENGTH_LONG).show();
- }
- });
- builder.create();
- builder.show();
二、单选列表
单选列表的样式是这样的:要实现单选列表,主要是通过下面四个其中之一来实现的:
方法一:
- setSingleChoiceItems(CharSequence[] items, int checkedItem, DialogInterface.OnClickListener listener)
方法二:
- setSingleChoiceItems (int itemsId, int checkedItem, DialogInterface.OnClickListener listener)
方法三:
- setSingleChoiceItems (ListAdapter adapter, int checkedItem, DialogInterface.OnClickListener listener)
方法四:
- setSingleChoiceItems (Cursor cursor, int checkedItem, String labelColumn, DialogInterface.OnClickListener listener)
好了,关于方法三,因为下面会接着讲SetAdapter的知识,所以这部分我留在最后再讲,这里先讲下方法一和方法二:
方法一和方法二,与上例没什么区别,下面我就以方法一以例来讲讲上面的效果图是怎么实现的吧:
代码如下:
- String[] mItems = {"攻", "受", "全能型", "不告诉你"};
- AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
- builder.setIcon(R.drawable.ic_launcher);
- builder.setTitle("你懂的");
- builder.setSingleChoiceItems(mItems, 0, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- Toast.makeText(MyActivity.this, "clicked:" + which, Toast.LENGTH_LONG).show();
- }
- });
- builder.create();
- builder.show();
三、多选列表
先看看效果图:
自带的多选构造函数有如下几个:
方法一:
- setMultiChoiceItems (CharSequence[] items, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener)
方法二:
- setMultiChoiceItems (int itemsId, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener)
方法三:
- setMultiChoiceItems (Cursor cursor, String isCheckedColumn, String labelColumn, DialogInterface.OnMultiChoiceClickListener listener)
下面以方法一为例看看多选列表的构造过程:
- String[] mItems = {"经常犯二", "傻叉一枚", "逗逼", "小清纯", "沉稳大叔", "有时可爱"};
- AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
- builder.setIcon(R.drawable.ic_launcher);
- builder.setTitle("性格类型");
- builder.setMultiChoiceItems(mItems,
- new boolean[]{false, false, false, false, false, false, false},
- new DialogInterface.OnMultiChoiceClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which, boolean isChecked) {
- Toast.makeText(MyActivity.this, "clicked:" + which, Toast.LENGTH_LONG).show();
- }
- }
- );
- builder.show();
- String[] mItems = {"经常犯二", "傻叉一枚", "逗逼", "小清纯", "沉稳大叔", "有时可爱"};
- new boolean[]{false, false, false, false, false, false, false}
四、自定义列表项
这节我带大家自定义列表项的实现,故名思义,就是每个ITEM都是我们自己来定义,使用我们自己的Adapter,先看看效果:
要实现自定义列表主要是造下面这个函数来实现的:
- setAdapter (ListAdapter adapter, DialogInterface.OnClickListener listener)
下面就带大家实现下图示中的视图:
1、构造Adapter
有关派生自BaseAdapter来构造ListViewAdapter的过程就不再细讲了,大家可以参考我以前的两篇文章:
1、《List控件使用---SimpleAdapter使用详解(一)》
2、《BaseAdapter——convertView回收机制与动态控件响应》
第 一篇现在看来着实太烂了,一直想重写又没时间来做,大家可以再搜搜网上的其它相关文章来学一下有关BaseAdapter的相关内容吧。第二篇有关 convertView的回收机制写的还是挺不错的,大家可以仔细看看,相信会对ListView的机制有一个更清晰的理解。废话不多说了,下面言转正 转,看看我们这里的ListAdapter的构造过程:
首先是单个Item的布局:(list_item.xml)
很简单,采用水平布局,左边一个图像,右边一个对应的TEXT,这里只是基本实现功能,大家完全可以在理解这篇文章的基础上派生出更复杂的视图
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="wrap_content"
- android:layout_height="match_parent">
- <ImageView
- android:id="@+id/item_imageview"
- android:layout_width="80dip"
- android:layout_height="80dip"
- android:scaleType="fitCenter"/>
- <TextView
- android:id="@+id/item_text"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:gravity="center_vertical|left"
- android:textSize="20sp"/>
- </LinearLayout>
- private class ViewHolder{
- public TextView mTvTitle;
- public ImageView mImageView;
- }
现在我们还需要一个类来存储视图类中TextView的标题字符串,和对应ImageView的图片资源的ID值,这就是我们的数据类:
- public static class DataHolder{
- public String title;
- public int ImageID;
- public DataHolder(String title,int imageID){
- this.title = title;
- this.ImageID = imageID;
- }
- }
首先是派生自BaseAdapter,然后是构造函数,我们传进去一个DataHolder的列表,用来存储每个Item应该显示的内容
- public class ListItemAdapter extends BaseAdapter {
- private List<DataHolder> mDataList = new ArrayList<DataHolder>();
- private LayoutInflater mInflater;
- public ListItemAdapter(Context context, ArrayList<DataHolder> datalist) {
- if (datalist != null && datalist.size() > 0) {
- mDataList.addAll(datalist);
- }
- mInflater = LayoutInflater.from(context);
- }
- ………………
- }
- public View getView(int position, View convertView, ViewGroup viewGroup) {
- ViewHolder holder = null;
- if (convertView == null) {
- holder=new ViewHolder();
- convertView = mInflater.inflate(R.layout.list_item, null);
- holder.mTvTitle = (TextView)convertView.findViewById(R.id.item_text);
- holder.mImageView = (ImageView)convertView.findViewById(R.id.item_imageview);
- convertView.setTag(holder);
- }else {
- holder = (ViewHolder)convertView.getTag();
- }
- holder.mImageView.setImageResource(mDataList.get(position).ImageID);
- holder.mTvTitle.setText(mDataList.get(position).title);
- return convertView;
- }
然后是利用传进来的数据,初始化列表Item:
- holder.mImageView.setImageResource(mDataList.get(position).ImageID);
- holder.mTvTitle.setText(mDataList.get(position).title);
到这里ListViewAdapter的构造过程就基本完成了,下面是完整的代码:
- /**
- * Created by harvic
- * date 2015-1-11
- * */
- public class ListItemAdapter extends BaseAdapter {
- private List<DataHolder> mDataList = new ArrayList<DataHolder>();
- private LayoutInflater mInflater;
- public ListItemAdapter(Context context,ArrayList<DataHolder> datalist){
- if (datalist != null && datalist.size()>0){
- mDataList.addAll(datalist);
- }
- mInflater = LayoutInflater.from(context);
- }
- @Override
- public int getCount() {
- return mDataList.size();
- }
- @Override
- public Object getItem(int position) {
- return mDataList.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup viewGroup) {
- ViewHolder holder = null;
- if (convertView == null) {
- holder=new ViewHolder();
- convertView = mInflater.inflate(R.layout.list_item, null);
- holder.mTvTitle = (TextView)convertView.findViewById(R.id.item_text);
- holder.mImageView = (ImageView)convertView.findViewById(R.id.item_imageview);
- convertView.setTag(holder);
- }else {
- holder = (ViewHolder)convertView.getTag();
- }
- holder.mImageView.setImageResource(mDataList.get(position).ImageID);
- holder.mTvTitle.setText(mDataList.get(position).title);
- return convertView;
- }
- private class ViewHolder{
- public TextView mTvTitle;
- public ImageView mImageView;
- }
- public static class DataHolder{
- public String title;
- public int ImageID;
- public DataHolder(String title,int imageID){
- this.title = title;
- this.ImageID = imageID;
- }
- }
- }
2、构造列表
在MainActivity中,首先是初始化我们的ListViewAdapter,要初始化Adapter,就要首先传进去一个DataHolder的数组,所有要先构造一个DataHolder的数组- private ArrayList<ListItemAdapter.DataHolder> initDataHolder(){
- ArrayList<ListItemAdapter.DataHolder> datalist = new ArrayList<ListItemAdapter.DataHolder>();
- ListItemAdapter.DataHolder data_1 = new ListItemAdapter.DataHolder("可爱萌宠1", R.drawable.animal1);
- ListItemAdapter.DataHolder data_2 = new ListItemAdapter.DataHolder("可爱萌宠2", R.drawable.animal2);
- ListItemAdapter.DataHolder data_3 = new ListItemAdapter.DataHolder("可爱萌宠3", R.drawable.animal3);
- ListItemAdapter.DataHolder data_4 = new ListItemAdapter.DataHolder("可爱萌宠4", R.drawable.animal4);
- datalist.add(data_1);
- datalist.add(data_2);
- datalist.add(data_3);
- datalist.add(data_4);
- return datalist;
- }
- private void createCustomList() {
- ArrayList<ListItemAdapter.DataHolder> dataHolders = initDataHolder();
- ListItemAdapter adapter = new ListItemAdapter(MyActivity.this, dataHolders);
- AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
- builder.setIcon(R.drawable.ic_launcher);
- builder.setTitle("可爱萌宠");
- builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int which) {
- Toast.makeText(MyActivity.this, "clicked:" + which, Toast.LENGTH_LONG).show();
- }
- });
- builder.create();
- builder.show();
- }
五、有关单选列表自定义Adapter
这里主要讲的函数是第二部分单选列表中的方法三:
- setSingleChoiceItems (ListAdapter adapter, int checkedItem, DialogInterface.OnClickListener listener)
也就是利用下面的代码:
我们直接在createCustomList()上面改一下,即把builder.setAdapter()直接换成setSingleChoiceItems() ,传进去Adpter,看看在我们定义的视图后面会不会出现一个单选按钮呢?
- private void createCustomList() {
- ArrayList<ListItemAdapter.DataHolder> dataHolders = initDataHolder();
- ListItemAdapter adapter = new ListItemAdapter(MyActivity.this, dataHolders);
- AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
- builder.setIcon(R.drawable.ic_launcher);
- builder.setTitle("可爱萌宠");
- builder.setSingleChoiceItems(adapter, 0,new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int which) {
- Toast.makeText(MyActivity.this, "clicked:" + which, Toast.LENGTH_LONG).show();
- }
- });
- builder.create();
- builder.show();
- }
看见了没,毛也没有,坑爹玩意,那这个函数到底是用来做什么的呢,在源码中有这样一个文件:
地址在:$android_sdk_home/platforms/Android-x.x/data/res/layout/simple_list_item_single_choice.xml
文件内容是这样的:
- <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/single_list_item"
- android:layout_width="match_parent"
- android:layout_height="?android:attr/listPreferredItemHeightSmall"
- android:textAppearance="?android:attr/textAppearanceListItemSmall"
- android:gravity="center_vertical"
- android:checkMark="?android:attr/listChoiceIndicatorSingle"
- android:paddingStart="?android:attr/listPreferredItemPaddingStart"
- android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
- />
1、新建Adapter
新建一个布局文件,来保存ListVIew的每项的Item,代码如下:- <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/single_list_item"
- android:layout_width="match_parent"
- android:layout_height="?android:attr/listPreferredItemHeightSmall"
- android:textAppearance="?android:attr/textAppearanceListItemSmall"
- android:gravity="center_vertical"
- android:checkMark="?android:attr/listChoiceIndicatorSingle"
- android:paddingStart="?android:attr/listPreferredItemPaddingStart"
- android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
- />
- private class ViewHolder{
- public TextView mName;
- }
所以简单的构造函数为:
在构造时,传进来我们每项要显示的标题的字符串数组String[] items,并将其直接添加到我们的List<String> mList中。
- public class SingleChoiceAdapter extends BaseAdapter {
- private LayoutInflater mInflater;
- private List<String> mList = new ArrayList<String>();
- public SingleChoiceAdapter(Context context, String[] items) {
- mInflater = LayoutInflater.from(context);
- if (items == null || items.length <= 0) {
- return;
- }
- for (String item : items) {
- mList.add(item);
- }
- }
- …………
- }
- public View getView(int position, View convertView, ViewGroup viewGroup) {
- ViewHolder holder = null;
- if (convertView == null) {
- holder=new ViewHolder();
- convertView = mInflater.inflate(R.layout.single_choice_item, null);
- holder.mName = (TextView)convertView.findViewById(R.id.single_list_item);
- convertView.setTag(holder);
- }else {
- holder = (ViewHolder)convertView.getTag();
- }
- holder.mName.setText(mList.get(position));
- return convertView;
- }
- holder.mName.setText(mList.get(position));
- /**
- * Created by harvic
- * date 2015-1-11
- */
- public class SingleChoiceAdapter extends BaseAdapter {
- private LayoutInflater mInflater;
- private List<String> mList = new ArrayList<String>();
- public SingleChoiceAdapter(Context context,String[] items){
- mInflater = LayoutInflater.from(context);
- if (items == null || items.length<=0){
- return;
- }
- for(String item:items){
- mList.add(item);
- }
- }
- @Override
- public int getCount() {
- return mList.size();
- }
- @Override
- public Object getItem(int position) {
- return mList.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup viewGroup) {
- ViewHolder holder = null;
- if (convertView == null) {
- holder=new ViewHolder();
- convertView = mInflater.inflate(R.layout.single_choice_item, null);
- holder.mName = (TextView)convertView.findViewById(R.id.single_list_item);
- convertView.setTag(holder);
- }else {
- holder = (ViewHolder)convertView.getTag();
- }
- holder.mName.setText(mList.get(position));
- return convertView;
- }
- private class ViewHolder{
- public TextView mName;
- }
- }
2、构造单选列表
这里首先构造SingleChoiceAdapter的实例,然后将其传入setSingleChoiceItems中,代码如下:
- String[] mItems = {"攻", "受", "全能型", "不告诉你"};
- SingleChoiceAdapter adapter = new SingleChoiceAdapter(MyActivity.this,mItems);
- AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
- builder.setIcon(R.drawable.ic_launcher);
- builder.setTitle("可爱萌宠");
- builder.setSingleChoiceItems(adapter, 0, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int which) {
- Toast.makeText(MyActivity.this, "clicked:" + which, Toast.LENGTH_LONG).show();
- }
- });
- builder.create();
- builder.show();
看到没,这里效果是出来了,可见,在单选按钮中,定义Adapter并不是随便定义的,只能定义CheckedTextView的视图,其它是不支持的!!!!
好了,到这里本篇就结束了,下篇给大家讲讲有关自定义对话框的内容。
如果本文有帮到你,记得关注哦
源码下载地址:http://download.csdn.net/detail/harvic880925/8355683
请大家尊重原创者版权,转载请标明出处:http://blog.csdn.net/harvic880925/article/details/42528663 谢谢!