关于PopupWindow点击不同按钮显示不同pop的解决

来源:互联网 发布:苹果cms整合ck播放器 编辑:程序博客网 时间:2024/05/10 09:09
最终效果:
1,点击不同的button显示不同的popupwindow。
2,当点击其中任意一个button时,如果当前有另外一个popupwindow在显示,那么,会先隐藏已经开启的popupwindow,再显示当前点击按钮对应的popupwindow。

(网上搜了好久没能实现我想要的以上效果,所以自己动手写了。不算什么好的解决方式,但是想要的功能确实实现了。有喜欢的可以看看。)

先上布局文件:

MainActivity的布局文件activity_main.xml(很简单,就三个button)

<?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="horizontal"    tools:context="com.example.a860617010.testpop.MainActivity">    <Button        android:id="@+id/btn1"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="按钮1" />    <Button        android:id="@+id/btn2"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="按钮2" />    <Button        android:id="@+id/btn3"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="按钮3" /></LinearLayout>
popupwindow的布局文件popuplayout.xml

也很简单,里面内容就是一个listview的展示

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:background="#fff"    android:layout_height="wrap_content"    android:orientation="vertical">    <ListView        android:id="@+id/list_view"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></LinearLayout>

listview中条目的布局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="match_parent"    android:orientation="vertical">    <TextView        android:id="@+id/tv_show"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal" /></LinearLayout>

listview中展示的数据是一些假数据,主要用来展示用(可直接复制过去,不是重要内容):

public class GetData {    public static final String TYPE_ONE = "type_one";    public static final String TYPE_TWE = "type_twe";    public static final String TYPE_THREE = "type_three";    public List<String> GetData(String type) {        List<String> data = new ArrayList<>();        switch (type) {            case TYPE_ONE:                data = loadTypeOne();                break;            case TYPE_TWE:                data=loadTypeTwe();                break;            case TYPE_THREE:                data=loadTypeThree();                break;        }        return data;    }    private List<String> loadTypeOne() {        List<String> data = new ArrayList<>();        for (int i = 0; i < 5; i++) {            data.add("第"+i+"条数据");        }        return data;    }    private List<String> loadTypeTwe() {        List<String> data = new ArrayList<>();        for (int i = 0; i < 50; i++) {            data.add("第"+i+"条数据");        }        return data;    }    private List<String> loadTypeThree() {        List<String> data = new ArrayList<>();        for (int i = 0; i < 14; i++) {            data.add("第"+i+"条数据");        }        return data;    }}
已接近尾声:

最主要的也是这里了。MainAcitivity。先上代码再解释。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private Button btn1;    private Button btn2;    private Button btn3;    private PopupWindow popupWindow;    private GetData getData;    private List<String> data;    private int popType;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        btn1 = (Button) findViewById(R.id.btn1);        btn2 = (Button) findViewById(R.id.btn2);        btn3 = (Button) findViewById(R.id.btn3);        btn1.setOnClickListener(this);        btn2.setOnClickListener(this);        btn3.setOnClickListener(this);        getData = new GetData();    }    @Override    public void onClick(View v) {        int id = v.getId();        switch (id) {            case R.id.btn1:                data = getData.GetData(GetData.TYPE_ONE);                if (popupWindow != null && popType == 1) {                    popupWindow = null;                } else {                    showPopupWindow(data);                    popType = 1;                }                break;            case R.id.btn2:                data = getData.GetData(GetData.TYPE_TWE);                if (popupWindow != null && popType == 2) {                    popupWindow = null;                } else {                    showPopupWindow(data);                    popType = 2;                }                break;            case R.id.btn3:                data = getData.GetData(GetData.TYPE_THREE);                if (popupWindow != null && popType == 3) {                    popupWindow = null;                } else {                    showPopupWindow(data);                    popType = 3;                }                break;        }    }    private void showPopupWindow(List<String> data) {        //设置contentView        View contentView = LayoutInflater.from(this).inflate(R.layout.popuplayout, null);        popupWindow = new PopupWindow(contentView, WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, true);        popupWindow.setContentView(contentView);        ListView listView = (ListView) contentView.findViewById(R.id.list_view);        listView.setAdapter(new MyAdapter(data));        // 需要设置一下此参数,点击外边可消失        popupWindow.setBackgroundDrawable(new BitmapDrawable());        // 设置点击窗口外边窗口消失        popupWindow.setOutsideTouchable(true);        //PopupWindow是否具有获取焦点的能力,如果你不这样设置的话,那么popupwindow之外的控件是无法响应事件的。        popupWindow.setFocusable(false);        //设置popupwindow显示在btn1下方        popupWindow.showAsDropDown(btn1);    }    //popupwindow中显示的是listview,这是listview的adapter    private class MyAdapter extends BaseAdapter {        private List<String> list;        public MyAdapter(List<String> list) {            this.list = list;        }        @Override        public int getCount() {            return list.size();        }        @Override        public Object getItem(int position) {            return list.get(position);        }        @Override        public long getItemId(int position) {            return position;        }        @Override        public View getView(final int position, View convertView, ViewGroup parent) {            ViewHolder viewHolder = null;            if (convertView == null) {                viewHolder = new ViewHolder();                convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);                viewHolder.textView = (TextView) convertView.findViewById(R.id.tv_show);                convertView.setTag(viewHolder);            } else {                viewHolder = (ViewHolder) convertView.getTag();            }            viewHolder.textView.setText(list.get(position));            convertView.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View v) {                    Toast.makeText(MainActivity.this, "点击了" + position + "位置的数据", Toast.LENGTH_SHORT).show();                    popupWindow.dismiss();                }            });            return convertView;        }    }    private class ViewHolder {        TextView textView;    }}

代码一开始就是找控件,设定点击事件。这个没什么好说的。再有就是一个listview的adapter。

showPopupWindow(List<String> data)方法。无疑,调用这个方法就会显示一个PopupWindow,内部属性方法我也做了简单的说明。不明白的可以学学pop的基础用法。


重点说逻辑。

      首先我先定义一个int类型的标记,就是MainActivity成员变量里面的,

  private int popType;
    最主要的就是这个逻辑,
if (popupWindow != null && popType == 1) {                    popupWindow = null;                } else {                    showPopupWindow(data);                    popType = 1;                }
    点击按钮后先判断popupWindow是否为空,如果不为空,再判断当前的popupwindow是不是当前点击的按钮对应的popupwindow.如果是,那就关闭这个popupwindow,如果不是,走else,显示点击按钮对应的popupwindow.并且给这个popupwindow一个标记.用于下一次点击按钮时的标记判断.

    如果看不太懂的话可以全部粘贴复制.跑一遍看看.我把所有的代码都贴上了.应该不难.谢谢!



 

目录结构看图片。一共就5个文件。5个文件中的所有代码我已列出。直接粘贴复制可以跑跑看。











阅读全文
1 0
原创粉丝点击