gpt4 book ai didi

java - java实现下拉刷新的方法

转载 作者:行者123 更新时间:2023-11-30 00:46:27 24 4
gpt4 key购买 nike

我要为我的 webview 做一个下拉刷新选项,我得到了 xml 代码和它的工作,但我不知道如何做通过使用 java 正常工作,我正在使用应用程序抽屉 Activity ,这是我的第一个 fragment (代码中的 home_fragment)

这是xml代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.fb.jaisonjoseph.facebookbasic.Home_Fragment">

<!-- TODO: Update blank fragment layout -->

<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">

<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/webView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true" />

</android.support.v4.widget.SwipeRefreshLayout>



<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:animationResolution="@integer/abc_max_action_buttons"
android:clickable="false"
android:theme="@style/Base.Theme.AppCompat"
android:indeterminate="false"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

</RelativeLayout>

这是我在 Home_Fragment.java 中的 java 代码

package com.fb.jaisonjoseph.facebookbasic;


import android.content.Context;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.view.KeyEvent;

/**
* A simple {@link Fragment} subclass.
*/
public class Home_Fragment extends Fragment {
public ProgressBar bar;
SwipeRefreshLayout mySwipeRefreshLayout;
public WebView mwebView;

public Home_Fragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_home_, null);
final WebView view=(WebView) rootView.findViewById(R.id.webView);
view.loadUrl("https://mbasic.facebook.com");
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new MyWebViewClient());
view.getSettings().setBuiltInZoomControls(true);
view.getSettings().setDisplayZoomControls(false);
bar=(ProgressBar)rootView.findViewById(R.id.progressBar);
return rootView;
}

private class MyWebViewClient extends WebViewClient {


@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}

@Override
public void onPageStarted(final WebView view, final String url, final Bitmap favicon) {
bar.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}

@Override
public void onPageFinished(WebView view, String url) {
bar.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
}
}

最佳答案

首先在 .Java 文件中声明 swiperefresh。

        SwipeRefreshLayout swiperefresh=(SwipeRefreshLayout) findViewById(R.id.swiperefresh); 

那么就需要用到setOnRefreshListener,这样每当布局向下滑动,setOnRefreshListener就会被调用

        swiperefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
// code to refresh

swiperefresh.setRefreshing(false); //code to stop refresh animation

}
});

关于java - java实现下拉刷新的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41715807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com