android - How can I fix this misbehavior of SwipeRefreshLayout -
i'm trying implement pull down refresh , using swiperefreshlayout. it's malfunctioning.
when pull down, swiperefreshlayout pulls down should remove hand, jumps top of first item in recyclerview. halve of loader hides behind toolbar , other half visible on top first item in recyclerview. if pull down again, doesn't work.
my layouts
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.drawerlayout 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" tools:opendrawer="start"> <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" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitssystemwindows="true" app:headerlayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer"/> </android.support.v4.widget.drawerlayout>
app_bar_main.xml
<?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="com.example.myapp.mainactivity"> <android.support.design.widget.appbarlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/apptheme.appbaroverlay"> <android.support.v7.widget.toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionbarsize" android:background="?attr/colorprimary" app:popuptheme="@style/apptheme.popupoverlay"/> </android.support.design.widget.appbarlayout> <include layout="@layout/content_main" /> <android.support.design.widget.floatingactionbutton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src="@android:drawable/ic_dialog_email"/> </android.support.design.widget.coordinatorlayout>
nav_header_main.xml
<?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="@dimen/nav_header_height" android:background="@drawable/side_nav_bar" android:gravity="bottom" android:orientation="vertical" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:theme="@style/themeoverlay.appcompat.dark"> <imageview android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingtop="@dimen/nav_header_vertical_spacing" android:src="@android:drawable/sym_def_app_icon"/> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingtop="@dimen/nav_header_vertical_spacing" android:text="android studio" android:textappearance="@style/textappearance.appcompat.body1"/> <textview android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="android.studio@android.com"/> </linearlayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.swiperefreshlayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/swipe_refrsh" xmlns:android="http://schemas.android.com/apk/res/android"> <android.support.v7.widget.recyclerview android:id="@+id/pews_recycler" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" android:scrollbarfadeduration="5000"/> </android.support.v4.widget.swiperefreshlayout>
mainactivity
public class mainactivity extends appcompatactivity implements navigationview.onnavigationitemselectedlistener { private final string tag = "mainactivity"; private alertdialog internetdialog; private static final string get_urll = "https:/johnie.com/page/json"; //creating list of pewss private list<newsitems> mnewsitemslist; //creating views private recyclerview recyclerview; private recyclerview.adapter adapter; private progressdialog mprogressdialog; swiperefreshlayout mswiperefreshlayout; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); log.d(tag, "oncreate called"); //initializing views recyclerview = (recyclerview) findviewbyid(r.id.pews_recycler); linearlayoutmanager layoutmanager = new linearlayoutmanager(this); recyclerview.setlayoutmanager(layoutmanager); mswiperefreshlayout = (swiperefreshlayout) findviewbyid(r.id.swipe_refrsh); mswiperefreshlayout.setcolorschemecolors(android.r.color.holo_green_light, android.r.color.holo_green_dark, android.r.color.holo_purple, android.r.color.holo_red_dark, android.r.color.holo_orange_dark); mswiperefreshlayout.setonrefreshlistener(new swiperefreshlayout.onrefreshlistener() { @override public void onrefresh() { refreshdata(); } }); adapter = new newsadapter(mnewsitemslist, this); recyclerview.setadapter(adapter); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { snackbar.make(view, "replace own action", snackbar.length_long) .setaction("action", null).show(); } }); drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); actionbardrawertoggle toggle = new actionbardrawertoggle( this, drawer, toolbar, r.string.navigation_drawer_open, r.string.navigation_drawer_close); drawer.setdrawerlistener(toggle); toggle.syncstate(); navigationview navigationview = (navigationview) findviewbyid(r.id.nav_view); navigationview.setnavigationitemselectedlistener(this); } private void showdialog() { internetdialog = new alertdialog.builder(mainactivity.this) ... } private void refreshdata(){ log.d(tag, "refreshdata called"); //creating json request jsonarrayrequest jsonarrayrequest = new jsonarrayrequest(get_url, new response.listener<jsonarray>() { @override public void onresponse(jsonarray response) { log.d(tag, "onresponse called"); //calling method parse json array parsedata(response); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { } }); //creating request queue requestqueue requestqueue = volley.newrequestqueue(this); //adding request queue requestqueue.add(jsonarrayrequest); } //this method data web api private void getdata(){ log.d(tag, "getdata called"); //showing progress dialog mprogressdialog = new progressdialog(mainactivity.this); mprogressdialog.setcancelable(false); mprogressdialog.setmessage(this.getresources().getstring(r.string.load_pews)); mprogressdialog.show(); //creating json request jsonarrayrequest jsonarrayrequest = new jsonarrayrequest(get_url, new response.listener<jsonarray>() { @override public void onresponse(jsonarray response) { log.d(tag, "onresponse called"); //dismissing progress dialog if (mprogressdialog != null) { mprogressdialog.hide(); } /*progressdialog.dismiss();*/ //calling method parse json array parsedata(response); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { if (mprogressdialog != null) { mprogressdialog.hide(); final alertdialog.builder sthwrongalert = new alertdialog.builder(mainactivity.this); sthwrongalert.setcancelable(false); sthwrongalert.setmessage(r.string.sth_wrongme); sthwrongalert.setpositivebutton(r.string.alert_retry, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { if (!networkcheck.isavailableandconnected(mainactivity.this)) { internetdialog.show(); } else { getdata(); } } }); sthwrongalert.setnegativebutton(r.string.alert_cancel, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { finish(); } }); sthwrongalert.show(); } } }); //creating request queue requestqueue requestqueue = volley.newrequestqueue(this); //adding request queue requestqueue.add(jsonarrayrequest); } //this method parse json data private void parsedata(jsonarray array){ log.d(tag, "parsing array"); ... mnewsitemslist.add(pewsitem); } adapter.notifyitemrangechanged(0, adapter.getitemcount()); } }
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest package="com.example.myapp" xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="android.permission.internet"/> <uses-permission android:name="android.permission.access_network_state"/> <uses-permission android:name="android.permission.write_external_storage"/> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsrtl="true" android:theme="@style/apptheme"> <activity android:name=".mainactivity" android:label="@string/app_name" android:theme="@style/apptheme.noactionbar"> <intent-filter> <action android:name="android.intent.action.main"/> <category android:name="android.intent.category.launcher"/> </intent-filter> </activity> <activity android:name=".postdetails" android:parentactivityname=".mainactivity"> </activity> </application> </manifest>
styles.xml
<resources> <!-- base application theme. --> <style name="apptheme" parent="theme.appcompat.light.darkactionbar"> <!-- customize theme here. --> <item name="colorprimary">@color/colorprimary</item> <item name="colorprimarydark">@color/colorprimarydark</item> <item name="coloraccent">@color/coloraccent</item> </style> <style name="apptheme.noactionbar"> <item name="windowactionbar">false</item> <item name="windownotitle">true</item> </style> <style name="apptheme.appbaroverlay" parent="themeoverlay.appcompat.dark.actionbar"/> <style name="apptheme.popupoverlay" parent="themeoverlay.appcompat.light"/> </resources>
styles-21
<resources>> <style name="apptheme.noactionbar"> <item name="windowactionbar">false</item> <item name="windownotitle">true</item> <item name="android:windowdrawssystembarbackgrounds">true</item> <item name="android:statusbarcolor">@android:color/transparent</item> </style> </resources>
please, what's problem , how correct it?
can try on oncreated method of mainactivity.java. placed referencing of toolbar after setcontentview.
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); log.d(tag, "oncreate called"); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); //initializing views recyclerview = (recyclerview) findviewbyid(r.id.pews_recycler); linearlayoutmanager layoutmanager = new linearlayoutmanager(this); recyclerview.setlayoutmanager(layoutmanager); mswiperefreshlayout = (swiperefreshlayout) findviewbyid(r.id.swipe_refrsh); mswiperefreshlayout.setcolorschemecolors(android.r.color.holo_green_light, android.r.color.holo_green_dark, android.r.color.holo_purple, android.r.color.holo_red_dark, android.r.color.holo_orange_dark); mswiperefreshlayout.setonrefreshlistener(new swiperefreshlayout.onrefreshlistener() { @override public void onrefresh() { refreshdata(); } }); adapter = new newsadapter(mnewsitemslist, this); recyclerview.setadapter(adapter); floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { snackbar.make(view, "replace own action", snackbar.length_long) .setaction("action", null).show(); } }); drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); actionbardrawertoggle toggle = new actionbardrawertoggle( this, drawer, toolbar, r.string.navigation_drawer_open, r.string.navigation_drawer_close); drawer.setdrawerlistener(toggle); toggle.syncstate(); navigationview navigationview = (navigationview) findviewbyid(r.id.nav_view); navigationview.setnavigationitemselectedlistener(this); }
also, content_main.xml, can try enclosing framelayout.
<?xml version="1.0" encoding="utf-8"?> <framelayout 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:showin="@layout/app_bar_main"> <android.support.v4.widget.swiperefreshlayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/swipe_refrsh"> <android.support.v7.widget.recyclerview android:id="@+id/pews_recycler" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" android:scrollbarfadeduration="5000"/> </android.support.v4.widget.swiperefreshlayout> </framelayout>
i'm out of ideas after this. unless willing try , use fragments use , works fine far.
Comments
Post a Comment