Strange glitches when using "Pull to refresh" - android

When I launch my activity i refresh my list with
swipeContainer.setRefreshing(true);
after refreshing is call
swipeContainer.setRefreshing(false);
But during the networkcalls The refreshIcon glitches, in a fraction of a second
and If I look to the Android Monitor I get these lines
D/OpenGLRenderer: endAllStagingAnimators on 0x7f926f5800 (RippleDrawable) with handle 0x7f8dcf50e0
D/OpenGLRenderer: endAllStagingAnimators on 0x7f7ce78000 (ListView) with handle 0x7f89455380
What does this mean? and how can I fix this that I don't get the glitches and messages?
it is realy annoying
EDIT 1
This is how I call my setRefreshing(true)
swipeContainer.post(new Runnable() {
#Override
public void run() {
swipeContainer.setRefreshing(true);
}
});

I use a swipeRefreshLayout, this might be usefull for you:
My XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/background2"
android:layout_marginTop="56dp"
android:weightSum="1">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#+id/lv1"
android:layout_marginTop="15dp"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="#f0f0f0"
android:alpha="0.90"
android:layout_weight="1.01" />
</android.support.v4.widget.SwipeRefreshLayout>
This is the code I use to refresh the activity:
SwipeRefreshLayout mSwipeRefreshLayout;
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipelayout);
mSwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
finish();
startActivity(getIntent());
}
}
);

Related

android SwipeRefreshLayout doesn't allow WebView scrolling

Am trying to implement android SwipeRefreshLayout in my WebView app. When I swipe down it refreshes the page but it doesn't allow to scroll up and down anymore except the swipe.
I have tried some basic fix I found online but it still not working.
private WebView mywebview, handshakemywebview;
private SwipeRefreshLayout mySwipeRefreshLayout;
private ViewTreeObserver.OnScrollChangedListener mOnScrollChangedListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebview = findViewById(R.id.MainWebInterface);
handshakemywebview = findViewById(R.id.HandShakeWebInterface);
mySwipeRefreshLayout = this.findViewById(R.id.swipeContainer);
mywebview.loadUrl("https://example.com");
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
shopwebview.reload();
}
}
);
}
#Override
protected void onRestart(){
super.onRestart();
mySwipeRefreshLayout.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
if (mywebview.getScrollY() == 0) {
mySwipeRefreshLayout.setEnabled(true);
} else {
mySwipeRefreshLayout.setEnabled(false);
}
}
});
}
#Override
protected void onPause(){
super.onPause();
mySwipeRefreshLayout.getViewTreeObserver().removeOnScrollChangedListener(mOnScrollChangedListener);
}
Layout XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
tools:context=".MainActivity">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#color/colorPrimary"
android:contentDescription="#string/activityLabel"
android:visibility="visible"
android:id="#+id/MainWebInterface"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<WebView
android:id="#+id/HandShakeWebInterface"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="#color/colorPrimary"
android:visibility="gone" />
</RelativeLayout>
Add NestedScrollView in swiperefreshlayout. You could wrap webview in Swipe refesh layout like this
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/MainWebInterface"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#color/colorPrimary"
android:contentDescription="#string/activityLabel"
android:visibility="visible" />
</androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
Write OnRestart code in onResume.
Its because you are Adding addOnScrollChangedListener on Restart and onRestart is not called Always

Update when pulling down the screen | ANDROID

Does anyone know how to code it to update the information from the Internet in my app with pulling down the screen?
Like this
Thanks for answering!
Here is example implemantation :
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/container_items"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="match_parent"
android:background="#color/white"
>
<ListView
android:id="#+id/list_recent"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</android.support.v4.widget.SwipeRefreshLayout>
Then in your class on create :
mItemsContainer = (SwipeRefreshLayout) findViewById(R.id.container_items);
mItemsContainer.setOnRefreshListener(this);
then some where in class :
#Override
public void onRefresh() {
// after refresh code
}
Step 1: paste the code in your layout file
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:smoothScrollbar="true"
android:id="#+id/mListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</android.support.v4.widget.SwipeRefreshLayout>
Step 2: Open your java file and add these lines
public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener
private SwipeRefreshLayout swipeRefreshLayout;
public void onCreate(Bundle savedStateInstances){
....
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
swipeRefreshLayout.setOnRefreshListener(this);
}
#Override
public void onRefresh() {
//implement the logic you want to do when the pull down happen.
//after completion of your logic add this line.
swipeRefreshLayout.setRefreshing(false);
}
What you need is Pull to Refresh. There are examples around. Check this out.
Use SwipeRefreshLayout like as
step1: make xml file
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="#string/hello_world"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"/>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
step2: in your activity do this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
}
#Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
#Override public void run() {
swipeLayout.setRefreshing(false);
}
}, 5000);
}

NativeAdsExpress forces RecyclerView to Scroll to have the NativeAd fully visible when first loaded

I have a weird "problem" or may be it's a "feature" and I just don't know, whenever a NativeAdExpress is loaded in my RecyclerView if only part of the NativeAd is visible, it forces the RecyclerView to scroll until the Native ad becomes fully visible, this behavior causes the list to keep jumping as I scroll.
My Layout is mainly:
Activity > AppBar with Tabs and ViewPager > each Page in the Pager contains PullToRefresh and inside it there's a RecyclerView,
The RecyclerView has 2 types of items (Article and NativeAdExpress).
UPDATE: My guess on why this is happening is mainly because native ads express render in a webview, and this webview receives focus then this causes the RecyclerView to scroll to it, but that's only a Guess
UPDATE 2: Apparently this is an issue in Support Lib. 24.0.0, that's the cost of being too up2date :(
Heres's my Full XML/Layouts
<?xml version="1.0" encoding="utf-8"?>
<my.package.custom.views.CustomDrawerLayout
android:id="#+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
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:fitsSystemWindows="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:openDrawer="end">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
style="#style/AlertDialog.AppCompat.Light"
fontPath="fonts/fonts/DroidKufi-Regular.ttf"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true"
app:elevation="5px"
app:headerLayout="#layout/nav_header"
app:itemIconTint="#color/colorPrimary"
app:itemTextColor="#color/contentColor"
app:actionLayout="#layout/nav_item_layout"
app:menu="#menu/drawer_menu"
app:theme="#style/NavDrawerStyle"
tools:openDrawer="end"
/>
</my.package.custom.views.CustomDrawerLayout>
where "app_bar_main.xml" is as follows:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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:fitsSystemWindows="true"
tools:context=".ui.activities.ArticlesListActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="52dp"
android:background="?attr/colorPrimary"
android:gravity="end"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingEnd="14dp"
android:paddingStart="14dp">
<android.support.v7.widget.AppCompatImageButton
android:id="#+id/ivCustomDrawable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#color/transparent"
android:tint="#color/white"
/>
<TextView
android:id="#+id/view_title"
android:visibility="gone"
style="#style/SectionTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/sources_spinner"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:transitionName="#string/transition_splash_logo"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
tools:targetApi="lollipop"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
style="#style/TabsStyle"
android:layout_width="match_parent"
android:layout_height="42dp"
android:layout_gravity="bottom"
android:layout_marginTop="0dp"
android:transitionGroup="true"
app:tabContentStart="0dp"
app:tabGravity="fill"
app:tabIndicatorColor="#color/white"
app:tabIndicatorHeight="3dp"
app:tabMode="scrollable"
app:tabPaddingBottom="0dp"
app:tabPaddingTop="0dp"
app:tabTextAppearance="#style/TextAppearance.RegularTextFont"
/>
</android.support.design.widget.AppBarLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".ui.activities.ArticlesListActivity"
tools:showIn="#layout/activity_newsitem_list">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
and finally I have 2 View Types Item and NativeAdExpress:
The NativeAdExpress ViewHolder is as follows:
public class NativeExpressAdViewHolder extends BaseCardAdViewHolder {
private final NativeExpressAdView view;
private boolean loaded = false;
private AdListener adListener;
private WeakReference<Context> context;
public NativeExpressAdViewHolder(View itemView, String adId, Context context) {
super(itemView);
view = new NativeExpressAdView(context);
view.setAdUnitId(adId);
((LinearLayout) itemView.findViewById(R.id.express_ad_holder)).addView(view);
this.context = new WeakReference<>(context);
}
public void loadAd(float cardWidthInDips) {
if (!loaded && null != context.get() && !view.isLoading()) {
int width = cardWidthInDips > 0 ? (int) cardWidthInDips : 330;
if (view.getAdSize() == null) {
view.setAdSize(new AdSize(width, 330));
view.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
super.onAdLoaded();
loaded = true;
if (adListener != null) {
adListener.onAdLoaded();
}
}
#Override
public void onAdFailedToLoad(int i) {
super.onAdFailedToLoad(i);
if (adListener != null) {
adListener.onAdFailedToLoad(i);
}
}
#Override
public void onAdOpened() {
super.onAdOpened();
if (adListener != null) {
adListener.onAdOpened();
}
}
});
}
new Handler(context.get().getMainLooper()).post(new Runnable() {
#Override
public void run() {
view.loadAd(new AdRequest.Builder().build());
}
});
}
}
public NativeExpressAdView getView() {
return view;
}
public void setAdListener(AdListener adListener) {
this.adListener = adListener;
}
#Override
public void destroyAd() {
if (view != null) {
view.destroy();
loaded = false;
}
}
}
and ads are create using a custom adapter as follows:
private BaseCardViewHolder createNativeExpressAdViewHolder(ViewGroup parent) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ad_express, parent, false);
final NativeExpressAdViewHolder viewHolder = new NativeExpressAdViewHolder(
view,
adManager.getNativeExpressAdId(),
context.get()
);
adViewHolders.add(viewHolder);
viewHolder.setAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
Log.i("ADS", "onAdFailedToLoad " + errorCode);
}
#Override
public void onAdLoaded() {
super.onAdLoaded();
// Do something
}
});
viewHolder.loadAd(cardWidthInDips);
return viewHolder;
}
I did not understand where you actually implement a RecyclerView in your code but here is anyway maybe something that could work in your case.
I had a similar problem with a GridView always getting the focus during a Fragment first load, it was also automatically scrolling down straight to the GridView.
After a lot of tests, I finally stopped this behavior with this one line on the parent layout of the GridView:
android:descendantFocusability="blocksDescendants"
This line basically means that all descendants from the parent layout will not get focus anymore. You can try it out on your RecyclerView parent layout.
I don't know if it's an issue, but the it looks like RecyclerView changed its behavior somewhere from support library 23.4.0 to 24.2.0.
Now instead of defaulting to descendantFocusability = beforeDescendants, it defaults to afterDescendants, which causes it to offer the focus to their children instead of taking it for itself.
When a child takes the focus, it ends up scrolling to make it visible.
I fixed it by adding android:descendantFocusability="beforeDescendants" to the RecyclerView, to recover the previous behavior, where it keeps the focus itself.
I had a similar issue with RecyclerView scrolling, not specifically to NativeAdExpress...but there seems to be an issue with the Android Support library 24.0.0 related to this. The android:descendantFocusability="blocksDescendants" didn't work for me. But downgrading to Android Support library 23.4.0 made it start working again.

Swipe Refresh Layout onRefresh working but not displaying refresh indicator

I've implemented a swipe refresh layout onto one of my List Views however the view is not sliding down when I swipe and the icon is not visible. Even though it seems it doesn't work, it does actually fire off the OnRefresh listener.
Also I think its worth mentioning that I'm using the ListView on a fragment so i'm initializing the listener in the fragment activity which is displayed below.
Swipe Refresh XML
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:layout_gravity="center_horizontal"
android:dividerHeight="1sp"
android:translationZ="5dp"
android:background="#color/background_gray"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:clickable="true"
/>
</android.support.v4.widget.SwipeRefreshLayout>
Fragment Activity
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) getActivity().findViewById(R.id.swipe_refresh_layout);
ListView lView = (ListView) getActivity().findViewById(R.id.listView);
mSwipeRefreshLayout.setColorScheme(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
mSwipeRefreshLayout.setRefreshing(true);
((MainActivity) getActivity()).refresh();
new Handler().postDelayed(new Runnable() {
#Override public void run() {
mSwipeRefreshLayout.setRefreshing(false);
}
}, 5000);
}
});
lView.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView absListView, int i) {
}
#Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem == 0)
mSwipeRefreshLayout.setEnabled(true);
else
mSwipeRefreshLayout.setEnabled(false);
}
});
}
I solved it, it appears that this is a current bug with the SwipeRefreshLayout. It will only work when Listview is wrapped inside a FrameLayout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:layout_gravity="center_horizontal"
android:dividerHeight="1sp"
android:translationZ="5dp"
android:background="#color/background_gray"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:clickable="true"
/>
</FrameLayout>
</android.support.v4.widget.SwipeRefreshLayout>
Simply set the property [android:clickable="true"] for the Immediate clild layout of SwipeRefreshLayout which occupies complete screen area.i.e. whose width and height is match_parent.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.support.v4.widget.SwipeRefreshLayout
android:id="#+id/srlayout_Container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true">
<RelativeLayout
android:id="#+id/rl_MainContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true">
<!-- Placeholder for Other View or ViewGroup as Clild -->
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>

SwipeRefreshLayout Tab layout. Webview can't scroll up

Okay so I have a Tab view going inside of which I have a webview, a listview and a few other pages. I want to be able to do a SwipeRefreshLayout to refresh each item. I have this working on each page. But, when I scroll down in a webview I can't scroll back up. It triggers the refresh and rebuilds it. I'd like to be able to scroll up in my webview.
Layout stuff
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="20px"
android:layout_weight="1"
android:nestedScrollingEnabled="true">
<RelativeLayout 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"
tools:context=".Splash$PlaceholderFragment">
<TextView
android:id="#+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:overScrollMode="ifContentScrolls"
android:nestedScrollingEnabled="false"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_centerVertical="false"
android:layout_centerHorizontal="true"
android:visibility="visible" />
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>
The SwipeRefreshLayout is being used inside the fragment for my tabbed layout.
swipeLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_container);
swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
swipeLayout.setOnRefreshListener( new SwipeRefreshLayout.OnRefreshListener() {
#Override public void onRefresh() {
if(getArguments().getInt(ARG_SECTION_NUMBER) == 4 ) {
stringArrayList.clear();
new updatetheQueue().execute(ctx);
}
else {
swipeLayout.setRefreshing(false);
}
}});
You can use an OnScrollChangedListener to disable SwipeRefreshLayout when the WebView is not scrolled to the top.
// Only enable swipe to refresh if the `WebView` is scrolled to the top.
webView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
if (webView.getScrollY() == 0) {
swipeRefreshLayout.setEnabled(true);
} else {
swipeRefreshLayout.setEnabled(false);
}
}
});
Getting this working was very challenging. However there is a fairly simple modification that can be made to the layout XML to work around this. It means I can't update the webviews on swipe but thats okay.
<RelativeLayout 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"
tools:context=".Splash$PlaceholderFragment">
<TextView
android:id="#+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:overScrollMode="ifContentScrolls" />
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout 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"
tools:context=".Splash$PlaceholderFragment">
<ListView
android:id="#+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="false"
android:visibility="visible" />
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>
you can make a class extend SwipeRefreshLayout and Override canChildScrollUp to sit scro
#Override
public boolean canChildScrollUp() {
// TODO Auto-generated method stub
return true; // if you want it to scroll up
// check if child Get To Top
//return false // if you want it to execute swipe down to refresh listener
}
If you are using the ViewPager and the tabs are Fragments, you can fix your WebViewFragment with this snippet:
private ViewTreeObserver.OnScrollChangedListener mOnScrollChangedListener;
#Override
public void onStart() {
super.onStart();
swipeLayout.getViewTreeObserver().addOnScrollChangedListener(mOnScrollChangedListener =
new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
if (mWebView.getScrollY() == 0)
swipeLayout.setEnabled(true);
else
swipeLayout.setEnabled(false);
}
});
}
#Override
public void onStop() {
swipeLayout.getViewTreeObserver().removeOnScrollChangedListener(mOnScrollChangedListener);
super.onStop();
}
More info in Android - SwipeRefreshLayout with empty textview.
this is how i solve the problem, hope it will help :
mWebView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
if(mWebView.getScrollY() == 0){
mSwipeRefreshLayout.setEnabled(true);
}else{
mSwipeRefreshLayout.setEnabled(false);
}
}
});
In kotlin:
webView.viewTreeObserver.addOnScrollChangedListener {
binding.swipeRefreshLayout.isEnabled = webView.scrollY == 0
}

Categories

Resources