android - Instantiate custom view programmatically the right way -


i have custom view called eventitemview i'm using in both xml file , in custom adapter (which extends recyclerview.adapter , used in recyclerview). problem appearance in recyclerview weird not in xml file :

enter image description here

  1. declared using xml
  2. instantiated dynamically in adapter

and believe due way instanciate views in adapter, doing new eventitemview(context) , not passing additional arguments attributeset, int, int needed render xml version. problem don't know how them in adapter.

here's custom view declaration :

eventitemview.java

public class eventitemview extends framelayout {     public eventitemview(context context) {         this(context, null);     }      public eventitemview(context context, attributeset attrs) {         this(context, attrs, 0);     }      public eventitemview(context context, attributeset attrs, int defstyleattr) {         super(context, attrs, defstyleattr, 0);         init(context, attrs, defstyleattr, 0);     }      @targetapi(build.version_codes.lollipop)     public eventitemview(context context, attributeset attrs, int defstyleattr, int defstyleres) {         super(context, attrs, defstyleattr, defstyleres);         init(context, attrs, defstyleattr, defstyleres);     }      protected void init(context context, attributeset attrs, int defstyleattr, int defstyleres) {         eventitem = new eventitem();          inflate(context, r.layout.view_event_item, this);         butterknife.bind(this);          typedarray values = context.gettheme().obtainstyledattributes(attrs, r.styleable.eventitemview, defstyleattr, defstyleres);          try {             setsubtitle(values.getstring(r.styleable.eventitemview_subtitle));             ...         }         {             values.recycle();         }     } } 

layout/view_event_item.xml

<?xml version="1.0" encoding="utf-8"?> <linearlayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:background="@drawable/selector_item_event"     android:clickable="true"     android:gravity="center"     android:layout_height="72dp"     android:layout_width="match_parent"     android:orientation="horizontal"     android:padding="16dp">      ...  </linearlayout> 

this view used in 1) activity.xml file :

<com.app.package.view.eventitemview     android:id="@+id/current_event_view"     android:layout_height="wrap_content"     android:layout_width="match_parent" /> 

and 2) in adapter.java :

@override public viewholder oncreateviewholder(viewgroup parent, int viewtype) {     eventitemview itemview = new eventitemview(context);      return new viewholder(itemview); } 

how can instantiate custom view relevant attrs, defstyleattr , defstyleres constructor parameters? or maybe view behaving in weird way totally different reason?

you need call measure() , layout().

an instantiated custom view doesn't know sizes need inflate to, need pass calls.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -