分享一個自定義的popuwindow效果,高度適配

在很多項目中我們可能會遇到這種效果。現在很多的應用效果都需要做的炫些,比如天天靜聽效果很炫的,源碼已經對外開放了,有興趣的可以去研究下;

直接上代碼:

1.佈局文件:

popwindow.xml:

<?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"
    android:gravity="center"
    android:background="@android:color/transparent">

<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:dividerHeight="@dimen/divider_width_max"
    android:paddingTop="@dimen/padding_20"
    android:paddingLeft="@dimen/padding_10"
    android:paddingRight="@dimen/padding_10"
    android:divider="@color/color_diver"
    android:paddingBottom="@dimen/padding_10"
    android:scrollbars="none"
    android:listSelector="@color/white"
    android:cacheColorHint="@color/white"
    >

</ListView>
</LinearLayout>

2.listview的item佈局item_popupwindow.xml:

<?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="40dp"
    android:layout_gravity="center"
    android:layout_marginTop="@dimen/padding_20"
    android:gravity="center"
    >

    <TextView
        android:id="@+id/tv_item_popupwindow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="@dimen/font_size_14"
        android:textColor="@color/dark_grey"
        android:text="y你老酒水"/>
</RelativeLayout>

3.listview的適配器GroupAdapater 繼承BaseAdapter,做了選中顏色變化處理和固定高度處理:

package com.duora.duoraorder_version1.adapter;

import android.content.Context;
import android.content.res.ColorStateList;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.duora.duoraorder_version1.R;
import com.duora.duoraorder_version1.helper.DesityUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by bobge on 15/8/5.
 */
public class GroupAdapter extends BaseAdapter {
    private Context context;
    private List<Map<String,String>> groups;
    private TextView tv_kind_show;
    public GroupAdapter(Context context, List<Map<String,String>> groups,TextView tv_kind_show) {
        this.context=context;
        this.groups=groups;
        this.tv_kind_show=tv_kind_show;
    }

    @Override
    public int getCount() {
        return groups.size();
    }

    @Override
    public Object getItem(int position) {
        return groups.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view=convertView;
        ViewHolder holder;
        if (view==null){
            view=LayoutInflater.from(context).inflate(R.layout.item_popupwind,parent,false);
            AbsListView.LayoutParams param = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    DesityUtils.dip2px(context, 40));
            view.setLayoutParams(param);
            holder=new ViewHolder();
            holder.tv_kind= (TextView) view.findViewById(R.id.tv_item_popupwindow);
            view.setTag(holder);
        }else {
            holder= (ViewHolder) view.getTag();
        }
        holder.tv_kind.setText(groups.get(position).get("name"));
        if (holder.tv_kind.getText().equals(tv_kind_show.getText())){
            ColorStateList colorStateList=context.getResources().getColorStateList(R.color.red);
            holder.tv_kind.setTextColor(colorStateList);
        }else{
            ColorStateList colorStateList=context.getResources().getColorStateList(R.color.dark_grey);
            holder.tv_kind.setTextColor(colorStateList);
        }

        return view;
    }
    class ViewHolder{
        TextView tv_kind;
    }
}

4.最後上自定義selectKindPopWin類:

public class SelectKindPopWin extends PopupWindow {
    private ListView listView;
    private View mMenuView;

    private List<Map<String,String>> groups_catogry;
   /* //模擬數據
    private List<String> groups;*/
    private TextView tv_kind;
    private FragmentActivity context;
    private TextView tv_bg_detail;

    public SelectKindPopWin(FragmentActivity context,TextView tv_kind,TextView tv_bg_detail) {
        super(context);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.popwindow, null);
        listView = (ListView) mMenuView.findViewById(R.id.listview);
        this.tv_kind=tv_kind;
        this.context=context;
        this.tv_bg_detail=tv_bg_detail;

        addListener(listView,this);

        //設置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //設置SelectPicPopupWindow彈出窗體的寬——>匹配不同機型的適配
        int screenWith=context.getWindowManager().getDefaultDisplay().getWidth();
        this.setWidth((screenWith*5)/12);
        //設置SelectPicPopupWindow彈出窗體的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        //設置SelectPicPopupWindow彈出窗體可點擊
        this.setFocusable(true);
        // 設置允許在外點擊消失
        this.setOutsideTouchable(true);
        //設置SelectPicPopupWindow彈出窗體動畫效果
        //  this.setAnimationStyle(R.style.AnimBottom);
        // 這個是爲了點擊“返回Back”也能使其消失,並且並不會影響你的背景
        this.setBackgroundDrawable(context.getResources().getDrawable(R.mipmap.bg_popupwindow));

    }

    private void addListener(ListView listView, final SelectKindPopWin selectKindPopWin) {

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                tv_kind.setText(groups_catogry.get(position).get("name"));
                // TODO Auto-generated method stub
                EventBus.getDefault().post(
                        new MessageEvent(groups_catogry.get(position).get("parent_id")));
                if (selectKindPopWin != null) {
                    selectKindPopWin.dismiss();
                }
                resetTextColor();
            }
        });
        selectKindPopWin.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                tv_bg_detail.setVisibility(View.GONE);
            }
        });
    }

    private void resetTextColor(){

        for (int i = 0; i < groups_catogry.size(); i++) {
            TextView tv=(TextView)listView.getChildAt(i).findViewById(R.id.tv_item_popupwindow);
            if (groups_catogry.get(i).get("name").equals(tv_kind.getText().toString())){
                tv.setTextColor(Color.RED);
            }else
                tv.setTextColor(Color.BLACK);
        }
    }

    public void addNetData(){
        ACache aCache=ACache.get(context);
        String local=aCache.getAsString(BaseConfig.HOME_GRID_DATA);
        if (local!=null){
            Gson_Category categoryBean = GsonHelper.getPerson(local, Gson_Category.class);
            groups_catogry = new ArrayList<Map<String,String>>();
            for (int i = 0; i < categoryBean.getResult().size(); i++) {
                Map<String,String> map=new HashMap<String,String>();
                map.put("name",categoryBean.getResult().get(i).getName());
                map.put("parent_id",categoryBean.getResult().get(i).getId());
                groups_catogry.add(map);
            }
            GroupAdapter groupAdapter = new GroupAdapter(context, groups_catogry,tv_kind);
            listView.setAdapter(groupAdapter);
        }
    }
    public void showPopWin(View view) {
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        int xPos=-this.getWidth()/2+view.getWidth()/2;
        this.showAsDropDown(view, xPos, 4);
        if (this.isShowing()){
            tv_bg_detail.setVisibility(View.VISIBLE);
        }
    }
}

這個例子直接跑是跑不起來的,這是我項目中的例子。數據源是我從本地取的,只需要替換這部分就可以。自定義類的參數有幾個控件,tv_kind用來顯示標題內容,tv_bg_detail是popupwindow後面的陰影。顏色可以自己定義。這些背景我更喜歡用FrameLayout佈局來實現。只需要用view的gone和visible實現就可以。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章