1. 程式人生 > >PopupWindow+GridView設定網格邊框不能滑動,Spinner效果

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]);
    }