android - Custom XML attributes without custom View in Fragment -
i'm trying add together custom xml attributes in existing view
adding them custom view not big deal don't know how access attributes in "basic" view (e.g. textview, linearlayout, imageview ...)
and more hard when there fragment , / or library project involved
so far here code
customs attributes definitions , xml (attrs.xml , layout):
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="starwars"> <attr name="jedi" format="string" /> <attr name="rank" format="string" /> </declare-styleable>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:sw="http://schemas.android.com/apk/res-auto" android:layout_width="fill_parent" android:layout_height="fill_parent"> <textview android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="28dp" android:gravity="center_horizontal" sw:jedi="obiwan" /> <textview android:id="@+id/rank" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="28dp" android:gravity="center_horizontal" sw:rank="master" />
since inflate on fragment (so no oncreate attrs arg !), way seek 2 sw custom attributes :
set custom layoutinflater.factory fragment layoutinflater in oncreateview
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { super.oncreateview(inflater, container, savedinstancestate); layoutinflater layoutinflater = inflater.cloneincontext(inflater.getcontext()); layoutinflater.setfactory(new starwarslayoutfactory()); view fragmentview = layoutinflater.inflate(r.layout.jedi, container, false); ((textview) fragmentview.findviewbyid(android.r.id.jedi)).settext("yep"); homecoming fragmentview; }
trying custom attributes on custom layoutinflater.factory :
public class starwarslayoutfactory implements mill { @override public view oncreateview(string name, context context, attributeset attrs) { *** here ? *** homecoming null; } }
anyone have had kind of question ?
what i'm missing here ?
thx in advance !
i have done :)
you have create new layoutinflater.factory
did on op since mill used inflated layout view, , have homecoming null on factory.oncreateview
(to allow android handle inflation) must cache custom xml attibutes somewhere
so here solution :
your layout xml view must have , android:id
create class maintain custom attributes :
class="lang-java prettyprint-override"> public class attributeparser { private attributeparserfactory mfactory; private map<integer, hashmap<integer, string>> mattributelist; private class attributeparserfactory implements layoutinflater.factory{ @override public view oncreateview(string name, context context, attributeset attrs) { string id = attrs.getattributevalue("http://schemas.android.com/apk/res/android", "id"); if(id != null){ // string reference character "@", strip maintain reference id = id.replace("@", ""); typedarray librarystyledattributelist = context.obtainstyledattributes(attrs, r.styleable.newshublibrary); hashmap<integer, string> libraryviewattribute = new hashmap<integer, string>(); int = 0; for(int attribute : r.styleable.newshublibrary){ string attributevalue = librarystyledattributelist.getstring(i); if(attributevalue != null) libraryviewattribute.put(attribute, attributevalue); i++; } if(!libraryviewattribute.isempty()) mattributelist.put(integer.valueof(id), libraryviewattribute); librarystyledattributelist.recycle(); } homecoming null; } } public attributeparser(){ mattributelist = new hashmap<integer, hashmap<integer, string>>(); mfactory = new attributeparserfactory(); } public void clear() { mattributelist.clear(); } public layoutinflater getlayoutinflater(layoutinflater inflater) { clear(); layoutinflater layoutinflater = inflater.cloneincontext(inflater.getcontext()); layoutinflater.setfactory(mfactory); homecoming layoutinflater; } public void setfactory(layoutinflater inflater){ inflater.cloneincontext(inflater.getcontext()).setfactory(mfactory); } public void setviewattribute(activity activity) { for(entry<integer, hashmap<integer, string>> attribute : mattributelist.entryset()) if(activity.findviewbyid((integer) attribute.getkey()) != null) activity.findviewbyid((integer) attribute.getkey()).settag(attribute.getvalue()); } public void setviewattribute(view view) { for(entry<integer, hashmap<integer, string>> attribute : mattributelist.entryset()) if(view.findviewbyid((integer) attribute.getkey()) != null) view.findviewbyid((integer) attribute.getkey()).settag(attribute.getvalue()); } public map<integer, hashmap<integer, string>> getattributelist() { homecoming mattributelist; } public void setattributelist(map<integer, hashmap<integer, string>> attributelist) { this.mattributelist = attributelist; } }
your custom attributes stored in each view tag when utilize attributeparser :
layoutinflater layoutinflater = mattributeparser.getlayoutinflater(inflater); view view = layoutinflater.inflate(r.layout.jedi, null); mattributeparser.setviewattribute(view);
android android-fragments android-view android-xml android-custom-attributes
No comments:
Post a Comment