Preface: Recently, it involves dealing with QQ. It defines that all friends can only have 300 messages in total. If 300 or more are read from the database at one time, the interface will be slight stuttering. So considering the next page, only 20 messages are displayed (similar to WeChat) when the user slides to the first message, if there are messages in the database, 20 more messages will be loaded.
Steps - How many steps should I cut off the elephant's refrigerator?
1. Customize
The core thing is the scrollListerner that monitors ListView. Here you can't find the original text connection. If the original author sees it, please contact the brothel Ai Xiaosheng and update your article link. I have dug a pretty good one online. When you use it, you can implement this scrollListerner and improve your logic.
public class MyOnScrollListener implements OnScrollListener { private int totalItemCount; //The last item of ListView private int lastItem; //listview item 1 private int firstItem; // Used to determine whether it is currently loading private boolean isLoading; //Load more layouts at the bottom private View footer; //Instance of interface callback private OnloadDataListener listener; //data private List<MsgBean> data; Handler handler = new Handler(); public MyOnScrollListener(View footer, List<MsgBean> data) { = footer; = data; } //Set an instance of interface callback public void setOnLoadDataListener(OnloadDataListener listener) { = listener; } /** * Change of sliding state * * @param view * @param scrollState 1 SCROLL_STATE_TOUCH_SCROLL is dragging 2 SCROLL_STATE_FLING is inertial sliding 0SCROLL_STATE_IDLE is stopping, only when switching between different states */ @Override public void onScrollStateChanged(AbsListView view, int scrollState) { //If the data is not loaded and the sliding state is stopped and it rolls to the first item, you can make a decision to update the pull-down or pull-up update. if (!isLoading && firstItem == 0 && scrollState == SCROLL_STATE_IDLE) { //Show more loading (); //Simulate a refresh function with two seconds delay (new Runnable() { @Override public void run() { if (listener != null) { //Start loading more data loadMoreData(); //Callback sets ListView data (data); //What to do after loading is completed loadComplete(); } } }, 2000); } } /** * When the loading data is completed, setting the loading flag to false means that there is no data loading. * And set the bottom load more to hide */ private void loadComplete() { isLoading = false; (); } /** * Start loading more new data, here only three pieces of data are updated at a time */ private void loadMoreData() { isLoading = true; MsgBean msg = null; for (int i = 0; i < 3; i++) { msg = new MsgBean(); msg .setRemark("Liming"+i); msg .setMsgID(i); (stu); } } /** * Listen to visible interface * * @param view ListView * @param firstVisibleItem index of the first visible item * @param visibleItemCount The number of items that can be displayed * @param totalItemCount How many items are there in total */ @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { //Implement drop-down loading lastItem = firstVisibleItem + visibleItemCount; //Implement pull-up loading firstItem = firstVisibleItem; //Total listView items = totalItemCount; } //Callback interface public interface OnloadDataListener { void onLoadData(List<MsgBean> data); } }
2. Implement this interface
public class ListPageActivity extends Activity implements { @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_list_page); //Show to ListView showListView(data); //Custom scrolling listening events MyOnScrollListener onScrollListener = new MyOnScrollListener(header, data); //Set the interface callback (this); //Set the scroll listening event of ListView (onScrollListener); @Override public void onLoadData(List<MsgBean> data) { //After loading data is completed, display the data to ListView showListView(data); } }
ShowListView is undoubtedly a normal job of updating adapters
So how do we use the xutils database to classify?
3. Use xutils database operations for pagination processing
First, let’s sort out the idea. We have implemented the pull-up callback above, and load the new data into the adapter in this callback.
The following db is an instance of Dbmanager
/** * Number of messages currently displayed on the screen */ private int MAX_MSG_NUMBER = 20;
private List<MsgBean> getDataFromDb() { List<?> dbSize = ().where(("id", "=", 400)).findAll();//Remember to catch null pointer and DbException exception//If the database is smaller than the number of pages we display, it will not be offset, otherwise, it will be offset to the position we need to display if (() < MAX_MSG_NUMBER) { indexOffset = 0; } else { indexOffset = () - MAX_MSG_NUMBER; } List<MsgBean> datas = ().where(("id", "=", 400)).limit(MAX_MSG_NUMBER) .offset(indexOffset).findAll(); return datas; }
Here's an explanation
().where(("id", "=", 400)).limit(MAX_MSG_NUMBER).offset(indexOffset).findAll(); is the key to our implementation of pagination
.limit is the page size we define
.offset offset, the size of our database is unchanged. If the offset is not defined, then the page size we define is only fetched from 0 to 19 each time. Assuming there are 21 pieces of data in the database, then we need to fetch from 1 to 20, instead of 0 to 19, so the offset is 1.
Then we are in loadMoreData
MAX_MSG_NUMBER += MAX_MSG_NUMBER; getDataFromDb();
Add the size by itself to complete the loading of more functions, and just load the data in onLoadData(List<MsgBean> data).
I have encapsulated my xutils database operation later, and there are still many imperfections.
/** * Database xutils usage * @author Brothel Loves Young Student */ public class DbUtil { private static final String TAG = (); private static DbUtil dbUtil; private DbManager db; private DbUtil(){ db = (().daoConfig); } public static DbUtil getInstance(){ if(dbUtil == null){ synchronized () { if(dbUtil == null){ dbUtil = new DbUtil(); } } } return dbUtil; } /** * Add data * @param list * @throws DbException */ public void addMsgList(List<MsgBean> list) { try { (list); } catch (DbException e) { (); (TAG, ()); } } /** * Add one data * @param node * @throws DbException */ public void addMsgToDb(MsgBean node) { try { (node); } catch (DbException e) { (); (TAG, ()); } } /** * Delete all data in the table * @param cls Mapping of tables created * @throws DbException */ public void deleteAll(Class cls) { try { (cls); } catch (DbException e) { (TAG, ()); (); } } /** * Delete the first piece of data * @param cls */ @SuppressWarnings("unchecked") public void deleteFirst(Class cls){ try { ((cls)); } catch (DbException e) { // TODO Auto-generated catch block (); } } /** * Query all data in the table * @throws DbException */ @SuppressWarnings("unchecked") public List<?> findAll(Class cls) { try { return (cls) == null ? () : (cls); } catch (DbException e) { (); (TAG, ()); return (); } } /** * //Add query conditions for query List<ChildInfo> all = ().where("id",">",2).and("id","<",4).findAll(); * @return Search for data for specified criteria */ @SuppressWarnings("unchecked") public List<?> findDataByWhere(Class cls,WhereBuilder format){ try { return (cls).where(format).findAll()== null ? () :(cls).where(format).findAll(); } catch (DbException e) { (TAG, ()); (); return (); } } /** * Add query conditions to query * @param cls table mapping * @param str select statement * @param format where statement * @return List<DbModel> DbModel key is the database column name value is the value * eg:(() .where("id" ,"<", 54) .and(("age", ">", 20).or("age", " < ", 30)) .orderBy("id") .limit(pageSize) .offset(pageSize * pageIndex)); * * * */ @SuppressWarnings("unchecked") public Selector<?> findDataBySelector(Class cls,WhereBuilder format){ try { return (cls).where(format); } catch (DbException e) { // TODO Auto-generated catch block (); } return null; } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.