查询条件样式的PopWindow

popwind在Android项目中用到的非常多今天就写一下我在项目中使用的情况,以后方便使用,下面直接上代码。

当点击城市模块的布局时候响应
ll_city.setOnClickListener(this);
执行以下操作
//城市
case R.id.ll_city:

    View view = LayoutInflater.from(this).inflate(R.layout.popup_area, null);
    lv_city = (ListView) view.findViewById(R.id.lv_area);
    initPopwind(view,ll_city);
    CityAdapter cityAdapter = new CityAdapter(this, cityList);
    lv_city.setAdapter(cityAdapter);
    //城市点击事件
    lv_city.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            popupWindow.dismiss();
            CityAdapter adapter = (CityAdapter) (parent.getAdapter());

            //根据左侧一级分类选中情况,更新背景色
            adapter.setSelectedPosition(position);
            adapter.notifyDataSetChanged();
            tv_city.setText(cityList.get(position).getCity_name());
            //请求最新数据
            city_id = cityList.get(position).getCity_id();
            requestData(province_id, city_id, 0);

        }
    });
    break;
初始化popwind,设置popwind的高度为屏幕的百分之60
private void initPopwind(View view, final View ll_view) {
    LinearLayout layout = new LinearLayout(PartyActivity.this);
    WindowManager wm = (WindowManager)
            getSystemService(Context.WINDOW_SERVICE);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT, (int) (screenHeight * 0.6));
    view.setLayoutParams(params);
    layout.addView(view);
    layout.setBackgroundColor(Color.argb(60, 0, 0, 0));//设置整个popwind的底色有透明覆盖效果
    popupWindow = new PopupWindow(layout, screenWidth, screenHeight);
    popupWindow.setFocusable(true);
    popupWindow.setBackgroundDrawable(new BitmapDrawable());
    popupWindow.setOutsideTouchable(true);
    layout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popupWindow.dismiss();
        }
    });
    popupWindow.showAsDropDown(ll_view);
}
popwind所加载的布局文件,你可以根据自己的需求更改,我需要展示的是城市列表所以写成了listview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/lv_area"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:divider="@color/liner_color_grag"
        android:dividerHeight="1dp"
        android:background="#fff"/>
</LinearLayout>
城市listview的adapter适配器
/**
 * 城市的adapter
 * Created by wss.
 */
public class CityAdapter extends BaseAdapter{
    private Context context;
    private List<CityBean> list;

    public CityAdapter(Context context, List<CityBean> list){
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.right_listview_item, null);
            holder.nameTV = (TextView) convertView.findViewById(R.id.right_item_name);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.nameTV.setText(list.get(position).getCity_name());

        return convertView;
    }

    private int selectedPosition = 0;

    public void setSelectedPosition(int selectedPosition) {
        this.selectedPosition = selectedPosition;
    }

    public int getSelectedPosition() {
        return selectedPosition;
    }


    private class ViewHolder{
        TextView nameTV;
    }

}
listview条目的布局文件,我这个写的布局很简单,自己可以根据需要写,如果是布局已经定好,条目固定而且数量有限就可以不用使用listview了,也就不需要adapter和条目的布局文件了。
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    >

    <TextView
        android:id="@+id/right_item_name"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:textSize="14sp"
        android:textColor="@color/province"
        android:text="安钢"
        android:maxLength="10"
        android:ellipsize="end"
        android:gravity="center_vertical"
        android:background="@color/lv_bg"
        android:paddingLeft="10dp"
        />

</LinearLayout>
popwind代码写完了效果如下:

这个效果需要加上siderbar,下一篇博客会介绍这个功能

popwind效果如上图,希望能帮到大家。
阅读更多
个人分类: android
想对作者说点什么? 我来说一句

Popupwindow的创建和样式的设计

2016年05月17日 18.85MB 下载

没有更多推荐了,返回首页

不良信息举报

查询条件样式的PopWindow

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭