popwindow能够在你指定的地方弹出一个框来显示你想要的内容,貌似系统的spinner也是用的popwind来实现的(其中的一种方式),因此 想自定义一个控制更容易的下拉框用popwindow比较简单好实现ide
具体代码:布局
此类用来呈现下来框的点击按钮多个能够水平滑动学习
public class PullDownController extends HorizontalScrollView implements ComBoxCallBackthis
{spa
public ArrayList<ComBox> comBoxs_list;.net
MXPullDownMenuCallBack callBack;ip
Context context;get
LinearLayout linearLayout;it
public PullDownController(Context context, MXPullDownMenuCallBack callBack)io
{
super(context);
this.context = context;
this.callBack = callBack;
linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
comBoxs_list = new ArrayList<ComBox>();
setBackgroundColor(Color.BLUE);
int count = callBack.numberOfColumnsInMenu(this);
for(int i=0; i<count; i++)
{
ComBox comBox =new ComBox(context, this, i);
comBoxs_list.add(comBox);
linearLayout.addView(comBox,new LayoutParams(100, LayoutParams.MATCH_PARENT));
}
linearLayout.setGravity(Gravity.CENTER);
linearLayout.setBackgroundColor(Color.YELLOW);
addView(linearLayout,new LayoutParams(200,50));
}
public interface MXPullDownMenuCallBack
{
public int numberOfColumnsInMenu(PullDownController menu);// 返回有多少列
public String currentTitleForColumn(PullDownController menu, int currentTitleForColumn);// 列标题
public ArrayList<String> inforOfRowsInColumn(PullDownController menu, int numberOfRowsInColumn);// 一列包含的所有行
public int sizeOfTitleForColum(PullDownController menu, int column);
public void SelectRowAtColumn(PullDownController menu, int column, int row);
}
@Override
public void OnComBoxItemClick(int column, int row)
{
callBack.SelectRowAtColumn(this, column, row);
}
@Override
public ArrayList<String> getListDataSource(int column)
{
return callBack.inforOfRowsInColumn(this, column);
}
@Override
public String currentTitleForColumn(int column)
{
return callBack.currentTitleForColumn(this, column);
}
public void ondestroy()
{
if (null != comBoxs_list || !comBoxs_list.isEmpty())
{
for (ComBox comBox : comBoxs_list)
{
comBox = null;
}
comBoxs_list.clear();
}
}
}
此类用来实现点击后popwindow弹出的内容
public class ComBox extends LinearLayout
{
PopupWindow popupWindow;
Context context;
LinearLayout combox_layout;
TextView combox_text;
ImageView combox_image;
int column = 0;
ComBoxCallBack callBack;
public ComBox(Context context, ComBoxCallBack callBack, int column)
{
super(context);
this.context = context;
this.callBack = callBack;
this.column = column;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
combox_layout = (LinearLayout) inflater.inflate(R.layout.combox_layout, null);
combox_layout.setBackgroundResource(R.drawable.cell_left_title);
combox_text = (TextView) combox_layout.findViewById(R.id.combox_text);
combox_image = (ImageView) combox_layout.findViewById(R.id.combox_image);
combox_text.setText(callBack.currentTitleForColumn(column));
combox_layout.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
showList(v);
}
});
addView(combox_layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
public void setOnComBoxItemClick(ComBoxCallBack callBack)
{
this.callBack = callBack;
}
public void showList(View view)
{
// 一个自定义的布局,做为显示的内容
ListView list = new ListView(context);
list.setAdapter(new ArrayAdapter<String>(context, R.layout.spinner_item, callBack.getListDataSource(column)));
list.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
combox_text.setText(parent.getItemAtPosition(position).toString());
callBack.OnComBoxItemClick(column, position);
popupWindow.dismiss();
popupWindow = null;
}
});
popupWindow = new PopupWindow(list, 100, 200, true);
popupWindow.setTouchable(true);
popupWindow.setClippingEnabled(true);
popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
popupWindow.setOnDismissListener(new OnDismissListener()
{
@Override
public void onDismiss()
{
popupWindow = null;
}
});
// 若是不设置PopupWindow的背景,不管是点击外部区域仍是Back键都没法dismiss弹框
popupWindow.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_launcher));
//此处用-200由于上面给出的固定值是200
popupWindow.showAsDropDown(view, 0, -200 - view.getHeight());
}
public interface ComBoxCallBack
{
public void OnComBoxItemClick(int column, int row);
public ArrayList<String> getListDataSource(int column);
public String currentTitleForColumn(int column);
}
}
这2个类就能够实现最基本简单的下落框功能,用起来也很简单调用的地方实现MXPullDownMenuCallBack便可
因为很简单也就很少作说明,但愿你们不要笑话,哪里很差但愿你们指出,使我提升学习
例子文件,例子比较混乱,随手试验的http://download.csdn.net/detail/yx19861211/8727587