Android中自定义popuWindow

    xiaoxiao2021-12-14  24

    效果图:

    核心代码: 1定义MyPopShow 

    public class MyPopShow { public PopupWindow show(Context context, View view, View v) { PopupWindow pop = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT); pop.setFocusable(true); ColorDrawable dw = new ColorDrawable(0); pop.setBackgroundDrawable(dw); pop.showAsDropDown(v); return pop; } } 2绑定popuWindow数据

    // 绑定泡泡窗口中的数据 popuServiceaddressView = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_popu_serviceaddress, null); lvServiceAddress = (ListView) popuServiceaddressView.findViewById(R.id.lvServiceAddress); list=new ArrayList<>(); list.add("刘备"); list.add("关羽"); list.add("张飞"); list.add("马超"); list.add("黄忠"); myExpertAdapter=new MyExpertAdapter(list,MainActivity.this); lvServiceAddress.setAdapter(myExpertAdapter); 3popuWindow的布局文件activity_popu_serviceaddress.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" android:id="@+id/ll_totleLayout" > <ListView android:layout_marginTop="0.5dp" android:id="@+id/lvServiceAddress" android:background="@android:color/white" android:layout_width="match_parent" android:divider="@null" android:layout_height="wrap_content"> </ListView> <View android:layout_width="match_parent" android:layout_height="match_parent" android:background="#9D777777"> </View> </LinearLayout> 4点击弹出popuWindow

    private void myOnClick() { rlServiceAddress.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mySelected(0); if (popupWindowServiceAddress != null) { popupWindowServiceAddress.showAsDropDown(v); } else { popupWindowServiceAddress = new MyPopShow().show(MainActivity.this, popuServiceaddressView, v); popupWindowServiceAddress.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { // 监听popu窗口消失 mySelected(4); } }); } } }); 5颜色的变化

    private void mySelected(int s) { for (int i = 0; i < textArray.length; i++) { if (i == s) { textArray[i].setTextColor(Color.parseColor("#0078d7")); imgArray[i].setImageResource(R.mipmap.xlico); } else { textArray[i].setTextColor(Color.parseColor("#666666")); imgArray[i].setImageResource(R.mipmap.xlico_off); } if (s == 4) {//4恢复默认设置 textArray[i].setTextColor(Color.parseColor("#666666")); imgArray[i].setImageResource(R.mipmap.xlico_off); } } } 6里面列表的点击事件

    // 泡泡窗口中条目点击事件 lvServiceAddress.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { myExpertAdapter.setDefSelect(position); // 选中显示的控件 tvServiceAddress.setText(list.get(position)); // 选中文字 if (popupWindowServiceAddress.isShowing()) { popupWindowServiceAddress.dismiss(); } } }); 源码下载: MyApplication02----mypopu http://download.csdn.net/detail/zhaihaohao1/9700744

    转载请注明原文地址: https://ju.6miu.com/read-969430.html

    最新回复(0)