效果圖如下:
GIF.gif
實現(xiàn)流程:
1斤寂、編寫列表Item布局:area_code_item_view.
很簡單填帽,就是一個TeztView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/area_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="123"
android:textColor="@color/c_black"
android:textSize="@dimen/text_size_20" />
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/c_bg_gray_light" />
</LinearLayout>
圖片.png
2评也、編寫列表適配器Adapter:JobPopupWindowAdapter
JobPopupWindowAdapter 繼承于BaseAdapter瞳脓,構(gòu)造方法中將數(shù)據(jù)源以及選擇某一項數(shù)據(jù)的回調(diào)方法傳進來
public JobPopupWindowAdapter(Context context,ArrayList<CodeDataResponse> data, PopupWindowCodeItemSelectInterface selectInterface) {
mContext = context;
mData = data;
mSelectInterface = selectInterface;
}
PopupWindowCodeItemSelectInterface 是用于選擇PopupWindow中列表的某一項
public interface PopupWindowCodeItemSelectInterface {
//這里按照自己的數(shù)據(jù)源做更改悔政,我這里的數(shù)據(jù)源是通過數(shù)據(jù)字典接口獲取,同事需要用到文字對應(yīng)的編碼
public void onItemSelected(CodeDataResponse item);
}
完整代碼如下:
public class JobPopupWindowAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<CodeDataResponse> mData;
private PopupWindowCodeItemSelectInterface mSelectInterface;
public JobPopupWindowAdapter(Context context,ArrayList<CodeDataResponse> data, PopupWindowCodeItemSelectInterface selectInterface) {
mContext = context;
mData = data;
mSelectInterface = selectInterface;
}
@Override
public int getCount() {
return mData != null ? mData.size() : 0;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.area_code_item_view, null);
}
// String areaCodeStr = mData.get(position);
CodeDataResponse codeDataResponse = mData.get(position);
TextView areaCode = (TextView) convertView.findViewById(R.id.area_code);
areaCode.setText(codeDataResponse.aaa103);
convertView.setTag(codeDataResponse);
convertView.setOnClickListener(mOnClickListener);
return convertView;
}
private View.OnClickListener mOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mSelectInterface != null) {
CodeDataResponse mCodeDataResponse = (CodeDataResponse)v.getTag();
// String code = String.valueOf(v.getTag());
mSelectInterface.onItemSelected(mCodeDataResponse);
}
}
};
}
3桥言、封裝通用的顯示PopupWindow的方法:showPupupWindow(JobPopupWindowAdapter amAdapter,View view)
/**
* 選擇的popupwindow
* amAdapter 適配器
* view 需要在對應(yīng)的那個控件中顯示
*/
private void showPupupWindow(JobPopupWindowAdapter amAdapter,View view) {
mView = view;
//PupupWindow的寬
int windowWidth = 450;//這里可以修改成動態(tài)修改成控件的長度
//PupupWindow的高
int windowHeight = 300;
if (mPopupWindowWindow == null) {
View contentView = LayoutInflater.from(mContext).inflate(R.layout.popup_window_listview_layout, null);
mPopupWindowWindow = new PopupWindow(contentView, windowWidth, windowHeight);
// 設(shè)置點擊屏幕其它地方彈出框消失
mPopupWindowWindow.setFocusable(true);
mPopupWindowWindow.setOutsideTouchable(true);
mPopupWindowWindow.setBackgroundDrawable(new BitmapDrawable());
ListView listView = (ListView) contentView.findViewById(R.id.listview);
listView.setAdapter(amAdapter);
}
if (!mPopupWindowWindow.isShowing()) {
int[] location = new int[2];
view.getLocationOnScreen(location); // 獲取組件在屏幕中的位置
mPopupWindowWindow.showAsDropDown(view, 0, 0);
}
}
4萌踱、點擊PopupWindow列表中具體某一項,PopupWindow消失的方法 hidePopupWindow()
/**
* 點擊item清除PopupWindow
*/
private void hidePopupWindow() {
if (mPopupWindowWindow == null) {
LogUtils.d("mPopupWindowWindow == null");
}
if (mPopupWindowWindow != null && mPopupWindowWindow.isShowing()) {
mPopupWindowWindow.dismiss();
LogUtils.d("hidePopupWindow=====dismiss");
}
}
5号阿、調(diào)用步驟
首先在對應(yīng)的Activity和Fragment中實現(xiàn)選擇的PopupWindowCodeItemSelectInterface 接口并鸵,實現(xiàn)對應(yīng)的方法
ublic class WriteJobIntentionInfoActivity extends BaseActivity implements PopupWindowCodeItemSelectInterface{
@Override
public void onItemSelected(CodeDataResponse item) {
if (mView.getId() == rltJobs.getId()){
etJobs.setText(item.aaa103);
mJobNameNum = item.aaa102;
}else if (mView.getId() == rltSalary.getId()){
etSalary.setText(item.aaa103);
mSalaryNum = item.aaa102;
}else if (mView.getId() == rltWorkTime.getId()){
etWorkTime.setText(item.aaa103);
}
hidePopupWindow();
}
}
接著在需要展示PopupWindow的地方,調(diào)用其方法扔涧,我這里是點擊EdText的時候园担,onClick中的代碼如下:
if (v.equals(etJobs)){ //求職崗位
setDataToPopupWindow(mRPCodeLists,this,rltJobs);
}else if (v.equals(etSalary)){ //薪資要求
setDataToPopupWindow(mRSCodeLists,this,rltSalary);
}else if (v.equals(etWorkTime)){ //到崗時間
setDataToPopupWindow(mWDCodeLists,this,rltWorkTime);
}
setDataToPopupWindow(mWDCodeLists,this,rltWorkTime);方法對之前的統(tǒng)一封裝的PopupWindow展示的方法showPupupWindow()再做一次封裝,為了調(diào)用方便
/**
* 組裝顯示poupwindow的方法
* @param mInterface
*/
private void setDataToPopupWindow(List<CodeDataResponse> list, PopupWindowCodeItemSelectInterface mInterface, View editTextView){
mCodes.clear();
mCodes.addAll(list);
mJobPopupWindowAdapter = new JobPopupWindowAdapter(mContext,mCodes, mInterface);
showPupupWindow(mJobPopupWindowAdapter,editTextView);
}
好了枯夜,這樣子就應(yīng)該可以實現(xiàn)上面動圖的效果弯汰!