您好,我的應用程序中有一個Fragment
,其中包含ViewPager
。我有一個String
變量代碼。如果code
等於「NBG」,我想要禁用刷卡的ViewPager
。無法禁用viewpager以編程方式刷卡
public class CurrencyFragment extends BaseFragment {
@Override
public View onCreateView(final LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_currency_layout, container, false);
mViewpager = (ViewPager) view.findViewById(R.id.Detailpager);
Bundle bundle = getArguments();
if (bundle != null) {
code = object.getCode();
}
if (code.equals("NBG")) {
CustomViewPager pager = new CustomViewPager(getActivity(), null);
pager.setPagingEnabled(false);
}
return view;
};
public class CustomViewPager extends ViewPager {
private boolean enabled;
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.enabled = true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onTouchEvent(event);
}
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onInterceptTouchEvent(event);
}
return false;
}
public void setPagingEnabled(boolean enabled) {
this.enabled = enabled;
} }
我試過以下方法,但我無法在mViewpager上調用此方法。 – user3314123
您在調用它時需要投射'CustomViewPager',或者不是將'mViewPager'聲明爲'ViewPager',而是使用'CustomViewPager'。 – adneal
現在它的作品非常感謝:) – user3314123