Android Gridview Row Height Issue -


i have gridview don't want constant row height. want row height vary accommodate tallest content in each row. using https://stackoverflow.com/a/7568226/1149456 (modified can resume multiple adapters) solution have ran problem. rows re-sized correctly until open in second fragment.

for example, open list of achievement categories in fragment fragment 1 , when select category new fragment opened list of sub-categories in fragment 2. first item not re-sized if second larger such here in screenshot.

if open fragment 2 first re-sizes correctly, or if scroll , down resizes correctly screenshot.

automeasuregridview.java

public class automeasuregridview extends gridview { private listadapter madapter;  public automeasuregridview(context context) {     super(context); }  public automeasuregridview(context context, attributeset attrs) {     super(context, attrs); }  public automeasuregridview(context context, attributeset attrs, int defstyle) {     super(context, attrs, defstyle); }  @override public void setadapter(listadapter adapter) {     this.madapter = adapter;      super.setadapter(adapter);     super.setadapter(this.madapter); }  @override public listadapter getadapter() {     return this.madapter; }  @override protected void onlayout(boolean changed, int l, int t, int r, int b) {     if(changed) {         //tutorialadapter adapter = (tutorialadapter)getadapter();         int numcolumns = getnumcolumns();          if(getadapter() != null) {             gridviewitemlayout.inititemlayout(numcolumns, getadapter().getcount());         }          /*if(numcolumns > 1) {             int columnwidth = getmeasuredwidth() / numcolumns;             adapter.measureitems(columnwidth);         }*/     }     super.onlayout(changed, l, t, r, b); } 

}

the original code calls adapter.measureitems(columnwidth); found code part worked without , unable reuse code multiple adapters calling custom function.

gridviewitemlayout.java public class gridviewitemlayout extends linearlayout {

// array of max cell heights each row private static int[] mmaxrowheight;  // number of columns in grid view private static int mnumcolumns;  // position of view cell private int mposition;  // public constructor public gridviewitemlayout(context context) {     super(context); }  // public constructor public gridviewitemlayout(context context, attributeset attrs) {     super(context, attrs); }  /**  * set position of view cell  * @param position  */ public void setposition(int position) {     mposition = position; }  /**  * set number of columns , item count in order accurately store  * max height each row. must called whenever there change layout  * or content data.  *   * @param numcolumns  * @param itemcount  */ public static void inititemlayout(int numcolumns, int itemcount) {     mnumcolumns = numcolumns;     mmaxrowheight = new int[itemcount]; }  @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {     super.onmeasure(widthmeasurespec, heightmeasurespec);     // not calculate max height if column count 1     if(mnumcolumns <= 1 || mmaxrowheight == null) {         return;     }      // current view cell index grid row     int rowindex = mposition / mnumcolumns;     // measured height layout     int measuredheight = getmeasuredheight();     // if current height larger previous measurements, update array     if(measuredheight > mmaxrowheight[rowindex]) {         mmaxrowheight[rowindex] = measuredheight;     }     // update dimensions of layout reflect max height     setmeasureddimension(getmeasuredwidth(), mmaxrowheight[rowindex]); } 

}

i have tried both achievementadapter.notifydatasetchanged(); , achievementlistview.invalidateviews(); neither forced resize.

achievementadapter.java public class achievementadapter extends arrayadapter {

    private final context context;     private final arraylist<achievementsitem> swtorachievements;     int advancedpos1;     int advancedpos2;     string type;      public achievementadapter(context context, arraylist<achievementsitem> swtorachievements, string type) {         super(context, r.layout.achievement_row, swtorachievements);          this.context = context;         this.swtorachievements = swtorachievements;         this.type = type;     }      @override     public view getview(int position, view convertview, final viewgroup parent) {          layoutinflater inflater = (layoutinflater) context             .getsystemservice(context.layout_inflater_service);          view rowview = null;         final achievementsitem item = swtorachievements.get(position);          if(item.isgroupheader()){             rowview = inflater.inflate(r.layout.advanced_class_tab3_header, parent, false);             textview txtheader = (textview) rowview.findviewbyid(r.id.txtabilitytitle);             txtheader.settext(item.gettitle());          } else {             rowview = inflater.inflate(r.layout.achievement_row, parent, false);              textview txtviewcategory1 = (textview) rowview.findviewbyid(r.id.txtcategory1);             textprogressbar txtviewprogress = (textprogressbar) rowview.findviewbyid(r.id.progressbarwithtext);              if (item != null) {                  if (type.equals("")) {                     txtviewcategory1.settext(item.getcategory1());                 } else if (type.equals("category1")) {                     txtviewcategory1.settext(item.getcategory1());                 } else if (type.equals("category2")) {                     txtviewcategory1.settext(item.getcategory2());                 } else if (type.equals("category3")) {                     txtviewcategory1.settext(item.getcategory3());                  }                   txtviewprogress.settext("0 / " + item.getcount());             }          }         return rowview;     } 

}

a stupid way can think of set layout height of gridview manually when setting up. like

if(newheight>oldheight)  {      gridview.setlayoutparams();       // here set layout height new height }  fragment.addfragment();                // replace old gridview element new element 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -