java - Implementing Endless scrolling in Recycler View -
i have recycler view uses asynctask populate ui.
currently retrieves data db , displays in 1 shot,
i want retrieve 15 records in 1 go , after scroll ends want load more 15 records , on...can please help. have pasted code below:
feedactivity.java
package com.bbau.ankit.test_splash; import android.app.activity; import android.graphics.color; import android.os.asynctask; import android.os.bundle; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.util.log; import android.view.window; import com.yqritc.recyclerviewflexibledivider.horizontaldivideritemdecoration; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader; import java.net.httpurlconnection;import java.net.url; import java.util.arraylist; import java.util.list; /** * created ankit on 8/14/2015. */ public class feedlistactivity extends activity { private static final string tag = "recyclerviewexample"; private list<feeditem> feeditemlist = new arraylist<feeditem>(); private recyclerview mrecyclerview; private myrecycleradapter adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); /* allow activity show indeterminate progressbar */ requestwindowfeature(window.feature_indeterminate_progress); setcontentview(r.layout.news); /* initialize recyclerview */ mrecyclerview = (recyclerview) findviewbyid(r.id.recycler_view); mrecyclerview.additemdecoration(new horizontaldivideritemdecoration.builder(this).color(color.black).build()); mrecyclerview.setlayoutmanager(new linearlayoutmanager(this)); /*downloading data below url*/ final string url = "http://192.168.170.72/bbau_news.php?before=1&after=5"; new asynchttptask().execute(url); } public class asynchttptask extends asynctask<string, void, integer> { @override protected void onpreexecute() { setprogressbarindeterminatevisibility(true); } @override protected integer doinbackground(string... params) { inputstream inputstream = null; integer result = 0; httpurlconnection urlconnection = null; try { /* forming th java.net.url object */ url url = new url(params[0]); urlconnection = (httpurlconnection) url.openconnection(); /* request */ urlconnection.setrequestmethod("get"); int statuscode = urlconnection.getresponsecode(); /* 200 represents http ok */ if (statuscode == 200) { bufferedreader r = new bufferedreader(new inputstreamreader(urlconnection.getinputstream())); stringbuilder response = new stringbuilder(); string line; while ((line = r.readline()) != null) { response.append(line); } parseresult(response.tostring()); result = 1; // successful }else{ result = 0; //"failed fetch data!"; } } catch (exception e) { log.d(tag, e.getlocalizedmessage()); } return result; //"failed fetch data!"; } @override protected void onpostexecute(integer result) { setprogressbarindeterminatevisibility(false); /* download complete. lets update ui */ if (result == 1) { adapter = new myrecycleradapter(feedlistactivity.this, feeditemlist); mrecyclerview.setadapter(adapter); } else { log.e(tag, "failed fetch data!"); } } } private void parseresult(string result) { try { jsonobject response = new jsonobject(result); jsonarray posts = response.optjsonarray("news"); /*initialize array if null*/ if (null == feeditemlist) { feeditemlist = new arraylist<feeditem>(); } (int = 0; < posts.length(); i++) { jsonobject post = posts.optjsonobject(i); feeditem item = new feeditem(); item.settitle(post.optstring("news_desc")); item.setdescription(post.optstring("date")); item.seturl(post.optstring("news")); feeditemlist.add(item); } } catch (jsonexception e) { e.printstacktrace(); } } }
myrecycleradapter.java
package com.bbau.ankit.test_splash; import android.content.context; import android.content.intent; import android.support.v7.widget.recyclerview; import android.text.html; import android.text.style.alignmentspan; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.textview; import android.widget.toast; import org.w3c.dom.text; import java.text.parseexception; import java.text.simpledateformat; import java.util.date; import java.util.list; /** * created ankit on 8/14/2015. */ public class myrecycleradapter extends recyclerview.adapter<feedlistrowholder> { private list<feeditem> feeditemlist; private context mcontext; public myrecycleradapter(context context, list<feeditem> feeditemlist) { this.feeditemlist = feeditemlist; this.mcontext = context; } @override public feedlistrowholder oncreateviewholder(viewgroup viewgroup, int i) { view v = layoutinflater.from(viewgroup.getcontext()).inflate(r.layout.list_row, null); feedlistrowholder mh = new feedlistrowholder(v); mh.relativelayout.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { log.d("list size", integer.tostring(getitemcount())); textview redditurl = (textview) v.findviewbyid(r.id.url); string posturl = redditurl.gettext().tostring(); log.d("the url:", posturl); intent intent = new intent(mcontext, webviewactivity.class); intent.putextra("url", posturl); mcontext.startactivity(intent); } }); return mh; } @override public void onbindviewholder(feedlistrowholder feedlistrowholder, int i) { final feeditem feeditem = feeditemlist.get(i); feedlistrowholder.title.settext(html.fromhtml(feeditem.gettitle())); feedlistrowholder.description.settext(feeditem.getdescription()); feedlistrowholder.url.settext(feeditem.geturl()); } @override public int getitemcount() { return (null != feeditemlist ? feeditemlist.size() : 0); } }
of course can use recycler view , endlessscrool combination this..
here sample
private boolean loading = true; int pastvisiblesitems, visibleitemcount, totalitemcount; mrecyclerview.setonscrolllistener(new recyclerview.onscrolllistener() { @override public void onscrolled(recyclerview recyclerview, int dx, int dy) { visibleitemcount = mlayoutmanager.getchildcount(); totalitemcount = mlayoutmanager.getitemcount(); pastvisiblesitems = mlayoutmanager.findfirstvisibleitemposition(); if (loading) { if ( (visibleitemcount + pastvisiblesitems) >= totalitemcount) { loading = false; log.v("...", "last item wow !"); } } } });
don't forget add
linearlayoutmanager mlayoutmanager; mlayoutmanager = new linearlayoutmanager(this); mrecyclerview.setlayoutmanager(mlayoutmanager);
here github example: endlessrecycleronscrolllistener
Comments
Post a Comment