我有一个viewpager,每个页面都必须显示不同的WebView。这个WebView应该允许horizontall scrool,我想在滚动完成时滑动到下一页。
我已经搜索过,并找到了一些解决方案,但我没有解决这个问题。我有一个CustomViewPager和一个CustomWebView来尝试处理滚动事件。
这是我的代码:
自定义ViewPager
public class WebViewPager extends ViewPager {
public WebViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
if (v instanceof ExtendedWebView) {
return ((ExtendedWebView) v).canScrollHor(-dx);
} else {
return super.canScroll(v, checkV, dx, x, y);
}
}
}
自定义WebView
public class ExtendedWebView extends WebView {
public ExtendedWebView(Context context) {
super(context);
}
public ExtendedWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public boolean canScrollHor(int direction) {
final int offset = computeHorizontalScrollOffset();
final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
if (range == 0)
return false;
if (direction < 0) {
return offset > 0;
} else {
return offset < range - 1;
}
}
}
ScreenSlidePagerActivity
public class ScreenSlidePagerActivity extends FragmentActivity {
class MyPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
private List<String> titles;
public MyPageAdapter(FragmentManager fm) {
super(fm);
this.fragments = new ArrayList<Fragment>();
this.titles = new ArrayList<String>();
}
public void addItem(String url, String title) {
Fragment myFragment = new WebViewFragment();
Bundle args = new Bundle();
args.putString("url", url);
myFragment.setArguments(args);
this.fragments.add(myFragment);
this.titles.add(title);
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
public CharSequence getPageTitle(int position) {
return this.titles.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
private MyPageAdapter pageAdapter = null;
private WebViewPager pager = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pageAdapter = new MyPageAdapter(getSupportFragmentManager());
pageAdapter.addItem("url1", "WebView 1");
pageAdapter.addItem("url2", "WebView 2");
pageAdapter.addItem("url3", "WebView 3");
pager = (WebViewPager) findViewById(R.id.pager);
pager.setOffscreenPageLimit(pageAdapter.getCount() - 1);
pager.setAdapter(pageAdapter);
}
}
活动布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:context=".MainActivity" >
<com.example.viewpagerexample.WebViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="6dp"
android:paddingTop="0dp" >
<android.support.v4.view.PagerTabStrip
android:id="@+id/pager_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#fff"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:textColor="#000" />
</com.example.viewpagerexample.WebViewPager>
</LinearLayout>
更新 只有将此参数放在我的网页的CSS中时,接受的答案才有效:
display: table;
显然,如果没有此参数,WebView将无法计算其内容的宽度。
答案 0 :(得分:4)
将以下属性设置为WebView
<your.package.name.MyWebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
mWebView.setInitialScale(120);
mWebView.setScrollContainer(true);
mWebView.bringToFront();
mWebView.setScrollbarFadingEnabled(true);
mWebView.setVerticalScrollBarEnabled(true);
mWebView.setHorizontalScrollBarEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setJavaScriptEnabled(false);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setUserAgentString("Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3");
<强> 1。实现自定义ViewPager实例,如下所示:
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
public class CustomViewPager extends ViewPager {
private MagazineWebView_WithoutFlipWebView mCurrentPageWebView_; //custom webview
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (Constants.LOGGING) {
Log.v(Constants.LOG_OEAMTC_APP, "CustomViewPager - onInterceptTouchEvent");
}
// if view zoomed out (view starts at 33.12... scale level) ... allow
// zoom within webview, otherwise disallow (allow viewpager to change
// view)
if (mCurrentPageWebView_ != null && (mCurrentPageWebView_.getScale() * 100) > 34) {
Log.v(Constants.LOG_OEAMTC_APP, "CustomViewPager - intrcepted: " + String.valueOf((mCurrentPageWebView_.getScale() * > 100)));
this.requestDisallowInterceptTouchEvent(true);
}
else {
if (mCurrentPageWebView_ != null) {
Log.v(Constants.LOG_OEAMTC_APP,
"CustomViewPager - not intrcepted: " + String.valueOf(mCurrentPageWebView_.getScale() * 100));
}
this.requestDisallowInterceptTouchEvent(false);
}
return super.onInterceptTouchEvent(event);
}
public MagazineWebView_WithoutFlipWebView getCurrentPageWebView() {
return mCurrentPageWebView_;
}
public void setCurrentPageWebView(MagazineWebView_WithoutFlipWebView currentPageWebView) {
mCurrentPageWebView_ = currentPageWebView;
}
}
<强> 2。在您的主(ViewPager)活动中,将以下行添加到视图寻呼机
mViewPager_ = new AwesomePagerAdapter();
viewpapgerInLayout = (CustomViewPager) findViewById(R.id.awesomepager);
viewpapgerInLayout.setAdapter(mViewPager_);
viewpapgerInLayout.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
viewpapgerInLayout.setCurrentPageWebView(mLstPagesWebviews_.get(position));
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
第3。最后,运行它:=)如果缩放级别是初始缩放, 允许更改页面,其他时间您可以浏览Web视图
另外,您也可以扩展WebView,但不能同时扩展
@Override
public boolean onInterceptTouchEvent(MotionEvent p_event)
{
return true;
}
@Override
public boolean onTouchEvent(MotionEvent p_event)
{
if (p_event.getAction() == MotionEvent.ACTION_MOVE && getParent() != null)
{
getParent().requestDisallowInterceptTouchEvent(true);
}
return super.onTouchEvent(p_event);
}