Android Development: Swiperefreshlayout cannot display Emptyview

Source: Internet
Author: User

In the use of the official control swiperefreshlayout, found that the original Emptyview disappeared, in the online search for a lot of information, did not match my answer, because my emptyview is a help class to achieve, and then the hundred setbacks, finally found a perfect way , introduced as follows:


First, change the layout of the XML, add a layer of framelayout outside, and the improved XML layout is as follows:

<framelayout android:id= "@+id/parent" android:layout_width= "Match_parent" android:layout_height= " 0DP "android:layout_weight=" 1 "> <android.support.v4.widget.swiperefreshlayout android:id=" @ +id/swipe_container "android:layout_width=" match_parent "android:layout_height=" match_parent "> &lt ;                TextView android:id= "@+id/empty_textview" android:layout_width= "Match_parent"                 android:layout_height= "Match_parent" android:text= "Empty_textview" android:textsize= "25SP" /> <listview android:id= "@+id/listview" Android:layout_widt                H= "Match_parent" android:layout_height= "match_parent" android:background= "@color/white" android:scrollbarstyle= "Outsideoverlay"/> </android.support.v4.widget.swipere Freshlayout> </framelayout> 

The reason for this is because Swipelayout is viewgroup, can not directly addview, or say yes, but AddView is no effect, so nested a framelayout outside, with this layout to add Emptyview , here is the Help class for Emptyview


public class Emptyviewhelper {private ListView mlistview;private View emptyview;private Context mcontext;private String m Emptytext;private TextView mtextview;private framelayout parent;public emptyviewhelper (ListView ListView, String Text) {Mlistview = Listview;mcontext = Listview.getcontext (); memptytext = Text;initemptyview ();} Public Emptyviewhelper (ListView ListView, String Text, framelayout parent) {Mlistview = Listview;mcontext = Listview.getc Ontext (); memptytext = Text;this.parent = Parent;initemptyview ();} private void Initemptyview () {Emptyview = View.inflate (mcontext, R.layout.empty_view, NULL); Layoutparams LP = new Layoutparams (layoutparams.wrap_content, layoutparams.wrap_content, gravity.center_horizontal); Parent.addview (Emptyview, LP); Mlistview.setemptyview (Emptyview); Textutils.isempty (Memptytext)) {((TextView) Emptyview.findviewbyid (R.id.textview)). SetText (Memptytext);}}


In this case, for a ListView that needs to add Emptyview, use the following code directly to achieve


Emptyviewhelper emptyviewhelper = new Emptyviewhelper (Mlistview, "Loading", (framelayout) V.findviewbyid (R.id.parent));

This way, the Emptyview effect of the ListView is out.


In the gradual development process, found that the code reuse more and more important, for the same function, can use the Help class to achieve, as far as possible with the help class, reduce the workload of our Code, the same work later, as long as very little code can be achieved, or the same module, but only need very little code, such a method, In the late update, but also as long as the update of a module, all are updated, suitable for use in the work.





Android Development: Swiperefreshlayout cannot display Emptyview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.