查詢條件樣式的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效果如上圖,希望能幫到大家。
發佈了39 篇原創文章 · 獲贊 7 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章