addView dynamically can not get measuredHeight in android -


i try encapulate custom listview head , footer.here description of problem.

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >  <com.example.pj.news_demo.widget.wrapperview     android:id="@+id/fragment_msg_wrapper_view"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="@android:color/holo_blue_bright"     >     <android.support.v7.widget.recyclerview         android:id="@+id/msg_list"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:background="#ff34ff"         android:paddingleft="10dp"         android:paddingright="10dp"         /> </com.example.pj.news_demo.widget.wrapperview> 

code above ui,wrapperview can treated container,and recycleview initial content.in outside,some interface provided user ,handling wrapperview,like addheaderview() , addfooterview().

so,here strange thing. in fragment,i got wrapperview,try add header , footer it.

  mwrapperview.addheaderview(new refreshheader(getcontext()));   mwrapperview.addfooterview(new refreshfooter(getcontext())); 

in wrapper,i try children's measuredheght,header,recycleview , footer.

i show detail of addheaderview() , addfooterview()

    public void addheaderview(refreshheader headerview) {     if (headerview == null) {         throw new illegalargumentexception("footerview must not null");     }     viewutil.removeviewfromparent(headerview);     mheaderview = headerview;     addview(mheaderview, 0); *//header first child*     if (mheaderview instanceof iheadercallback) {         mheadercallback = (iheadercallback) mheaderview;     } else {         throw new illegalstateexception("headview must implement iheadercallback interface");     } }    public void addfooterview(refreshfooter footerview) {     if (footerview == null) {         throw new illegalargumentexception("footerview must not null");     }     viewutil.removeviewfromparent(footerview);     mfooterview = footerview;     log.i(tag, "footerview添加之前的子孩子数量:" + getchildcount());     addview(footerview,getchildcount()); *//footer last child*     if (mfooterview instanceof ifootercallback) {         mifootercallback = (ifootercallback) footerview;     } else {         throw new illegalstateexception("footerview must implement ifootercallback interface");     }  } 

i override wrapperview's onmeasure function,try children's name , measuredheight,here code.

    @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {     super.onmeasure(widthmeasurespec, heightmeasurespec);     (int = 0; < getchildcount(); i++) {         log.i(tag, "child---" + getchildat(i).getclass().getcanonicalname() + "---height:" + getchildat(i).getmeasuredheight() + "\n");     } } 

and,here result: child---com.example.pj.news_demo.widget.refreshheader---height:210

child---android.support.v7.widget.recyclerview---height:1323

child---com.example.pj.news_demo.widget.refreshfooter---height:0

i can not footerview's measuredheight.

util problem solved,i can not move step further.i hope guys can give me suggestions.thank you!

it because view did not created when getmeasuredheight executed.

use viewtreeobserver on wrapperview.

viewtreeobserver vto = mwrapperview.getviewtreeobserver();  vto.addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() {         @override         public void ongloballayout() {                              //get height here , stuff         }     }); 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -