更改滑动位置刷新进度以居中

时间:2018-10-30 12:10:12

标签: android progress-bar

我正在尝试更改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扩展了活动{

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();
    }
}}

3 个答案:

答案 0 :(得分:0)

使用setProgressViewOffset更改刷新指示器的位置。

要在SwipeRefreshLayout的中心显示刷新指示器。

ResourceLoader

答案 1 :(得分:0)

使用setProgressViewOffset中的SwipeRefreshLayout。基本上,它使您能够确定垂直开始和结束动画的位置。

答案 2 :(得分:0)

我意识到当我们从 SwipeRefreshView 设置 rect 时,属性没有被初始化。所以,为了解决这个问题,我们可以从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