Android中popuwindow中使用listview

在Android中,一个简单的popuwindow已经不能满足业务的需求了,我们可以使用popuwindow装listview来展示数据……


首先定义一个popuwindow布局,里面就是一个listview,上代码:


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="match_parent">

    <ListView
        android:id="@+id/lv_appointment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorWhite">ListView>

LinearLayout>


然后在Activity中定义方法:我这里布局中有两个箭头,一个显示一个隐藏,做的处理,如下



/**
 * 显示popuwindow
 *
 * @param parent
 */
private void showWindow(View parent) {
    LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = layoutInflater.inflate(R.layout.activity_reservation_popuwindow, null);
    ListView lv_appointment = (ListView) view.findViewById(R.id.lv_appointment);
    if (datas != null) {
        adapter = new MyAdapter(datas);
    }
    lv_appointment.setAdapter(adapter);

    // 创建一个PopuWidow对象,设置宽高
    popupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

    // 使其聚集,可点击
    popupWindow.setFocusable(true);
    // 设置允许在外点击消失
    popupWindow.setOutsideTouchable(true);

    //设置SelectPicPopupWindow弹出窗体动画效果
    popupWindow.setAnimationStyle(R.style.AnimationFade);

    // 这个是为了点击返回Back”也能使其消失,并且并不会影响你的背景
    popupWindow.setBackgroundDrawable(new BitmapDrawable());
    WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

    popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
    //实现ppopuwindow的消失监听
    popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            iv_down.setVisibility(View.VISIBLE);
            iv_up.setVisibility(View.GONE);
            isShow=true;
        }
    });

    //listview设置条目点击监听
    lv_appointment.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView adapterView, View view, int i, long l) {
            if (popupWindow != null) {
                popupWindow.dismiss();
                tv_appointment.setText(datas.get(i));

                iv_down.setVisibility(View.VISIBLE);
                iv_up.setVisibility(View.GONE);
                isShow = true;

            }
        }
    });

}



listview的适配器


class MyAdapter extends BaseAdapter {


    private String tvName;

    public MyAdapter(List datas) {

    }

    @Override
    public int getCount() {

        return datas.size();
    }

    @Override
    public Object getItem(int i) {
        return datas.get(i);
    }

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

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

        if (convertView == null) {

            convertView = View.inflate(context, R.layout.activty_appointment_list_item, null);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.bound_account_list_item_name);
            //打包
            convertView.setTag(holder);

        } else {

            holder = (ViewHolder) convertView.getTag();

        }
        tvName = datas.get(position);
        holder.name.setText(tvName);

        return convertView;
    }
}

class ViewHolder {
    TextView name;
}
//集合,添加数据

private List datas = new ArrayList<>();
//添加数据
datas.add("过户");
datas.add("改管");
datas.add("搬迁");

如果有好的建议,可以告诉我…………

你可能感兴趣的:(Android中popuwindow中使用listview)