下拉选择弹窗

在项目中很多的时候会用到下拉弹窗,进行条件的筛选等,具体的如下图所示,简单的记录一下。


1、方法调用----在方法中传的是需要的参数

public void showServiceTypeDialog(final Activity mActicity, final CheckBox v,
        final String curProCode, final String orderBy) {
    final List<String> mlist = new ArrayList<>();
    mlist.clear();
    mlist.add("全部");
    mlist.add("印刷");
    mlist.add("设计");
    mlist.add("排版");
    mlist.add("编辑");

    final TypePopWindow popWindow = new TypePopWindow(mActivity, mlist, "");
    popWindow.setListItemClickListener(new TypePopWindow.IListItemClickListener() {
        @Override public void clickItem(int position) {
            mActivity.serverDate.clear();
            Log.i("OkHttp","点击的==="+position);
            getSertverList_paramet().setPageNo("1");
            if (position == 0) {//positon代表位置
                loadServerListData("", "", orderBy);
            } else {//实现业务内容
                loadServerListData(curProCode, position + "", orderBy);
            }
            v.setText(mlist.get(position));
        }
    });
    popWindow.setWidth(v.getWidth());
    popWindow.setHeight(UIUtil.dip2px(40) * mlist.size());
    popWindow.showAsDropDown(v);
}
2.封装popuwindow
 
 
public class TypePopWindow extends PopupWindow {
    private View mConentView;                // PopupWindow视图
    private Activity mActivity;
    private Context mContext;
    private SpinerListAdapter mAdapter;
    private IListItemClickListener mListItemClickListener;
    private List<String> mDatas;
    private String type;

    /**
     * @param context
     * @param datas
     * @param type left 条目居左,其他居中
     */
    public TypePopWindow(Context context, List<String> datas,String type) {
        super(context);
        this.mActivity = (Activity) context;
        this.mContext = context;
        this.mDatas = datas;
        this.type = type;
        LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mConentView = inflater.inflate(R.layout.pop_window_list_layout, null);
        this.setContentView(mConentView);
        initPopWindow();
        initView();
    }

    @SuppressWarnings("deprecation")
    private void initPopWindow() {
        // 设置SelectPicPopupWindow弹出窗体的宽
        // int width =
        // mActivity.getWindowManager().getDefaultDisplay().getWidth();
        // this.setWidth(width / 2 + 50);
        // this.setWidth(width - 100);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(false);
        // 刷新状态
        this.update();
        // back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作
        this.setBackgroundDrawable(new BitmapDrawable());
        // 设置SelectPicPopupWindow弹出窗体动画效果
        //this.setAnimationStyle(android.R.style.Animation_Dialog);
        this.setAnimationStyle(R.style.add_popwin_anim_style);// 自定义动画
        // 幕背景变暗
        // setBackgroundAlpha(0.5f);
    }

    private void initView() {
        ListView mListView = (ListView) mConentView.findViewById(R.id.lv_popwin);
        mListView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        mAdapter = new SpinerListAdapter();
        mListView.setAdapter(mAdapter);

        mListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mListItemClickListener.clickItem(position);
                TypePopWindow.this.dismiss();
            }
        });
    }

    /**
     * 设置添加屏幕的背景透明度
     * @param bgAlpha
     */
    public void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        lp.alpha = bgAlpha; // 0.0-1.0
        mActivity.getWindow().setAttributes(lp);
    }

    class SpinerListAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            if (mDatas != null) {
                return mDatas.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            if (mDatas != null) {
                return mDatas.get(position);
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;

            if (convertView == null) {
                if (type.equals("left")) {
                    convertView = View.inflate(mContext, R.layout.item_poplist2, null);
                }else {
                    convertView = View.inflate(mContext, R.layout.item_poplist, null);
                }
                holder = new ViewHolder();
                holder.tv = (TextView) convertView.findViewById(R.id.tv_type);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.tv.setText(mDatas.get(position));
            return convertView;
        }
    }

    static class ViewHolder {
        TextView tv;
    }

    public interface IListItemClickListener {
        void clickItem(int position);
    }

    public void setListItemClickListener(IListItemClickListener listener) {
        this.mListItemClickListener = listener;
    }

}
3.popuwindows的布局文件-----
pop_window_list_layout
 
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/global"
    android:paddingTop="1dp"
    android:paddingLeft="1dp"
    android:paddingRight="1dp"
    android:paddingBottom="1dp"
    android:orientation="vertical" >

    <ListView
        android:background="@color/global"
        android:id="@+id/lv_popwin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:divider="#CCCCCC"
        android:dividerHeight="0.5dp"
        android:fadingEdge="none"
        android:scrollbars="none" />

</LinearLayout>
4.下拉动画--
add_popwin_anim_style
 
 
<!-- 下拉框动画 -->
<style name="add_popwin_anim_style">
    <item name="android:windowEnterAnimation">@anim/pop_enter</item>
    <item name="android:windowExitAnimation">@anim/pop_out</item>
</style>
5动画--
pop_enter
 
 
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <scale
        android:duration="150"
        android:fromXScale="1"
        android:fromYScale="0"
        android:pivotX="100%"
        android:pivotY="0%"
        android:toXScale="1.0"
        android:toYScale="1.0" /> 

    <alpha
        android:duration="150"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/decelerate_interpolator"
        android:toAlpha="1.0" />

</set>
6.
pop_out
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

     <scale
        android:duration="150"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="100%"
        android:pivotY="0%"
        android:toXScale="1"
        android:toYScale="0" />

    <alpha
        android:duration="150"
        android:fromAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="0.0" />

</set>
7.adapter的xml----
item_poplist2
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:background="@drawable/list_item" >

    <TextView
        android:id="@+id/tv_type"
        android:gravity="center_vertical"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="内容"
        android:textColor="@color/main_text_color"
        android:textSize="12sp" />

</RelativeLayout>
8.xml----
item_poplist
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:background="@drawable/list_item" >

    <TextView
        android:id="@+id/tv_type"
        android:gravity="center"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="内容"
        android:textColor="@color/main_text_color"
        android:textSize="12sp" />

</RelativeLayout>
9.Uiutil操作类
/**
 * @param dip
 * @return
 */
public static int dip2px(int dip) {
    // 公式 1: px = dp * (dpi / 160)
    // 公式 2: dp = px / denistity;
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    float density = metrics.density;
    // metrics.densityDpi
    return (int) (dip * density + 0.5f);
}



猜你喜欢

转载自blog.csdn.net/wzs0316xuan/article/details/79088114
今日推荐