1. 程式人生 > >Android 使用PopupWindow實現單級篩選列表,7.0bug解決

Android 使用PopupWindow實現單級篩選列表,7.0bug解決

解決頂部陰影不一致樣式

PopupWindow淡入淡出動畫anim

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

    <alpha
        android:duration="300"
        android:fromAlpha="0.01"
        android:toAlpha="1" />

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

    <alpha
        android:duration="300"
        android:fromAlpha="1"
        android:toAlpha="0.01" />

</set>

styles中新增樣式

 <style name="popwindow_anim">
        <item name="android:windowEnterAnimation">@anim/pop_in</item>
        <item name="android:windowExitAnimation">@anim/pop_out</item>
    </style>

popview 的樣式pop_view.xml

<?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"
    android:background="#b3000000"
    android:id="@+id/ll"
    android:orientation="vertical">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="@android:color/transparent"
        android:divider="@null"
        android:background="#ffffff"
        android:fadingEdge="none"
        android:listSelector="@android:color/transparent" />

</LinearLayout>

pop_view_item.xml

<?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="wrap_content"
    android:orientation="vertical">

    <View
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="#e4e4e4" />

    <TextView
        android:id="@+id/tv_time"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center"
        android:text="全部"
        android:textColor="#666666"
        android:textSize="13dp" />


</LinearLayout>

新增實體用於接收資料展示SelectStyle

import java.io.Serializable;

/**
 * item Bean
 */
public class SelectStyle implements Serializable {

    private static final long serialVersionUID = 1L;

    public String name;

}

彈出的popupwindow—SelectPopupWindow

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;


import java.util.List;

/**
 * 篩選
 */

public class SelectPopupWindow {

    private PopupWindow popupWindow;
    private ListView listView;
    private List<SelectStyle> data;
    private LayoutInflater layoutInflater;
    private int selected;
    private Context mContext;

    private MySelectPopupWindow mySelectPopupWindow;
    private LinearLayout linearLayout;

    public SelectPopupWindow(List<SelectStyle> list, final Context context) {
        this.mContext = context;
        if (popupWindow == null) {
            layoutInflater = LayoutInflater.from(context);
            this.data = list;
            final View popupView = layoutInflater.inflate(R.layout.pop_view, null);
            listView = (ListView) popupView.findViewById(R.id.listView);

            linearLayout = popupView.findViewById(R.id.ll);
            listView.setAdapter(new MyBaseAdpter());
            popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            popupWindow.setAnimationStyle(R.style.popwindow_anim);
            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            popupWindow.setFocusable(true);
            popupWindow.setOutsideTouchable(true);

            linearLayout.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    popupWindow.dismiss();
                    return true;
                }
            });

        }

    }

    public void setMySelectPopupWindow(MySelectPopupWindow mySelectPopupWindow) {
        this.mySelectPopupWindow = mySelectPopupWindow;
    }

    public void setBackgroundAlpha(float bgAlpha, Context mContext) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        ((Activity) mContext).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        ((Activity) mContext).getWindow().setAttributes(lp);
    }

    public void show(View view) {
        setBackgroundAlpha(1.0f, mContext);
        popupWindow.showAsDropDown(view);
    }


    public class MyBaseAdpter extends BaseAdapter {

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

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            HolderView holderView = null;
            if (convertView == null) {
                holderView = new HolderView();
                convertView = layoutInflater.inflate(R.layout.pop_view_item, null);
                holderView.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
                convertView.setTag(holderView);
            } else {
                holderView = (HolderView) convertView.getTag();
            }
            SelectStyle selectStyle = data.get(position);
            holderView.tv_time.setText(selectStyle.name);
            if (position == selected) {
                holderView.tv_time.setTextColor(Color.parseColor("#ff6d00"));
            } else {
                holderView.tv_time.setTextColor(Color.parseColor("#333333"));
            }
            holderView.tv_time.setOnClickListener(new MyOnClickListener(position, selectStyle));
            return convertView;
        }

        public class HolderView {
            TextView tv_time;
        }

        public class MyOnClickListener implements View.OnClickListener {
            private int postion;
            private SelectStyle selectStyle;

            public MyOnClickListener(int postion, SelectStyle selectStyle) {

                this.postion = postion;
                this.selectStyle = selectStyle;

            }
            @Override
            public void onClick(View v) {
                selected = postion;
                mySelectPopupWindow.itemOnclick(selected, selectStyle);
                notifyDataSetChanged();
                setBackgroundAlpha(1f, mContext);
                popupWindow.dismiss();
            }
        }


    }

    public interface MySelectPopupWindow {
        public void itemOnclick(int position, SelectStyle selectStyle);
    }
}

活動佈局簡單設定activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="顯示"
            android:textSize="15sp" />

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="no clickable"
            android:textSize="15sp" />

        <Button
            android:id="@+id/button3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="no clickable"
            android:textSize="15sp" />

    </LinearLayout>

</LinearLayout>

活動中呼叫MainActivity

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements SelectPopupWindow.MySelectPopupWindow {

    private Button button1;


    /**
     * 篩選pop
     */
    private SelectPopupWindow selectPopupWindow;
    private List<SelectStyle> selectStyles = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1 = findViewById(R.id.button1);

        for (int i = 0; i < 5; i++) {
            SelectStyle selectStyle = new SelectStyle();
            selectStyle.name = "balance" + i;
            selectStyles.add(selectStyle);
        }

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectStyles != null && selectStyles.size() > 0) {
                    if (selectPopupWindow == null) {
                        showBalancePopList();
                    }
                    selectPopupWindow.show(button1);
                }
            }
        });
    }

    private SelectPopupWindow showBalancePopList() {
        selectPopupWindow = new SelectPopupWindow(selectStyles, this);
        selectPopupWindow.setMySelectPopupWindow(this);
        return selectPopupWindow;


    }

    @Override
    public void itemOnclick(int position, SelectStyle selectStyle) {
        Toast.makeText(MainActivity.this, "position=" + position, Toast.LENGTH_SHORT).show();
    }
}

bug修改,在 Android N(7.0) 的相容性問題

原因https://www.jianshu.com/p/0df10893bf5b
解決https://www.cnblogs.com/cherrylv/p/6565443.html

針對上面專案,重寫popupwindow—-SupportPopupWindow

import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.widget.PopupWindow;

/**
 * Created by Sugar on 2018/3/26/0026.
 */

public class SupportPopupWindow extends PopupWindow {

    public SupportPopupWindow(View contentView, int width, int height) {
        super(contentView, width, height);
    }

    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        if (Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor, xoff, yoff);
    }
}

修改SelectPopupWindow中的popupwindow為SupportPopupWindow

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

/**
 * 篩選
 */

public class SelectPopupWindow {

    private SupportPopupWindow popupWindow;
    private ListView listView;
    private List<SelectStyle> data;
    private LayoutInflater layoutInflater;
    private int selected;
    private Context mContext;

    private MySelectPopupWindow mySelectPopupWindow;
    private LinearLayout linearLayout;

    public SelectPopupWindow(List<SelectStyle> list, final Context context) {
        this.mContext = context;
        if (popupWindow == null) {
            layoutInflater = LayoutInflater.from(context);
            this.data = list;
            final View popupView = layoutInflater.inflate(R.layout.pop_view, null);
            listView = (ListView) popupView.findViewById(R.id.listView);

            linearLayout = popupView.findViewById(R.id.ll);
            listView.setAdapter(new MyBaseAdpter());
            popupWindow = new SupportPopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            popupWindow.setAnimationStyle(R.style.popwindow_anim);
            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            popupWindow.setFocusable(true);
            popupWindow.setOutsideTouchable(true);

            linearLayout.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    popupWindow.dismiss();
                    return true;
                }
            });

        }

    }

    public void setMySelectPopupWindow(MySelectPopupWindow mySelectPopupWindow) {
        this.mySelectPopupWindow = mySelectPopupWindow;
    }

    public void setBackgroundAlpha(float bgAlpha, Context mContext) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        ((Activity) mContext).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        ((Activity) mContext).getWindow().setAttributes(lp);
    }

    public void show(View view) {

        setBackgroundAlpha(1.0f, mContext);
        popupWindow.showAsDropDown(view);

    }


    public class MyBaseAdpter extends BaseAdapter {

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

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            HolderView holderView = null;
            if (convertView == null) {
                holderView = new HolderView();
                convertView = layoutInflater.inflate(R.layout.pop_view_item, null);
                holderView.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
                convertView.setTag(holderView);
            } else {
                holderView = (HolderView) convertView.getTag();
            }
            SelectStyle selectStyle = data.get(position);
            holderView.tv_time.setText(selectStyle.name);
            if (position == selected) {
                holderView.tv_time.setTextColor(Color.parseColor("#ff6d00"));
            } else {
                holderView.tv_time.setTextColor(Color.parseColor("#333333"));
            }
            holderView.tv_time.setOnClickListener(new MyOnClickListener(position, selectStyle));
            return convertView;
        }

        public class HolderView {
            TextView tv_time;
        }

        public class MyOnClickListener implements View.OnClickListener {
            private int postion;
            private SelectStyle selectStyle;

            public MyOnClickListener(int postion, SelectStyle selectStyle) {

                this.postion = postion;
                this.selectStyle = selectStyle;

            }

            @Override
            public void onClick(View v) {
                selected = postion;
                mySelectPopupWindow.itemOnclick(selected, selectStyle);
                notifyDataSetChanged();
                setBackgroundAlpha(1f, mContext);
                popupWindow.dismiss();
            }
        }


    }

    public interface MySelectPopupWindow {
        public void itemOnclick(int position, SelectStyle selectStyle);
    }
}