In the project development process, will encounter many of the ListView, the ancient method is every encounter a ListView, in the XML file inside write Emptyview, and then add Emptyview, in fact, in many cases, Different ListView Emptyview is the same, for this, write a simple helper class, in the ListView need to add Emptyview, as long as the addition of a line of code can be implemented
The name of the helper class is Emptyviewhelper, and the code is as follows:
public class Emptyviewhelper {private ListView mlistview;private View emptyview;private Context mcontext;private String m Emptytext;private TextView mtextview;public Emptyviewhelper (ListView listview) {Mlistview = Listview;mcontext = Listview.getcontext (); Initemptyview ();} Public Emptyviewhelper (ListView ListView, String text) {Mlistview = Listview;mcontext = Listview.getcontext (); Memptytext = Text;initemptyview ();} private void Initemptyview () {Emptyview = View.inflate (mcontext, R.layout.empty_view, null);((ViewGroup) Mlistview.getparent ()). AddView (Emptyview); Mlistview.setemptyview (Emptyview); if (! Textutils.isempty (Memptytext)) {((TextView) Emptyview.findviewbyid (R.id.textview)). SetText (Memptytext);}}
This help class, get the current ListView, then inflate write the Emptyview in advance, and then set the Emptyview on it.
Then, when a ListView needs to add Emptyview, as long as the following line of code is available
Emptyviewhelper emptyviewhelper = new Emptyviewhelper (Mlistview, "You do not have an order");
The implementation method is quite simple, and this has the following benefits:
1, pre-written good emptyview, each encounter needs to set Emptyview, as long as a line of code can be achieved
2, when the Emptyview need to change, as long as the change of Emptyview, all the Emptyview in the ListView is changed
Since I've used this help class in my project, I've saved a lot of time in development, and now I'm sharing it as a backup for the next development, and I hope it will be a little helpful ....
Android Development: ListView Add Emptyview Help Class