使用 mWv.loadDataWithBaseURL(“about:blank”, mUrl1, “text/html”, “utf-8”, null); 进行加载
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.viewpager.widget.ViewPager;
import com.alibaba.fastjson.JSON;
import com.ruhai.yibei.Constant;
import com.ruhai.yibei.R;
import com.ruhai.yibei.adapter.HomeFragBannerAdapter;
import com.ruhai.yibei.adapter.HotServiceAdapter;
import com.ruhai.yibei.adapter.ServiceDetailAdapter;
import com.ruhai.yibei.bean.HotServiceBean;
import com.ruhai.yibei.bean.ServiceDetailBean;
import com.zhy.adapter.recyclerview.wrapper.HeaderAndFooterWrapper;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;
import java.util.Timer;
import java.util.TimerTask;
public class ServiceDetailWebActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
private ProgressBar mPb;
private WebView mWv;
private WebSettings mWebSettings;
private String mUrl = "";
private ViewPager mVpHomeFragBanner;
private int mBannerCount;
private Timer mTimer;
private boolean mVPCanPlay = true;
private RadioGroup mRgHomeFragBanner;
private TextView serviceNameTv;
private TextView serviceDescTv;
private TextView backTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_service_detail_web);
mUrl = getIntent().getStringExtra("weburl");
// https://test.winkong.pro/platformProduct/appPlatformService/service?serviceClassId=651&serviceId=1
Log.e("wy", "onCreate mUrl: " + mUrl);
mPb = (ProgressBar) findViewById(R.id.pb);
mWv = (WebView) findViewById(R.id.ship_webv);
serviceNameTv = (TextView) findViewById(R.id.service_name_tv);
serviceDescTv = (TextView) findViewById(R.id.service_desc_tv);
mRgHomeFragBanner = (RadioGroup) findViewById(R.id.rg_home_frag_banner);
mVpHomeFragBanner = (ViewPager) findViewById(R.id.vp_home_frag_banner);
backTv = (TextView) findViewById(R.id.back_tv);
backTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
// 监听用户是否接触banner,如果是则不轮播,否则轮播
mVpHomeFragBanner.addOnPageChangeListener(this);
initDatas();
}
private void initDatas() {
RequestParams params = new RequestParams(mUrl);
params.addHeader("Authorization", Constant.authorization);
params.addHeader("language", "zh_CN");
params.setAsJsonContent(true);
x.http().get(params, new Callback.CommonCallback<String>() {
@SuppressLint("ResourceAsColor")
@Override
public void onSuccess(String result) {
ServiceDetailBean professionInfo = JSON.parseObject(result, ServiceDetailBean.class);
Log.e("wy", "onSuccess: " + professionInfo.getMsg());
if (professionInfo.getCode().equals("00000000")) {
serviceNameTv.setText(professionInfo.getData().getServiceName());
// serviceDescTv.setText(professionInfo.getData().getServiceDesc());
webSettings();
webView(professionInfo.getData().getServiceDesc() + professionInfo.getData().getServiceDetail());
// webView("http://www.baidu.com");
mBannerCount = professionInfo.getData().getDetailPicUrlList().size();
//轮播图在集合里,从中取出轮播图
mVpHomeFragBanner.setAdapter(new ServiceDetailAdapter(professionInfo.getData().getDetailPicUrlList(), ServiceDetailWebActivity.this));
mVpHomeFragBanner.setCurrentItem(25000);
if (mTimer == null) {
mTimer = new Timer();
mTimer.schedule(new TimerTask() {
@Override
public void run() {
// mHandler.sendEmptyMessage(1);
runOnUiThread(new Runnable() {
@Override
public void run() {
if (mVPCanPlay) {
mVpHomeFragBanner.setCurrentItem(mVpHomeFragBanner.getCurrentItem() + 1);
}
}
});
}
}, 3000, 3000);
}
} else {
Toast.makeText(x.app(), professionInfo.getMsg(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
Log.e("wy", "result: " + ex.getMessage());
}
@Override
public void onCancelled(CancelledException cex) {
Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
}
@Override
public void onFinished() {
}
});
}
/**
* webView的相关设置
*/
private void webView(String mUrl1) {
//为了更好的支持表单
mWv.setFocusable(true);
mWv.setFocusableInTouchMode(true);
mWv.requestFocus();
mWv.setWebViewClient(new WebViewClient() {
//目的是要让我们应用自己来加载网页,而不是交给浏览器
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
mWv.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
mPb.setProgress(newProgress);
mPb.setVisibility(newProgress == 100 ? GONE : VISIBLE);
}
});
// mWv.loadUrl(mUrl1);
mWv.loadDataWithBaseURL("about:blank", mUrl1, "text/html", "utf-8", null);
}
/**
* web的相关设置
*/
private void webSettings() {
mWebSettings = mWv.getSettings();
//让webview支持js
mWebSettings.setJavaScriptEnabled(true);
//设置是否支持缩放模式
mWebSettings.setSupportZoom(true);
mWebSettings.setBuiltInZoomControls(true);
// 是否显示+ -
mWebSettings.setDisplayZoomControls(false);
mWebSettings.setDomStorageEnabled(true);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
// rudiobutton小点点和轮播图做关联,这是viewPager的监听
@Override
public void onPageSelected(int position) {
Log.e("wy", "position: " + position + " mBannerCount:" + mBannerCount);
if (mBannerCount != -1) {
int realPos = position % mBannerCount;
Log.e("wy", "onPageSelected: " + realPos);
RadioButton childAt = (RadioButton) mRgHomeFragBanner.getChildAt(realPos);
childAt.setChecked(true);
}
}
@Override
public void onPageScrollStateChanged(int state) {
// 判断轮播图是否被用户拖拽
mVPCanPlay = state == ViewPager.SCROLL_STATE_IDLE;
}
}
|