popwindow可以在你指定的地方弹出一个框来显示你想要的内容,貌似系统的spinner也是用的popwind来实现的(其中的一种方式),所以 想自定义一个控制更容易的下拉框用popwindow比较简单好实现
具体代码:
此类用来呈现下来框的点击按钮多个可以水平滑动
public class PullDownController extends HorizontalScrollView implements ComBoxCallBack
{
public ArrayList<ComBox> comBoxs_list;
MXPullDownMenuCallBack callBack;
Context context;
LinearLayout linearLayout;
public PullDownController(Context context, MXPullDownMenuCallBack callBack)
{
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