禁用标签之间刷卡 [英] Disable swiping between tabs
问题描述
我设置了滑动卡口与两个片段
s分别片段
有一个按钮
这都到了的WebView
。与此问题是当的WebView
按钮
点击滑动片仍然被激活,当用户试图浏览在的WebView在
你最终滑动到其他选项卡。是否有在上点击方法的方法来禁用选项卡的刷卡功能?任何帮助将是巨大的AP preciated!
在这里,code:
公共类MyWebViewClass扩展片段{私人的WebView mWebView;
私人按钮mButton;公共MyWebViewClass(){
//必需的空公共构造
}
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
//充气的布局该片段
查看查看= inflater.inflate(R.layout.fragment_webview,集装箱,FALSE); mWebView =(的WebView)view.findViewById(R.id.WebView); mButton =(按钮)view.findViewById(R.id.Button1);
mButton.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
mWebView.setVisibility(View.VISIBLE);
mButton.setVisibility(View.GONE);
mWebView.getSettings()setJavaScriptEnabled(真)。
mWebView.loadUrl(www.google.com);
}
}); 返回视图。
}
这个答案可以适用于任何ViewPager其实不管是您正在使用实现无标签的标签,甚至一个正常的ViewPager库。
您使用的是 neokree / MaterialTabs 库备份与ViewPager,负责刷卡的效果您可以通过提供自己的自定义ViewPager禁用。
进口android.content.Context;
进口android.util.AttributeSet;
进口android.view.MotionEvent;/ **
*创建者hegazy于15年2月13日。
* /
公共类CustomViewPager扩展android.support.v4.view.ViewPager {
启用私人布尔; 公共CustomViewPager(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
this.enabled = TRUE;
} @覆盖
公共布尔onTouchEvent(MotionEvent事件){
返回启用? super.onTouchEvent(事件):假的;
} @覆盖
公共布尔onInterceptTouchEvent(MotionEvent事件){
返回启用? super.onInterceptTouchEvent(事件):假的;
} 公共无效setPagingEnabled(布尔启用){
this.enabled =启用;
} 公共布尔isPagingEnabled(){
返回启用;
}}
这个类提供了启用快速滑动一个ViewPager,你可以通过 mViewPager.setPagingEnabled关掉它(假);
没有提及的是,你必须在XML布局更改为新的自定义ViewPager,而不是原来的。
< android.support.v4.view.ViewPager
...
/>
到
< my.package.CustomViewPager
...
/>
I set up sliding tabs with two Fragment
s each Fragment
has a Button
which goes to a WebView
. The problem with this is when the WebView
Button
is clicked the sliding tabs are still activated and when a user tries to navigate within the WebView
you end up swiping to the other tab. Is there a way in an on click method to disable the swiping ability of the tabs? Any help would be hugely appreciated!
Here the code:
public class MyWebViewClass extends Fragment {
private WebView mWebView;
private Button mButton;
public MyWebViewClass() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_webview, container, false);
mWebView = (WebView) view.findViewById(R.id.WebView);
mButton = (Button) view.findViewById(R.id.Button1);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mWebView.setVisibility(View.VISIBLE);
mButton.setVisibility(View.GONE);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("www.google.com");
}
});
return view;
}
This answer can be applied to any ViewPager actually no matter what is the library you are using to implement the tabs or even a normal ViewPager without tabs.
The library you are using neokree/MaterialTabs is backed with a ViewPager that is responsible for the swiping effect and you can disable that by providing your own custom ViewPager.
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
/**
* Created by hegazy on 2/13/15.
*/
public class CustomViewPager extends android.support.v4.view.ViewPager{
private boolean enabled;
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.enabled = true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return enabled ? super.onTouchEvent(event) : false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return enabled ? super.onInterceptTouchEvent(event) : false;
}
public void setPagingEnabled(boolean enabled) {
this.enabled = enabled;
}
public boolean isPagingEnabled() {
return enabled;
}
}
This class provides a ViewPager that is swiping enabled and you can turn it off by mViewPager.setPagingEnabled(false);
No to mention that you have to change the xml layout to your new custom ViewPager rather than the original one.
<android.support.v4.view.ViewPager
...
/>
to
<my.package.CustomViewPager
...
/>
这篇关于禁用标签之间刷卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!