Android Recyclerview:super Fast ListView
The original is from my public number: longkai_1991
First, look at the effect:
A few days ago just finished release of a company project, have a little time, so I want to find some interesting things to learn, by the way in the project. When I saw my iOS colleagues talking about IOS8 's xx features, I suddenly wanted to add the features of the Android L version to the next version of the company project.
At the end of June when watching Google Io, when the new design language for Android, Material designed, there is not much affection, feel the color of a lump of a lump, good ugly appearance, then felt that the bright spot is the new art run-time environment and some cool animation effect. Then, at the beginning of August, I really took the Nexus 5 out of curiosity to install a preview version of L, the experience was poor ... A lot of software is still holo, anyway feel not very look forward to IS.
Back to the point, download the latest SDK and you will find ANDROID_HOME/extras/android/m2repository/com/android/support a lot more compatible libraries,CardView, support-annotations, recyclerview-v7, This time, Google really took out a lot of things ah, like, especially CardView and Recyclerview these two new controls, this is on Google's latest material Design home page with instructions and simple introduction, in short, CardView can provide a card layout that is consistent with Google's many home apps, while Recyclerview is an enhanced ListView that is more powerful and usable.
Preface, especially want to try, but there is a pit, because it is still a preview version, so Google set minsdkversion as L, meaning that only use the L Preview system of the machine can be tested. Hehe, the masses of people how will be this to frighten, online has in Androidmanifest.xml set <uses-sdk tools:node="replace" /> can. There is another way to extract the source code, and then in accordance with the project structure to put the source files, and finally in their own projects introduced in the good, but to note that the L version of the relevant code to delete, do not matter, anyway, Google launched the official version.
Nonsense pull so much, the following is today's theme, Super fast ListView, never seen such a quick list, and even support horizontal scrolling, to know, this in the previous Android, to achieve horizontal list is more than the egg hurts! There are more surprises, in another compatibility library leanback-v17 , and more advanced widgets such as Grid,stagedgrid,horizonalgrid, know Pinterest waterfall flow?
The following code, which provides a swipe to the bottom of the auto-load more features, is my own according to the experience of previous ListView, because of the speed of loading, in the removal of loading more hints, sometimes there will be a part of the page blank spacing problem, no way, had to postdelay 50 milliseconds, Append the loaded list back to the end.
The following is the source code, using Recycle view with card view to implement an infinite list (automatically with prompt loading more, and contains no type of view), Super fast~ look at this code before I hope you can go first material Design's homepage to see the basic introduction and sample code.
MainActivity.java
/* * Copyright (c) Longkai * The Software shall is used for good, not evil. */package com.example.gridlayout;import android.app.activity;import Android.app.fragment;import android.os.Bundle; Import Android.os.handler;import Android.support.annotation.nullable;import Android.support.v7.widget.linearlayoutmanager;import Android.support.v7.widget.recyclerview;import Android.util.log;import Android.view.layoutinflater;import Android.view.menu;import Android.view.MenuItem;import Android.view.view;import Android.view.viewgroup;import Android.widget.textview;import Com.manuelpeinado.refreshactionitem.refreshactionitem;import Org.json.jsonexception;import Org.json.JSONObject; Import Java.util.arraylist;import Java.util.list;import Java.util.concurrent.timeunit;public class MainActivity Extends Activity {public static final String TAG = MainActivity.class.getSimpleName (); public static final String type = "type"; public static final int ITEM = 0; public static final int simple = 1; PubLIC static final int FOOTER = 2; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); if (savedinstancestate = = null) {try {Getfragmentmanager (). BeginTransaction (). Replace (Android. R.id.content, CardFragment.class.newInstance ()). commit (); } catch (Instantiationexception e) {e.printstacktrace (); } catch (Illegalaccessexception e) {e.printstacktrace (); }}} @Override public boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override public Boolean onmenuitemselected (Int. Featureid, MenuItem Item) {switch (Item.getitemid ()) {case R. Id.action_settings:break; Default:break; } return super.onmenuitemselected (Featureid, item); public static class Cardfragment extends Fragment {Boolean loading = false; Handler Mhandler = new Handler (); Recyclerview MrecyclervieW Cardadapter Madapter; @Nullable @Override public View oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { View view = Inflater.inflate (R.layout.recycler, container, false); Mrecyclerview = (Recyclerview) View.findviewbyid (R.id.recycler); return view; } @Override public void onviewcreated (view view, Bundle savedinstancestate) {super.onviewcreated (view, Savedinsta Ncestate); } @Override public void onactivitycreated (Bundle savedinstancestate) {super.onactivitycreated (savedinstancestate) ; list<jsonobject> list = new arraylist<> (); try {for (int i = 0; i <; i++) {Jsonobject jsonobject = new Jsonobject (); if (i% = = 0) {jsonobject.put (TYPE, simple); } else {jsonobject.put (TYPE, ITEM); } list.add (Jsonobject); }} catch (Jsonexception ignore) {} Madapter = new Cardadapter (list); Mrecyclerview.sethasfixedsize (TRUE); Mrecyclerview.setadapter (Madapter); Linearlayoutmanager LayoutManager = new Linearlayoutmanager (getactivity ()); Layoutmanager.setorientation (linearlayoutmanager.vertical); Mrecyclerview.setlayoutmanager (LayoutManager); Mrecyclerview.setonscrolllistener (New Recyclerview.onscrolllistener () {@Override public void onscrollstatechanged ( int newstate) {} @Override public void onscrolled (int dx, int dy) {if (!loading && layout Manager.findlastvisibleitemposition () = = List.size ()-1) {loading = true; Jsonobject jsonobject = new Jsonobject (); try {jsonobject.put (TYPE, FOOTER); } catch (Jsonexception ignore) {} madapter.add (Jsonobject); New Thread ((), {try {TimeUnit.SECONDS.sleep (3); } catch (Interruptedexception e) {e.printstacktrace (); } list<jsonobject> tmp = new arraylist<jsonobject> (); for (int i = 0; i < i++) {Jsonobject json = new Jsonobject (); try {json.put (TYPE, ITEM); } catch (Jsonexception e) {} tmp.add (JSON); } mhandler.post ((), {madapter.remove (Madapter.getitemcount ()-1); Madapter.addall (TMP); Loading = false; }); }). Start (); } } }); }} private static class Cardadapter extends Recyclerview.adapter<recyclerview.viewholder> {private List<js Onobject> list; Private Cardadapter (list<jsonobject> list) {this.list = list; } @Override public int getitemcount () {return list.size (); } @Override Public Recyclerview.viewholder oncreateviewholder (viewgroup parent, int viewtype) {switch (ViewType) {case Simple:return new Recyclerview.viewholder (Layoutinflater.from (Parent.getcontext ()). Infla Te (Android. R.layout.simple_list_item_1, parent, False)) {}; Case Footer:return New Recyclerview.viewholder (Layoutinflater.from (Parent.getcontext ()). Inflate (R.layout.load_mo Re, parent, false)) {}; Default:case Item:view View = Layoutinflater.from (Parent.getcontext ()). Inflate (R.layout.card, parent, FA LSE); Cardviewholder holder = new Cardviewholder (view); return holder; }} @Override public void Onbindviewholder (recyclerview.viewholder holder, int position) {switch (List.get (POS ition). Optint (TYPE)) {case Item:cardviewholder Cardviewholder = (cardviewholder) holder; CardViewHolder.textView.setText ("item" + position); Break Case Simple:textview txt = (TextView) holder.itemView.findViewById (Android. R.ID.TEXT1); TXt.settext ("Simple txt!"); LOG.D (TAG, "simple text"); Break Case FOOTER:LOG.D (TAG, "footer!"); Break }} @Override public int getitemviewtype (int position) {return list.get (position). Optint (TYPE); } public void Add (Jsonobject jsonobject) {this.list.add (jsonobject); Notifyiteminserted (List.size ()-1); } public void AddAll (list<jsonobject> list) {this.list.addAll (list); Notifydatasetchanged (); } public void Remove (int i) {list.remove (i); Notifyitemremoved (i); } static class Cardviewholder extends Recyclerview.viewholder {TextView TextView; Cardviewholder (view view) {super (view); TextView = (TextView) View.findviewbyid (r.id.txt); } } }}
Here is the layout file, very simple
card.xml
<android.support.v7.widget.cardview xmlns:android= "http://schemas.android.com/apk/res/android" xmlns: card_view= "Http://schemas.android.com/apk/res-auto" style= "@style/cardview.light" Card_view: cardcornerradius= "4DP" android:layout_width= "match_parent" android:layout_height= "Wrap_content" > <textview android:id= "@+id/txt" android:textappearance= "? Android:textappearancemedium " android:gravity= "center" android:layout_width= "match_parent" android:layout_height= "200DP"/></ Android.support.v7.widget.cardview>
recycler.xml
<android.support.v7.widget.recyclerview xmlns:android= "Http://schemas.android.com/apk/res/android" Android:id= "@+id/recycler" android:layout_width= "match_parent" android:layout_height= "match_parent"/ >
The last one, it's ugly, just for demonstration.
Because it is the company's project, so the more detailed code is not posted, but also based on this code out, there is time, another day package a out ~
by Longkai to 1 Sep. In Sz.
Android Recyclerview:super fast ListView super fast lis