我正在尝试更改 swipeRefresh 进度的位置并使其位于移动屏幕的中心,但它始终出现在顶部,
我尝试使用此代码将 swipeRefresh 放在中心,但它不起作用
android:layout_centerVertical="true"
这是我的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg">
<ProgressBar
android:id="@+id/awv_progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="ss"
android:layout_marginTop="150dp"/>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</android.support.v4.widget.SwipeRefreshLayout>
<com.google.android.gms.ads.AdView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-2615894909216466/9780888430">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
主要活动java代码:
公共类 my_activity 扩展 Activity {
WebView webView;
SwipeRefreshLayout swipe;
ProgressBar progressBar;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
swipe = findViewById(R.id.swipe);
swipe.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
swipe.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
swipe.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
Rect rect = new Rect();
swipe.getDrawingRect(rect);
swipe.setProgressViewOffset(false, 0, rect.centerY() - (swipe.getProgressCircleDiameter() / 2));
}
});
AdView mAdView = (AdView)findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
progressBar = (ProgressBar) findViewById(R.id.awv_progressBar);
LoadWeb();
progressBar.setMax(100);
progressBar.setProgress(1);
swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webView.reload();
}
});
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/error.html");
}
public void onLoadResource(WebView view, String url) { //Doesn't work
//swipe.setRefreshing(true);
}
public void onPageFinished(WebView view, String url) {
//Hide the SwipeReefreshLayout
progressBar.setVisibility(View.GONE);
swipe.setRefreshing(false);
}
});
}
public void LoadWeb() {
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.loadUrl("http://promotions.panda.com.sa/flipbook/Zone3/");
swipe.setRefreshing(true);
}
@Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
finish();
}
}}
使用 setProgressViewOffset 更改刷新指示器的位置。
在 SwipeRefreshLayout 中心显示刷新指示器。
final SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swipe);
swipeRefreshLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
swipeRefreshLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
swipeRefreshLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
Rect rect = new Rect();
swipeRefreshLayout.getDrawingRect(rect);
swipeRefreshLayout.setProgressViewOffset(false, 0, rect.centerY() - (swipeRefreshLayout.getProgressCircleDiameter() / 2));
}
});
使用
setProgressViewOffset
中的 SwipeRefreshLayout
。它基本上使您能够决定垂直开始和结束动画的位置。
我意识到,当我们从 SwipeRefreshView 设置矩形时,属性未初始化。所以,为了解决这个问题,我们可以从windowManager rect中获取设备的高度,这就是Activity的一个公共乐趣。
private companion object {
private const val SCREEN_FACTOR = 2
}
Java ->
public Int fun center(){
return getWindowManager().getMaximumWindowMetrics().getBounds.bottom/SCREEN_FACTOR
}
科特林->
private val center
get() = windowManager.maximumWindowMetrics.bounds.bottom/SCREEN_FACTOR
您不需要创建新的 Rect 对象。更有效的方法是直接从已创建的进度圆中检索直径:
int circleDiameter = binding.swipeRefresh.getProgressCircleDiameter();
此方法根据 SwipeRefreshLayout 的实际尺寸动态计算垂直偏移,确保无论屏幕尺寸或密度如何,进度指示器都能准确居中。
Java:
// Listens for when the layout has been completely laid out on the screen.
binding.swipeRefresh.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
binding.swipeRefresh.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int circleDiameter = binding.swipeRefresh.getProgressCircleDiameter();
int circleOffset = (binding.swipeRefresh.getHeight() - circleDiameter) / 2;
// Sets the progress indicator to be vertically centered based on the calculated circleOffset.
binding.swipeRefresh.setProgressViewOffset(false, 0, circleOffset);
}
});
科特林:
binding.swipeRefresh.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
binding.swipeRefresh.viewTreeObserver.removeOnGlobalLayoutListener(this)
val circleDiameter = binding.swipeRefresh.progressCircleDiameter
val circleOffset = (binding.swipeRefresh.height - circleDiameter) / 2
binding.swipeRefresh.setProgressViewOffset(false, 0, circleOffset)
}
})
进度条常用的3个位置:
中心:
int circleOffsetCenter = (binding.swipeRefresh.getHeight() - circleDiameter) / 2;
顶部:
int circleOffsetTop = binding.swipeRefresh.getHeight() / 3 - circleDiameter / 2;
下:
int circleOffsetBottom = binding.swipeRefresh.getHeight() * 2 / 3 - circleDiameter / 2;
享受生活!