Android popwindow的使用例子

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

相關文章
相關標籤/搜索