PopupWindow+GridView设置网格边框不能滑动,Spinner效果

如图PopupWindow+GridView设置网格边框不能滑动,Spinner效果

 <net.xy.demo.widget.NoScrollGridView
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="12dp"
        android:listSelector="#00000000"//选中颜色
        android:background="@color/main_top_line"//背景颜色
        android:scrollbars="none"
        android:horizontalSpacing="1sp"//item间距
        android:verticalSpacing="1sp"
        android:padding="1sp"
        android:numColumns="3"/>
public class NoScrollGridView extends GridView {
    public NoScrollGridView(Context context) {
        super(context);
    }

    public NoScrollGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NoScrollGridView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    /**
     * 设置上下不滚动
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if(ev.getAction() == MotionEvent.ACTION_MOVE){
            return true;//true:禁止滚动
        }
        return super.dispatchTouchEvent(ev);
    }
}

PopupWindow

 public static void showSelectGradePopUp(final Activity activity , View showView, int width,int height, BaseAdapter adapter, AdapterView.OnItemClickListener onItemClickListener) {
        if(FastClick.isFastClick()){
            return;
        }
        dissmiss();
        LayoutInflater layoutInflater = activity.getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.popup_select_grade_layout,null);
        NoScrollGridView gridview = view.findViewById(R.id.gridview);
        gridview.setAdapter(adapter);
        gridview.setOnItemClickListener(onItemClickListener);
        //固定item高度40dp
        popupWindow = new PopupWindow(view,width,height);
        //指定透明背景,back键相关
        popupWindow.setBackgroundDrawable(new ColorDrawable());
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(false);
        //无需动画
        popupWindow.setAnimationStyle(R.style.PopupAnimaFade);
        int[] location = new int[2];
        showView.getLocationOnScreen(location);
//        popupWindow.showAtLocation(showView, Gravity.NO_GRAVITY, location[0], location[1]-popupWindow.getHeight());
//        //在控件的下方
        popupWindow.showAsDropDown(showView);
//        //在控件的左边
//        popupWindow.showAtLocation(showView, Gravity.NO_GRAVITY, location[0]-popupWindow.getWidth(), location[1]);
//        //在控件的右边
//        popupWindow.showAtLocation(showView, Gravity.NO_GRAVITY, location[0]+showView.getWidth(), location[1]);
    }


猜你喜欢

转载自blog.csdn.net/xiaoyi848699/article/details/79886284