Android Recycler layouts not inflating properly why -
i use android recyclerview inflate 3 layouts shown in code footer not being inflated hence hides data of listview layout. problem code since want listview layout displayed last layout in recyclerview adapter.
package com.news254.inception.newsapp; import android.content.intent; import android.content.res.configuration; import android.support.v4.widget.drawerlayout; import android.support.v7.app.actionbardrawertoggle; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.support.v7.widget.toolbar; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.imageview; public class newspaper extends appcompatactivity { toolbar mtoolbar; string titles[] = {"newspaper","gossip","entertainment","sports","international","about","exit"}; int icons[] = {r.drawable.newspaper,r.drawable.gossip,r.drawable.entertainment,r.drawable.sports,r.drawable.international,r.drawable.about,r.drawable.exit}; //similarly create string resource name , email in header view //and create int resource profile picture in header view boolean isdraweropen =true; private toolbar toolbar; // declaring toolbar object recyclerview mrecyclerview; // declaring recyclerview recyclerview.adapter madapter; // declaring adapter recycler view recyclerview.layoutmanager mlayoutmanager; // declaring layout manager linear layout manager drawerlayout drawer; // declaring drawerlayout actionbardrawertoggle mdrawertoggle; imageview nation,standard,capital,nairobian,businessdaily,eastafrican,star,mwakilishi,citizen; string nation = "http://www.nation.co.ke"; string standard = "http://www.standardmedia.co.ke"; string star = "http://www.the-star.co.ke"; string nairobian = "http://www.sde.co.ke/m/thenairobian"; string eastafrican = "http://www.theeastafrican.co.ke"; string businessdaily = "http://www.businessdailyafrica.com"; string mwakilishi = "http://www.mwakilishi.com"; string capital = "http://www.capitalfm.co.ke"; string citizen = "http://www.theweeklycitizen.co.ke"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_news); mtoolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(mtoolbar); mrecyclerview = (recyclerview) findviewbyid(r.id.recyclerview); // assigning recyclerview object xml view mrecyclerview.sethasfixedsize(true); // letting system know list objects of fixed size madapter = new myadapter(titles,icons); // creating adapter of myadapter class(which going see in bit) // , passing titles,icons,header view name, header view email, // , header view profile picture mrecyclerview.setadapter(madapter); // setting adapter recyclerview mlayoutmanager = new linearlayoutmanager(this); // creating layout manager mrecyclerview.setlayoutmanager(mlayoutmanager); // setting layout manager nation = (imageview) findviewbyid(r.id.nation); nation.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent p = new intent(newspaper.this,browser.class); p.putextra("site",nation); startactivity(p); } }); standard = (imageview) findviewbyid(r.id.standard); standard.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent p = new intent(newspaper.this,browser.class); p.putextra("site",standard); startactivity(p); } }); star = (imageview) findviewbyid(r.id.star); star.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent p = new intent(newspaper.this,entertainment.class); p.putextra("site",star); startactivity(p); } }); capital = (imageview) findviewbyid(r.id.capital); capital.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent p = new intent(newspaper.this,browser.class); p.putextra("site",capital); startactivity(p); } }); nairobian = (imageview) findviewbyid(r.id.nairobian); nairobian.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent p = new intent(newspaper.this,browser.class); p.putextra("site",nairobian); startactivity(p); } }); citizen = (imageview) findviewbyid(r.id.citizen); citizen.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent p = new intent(newspaper.this,browser.class); p.putextra("site",citizen); startactivity(p); } }); eastafrican = (imageview) findviewbyid(r.id.eastafrican); eastafrican.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent p = new intent(newspaper.this,browser.class); p.putextra("site",eastafrican); startactivity(p); } }); businessdaily = (imageview) findviewbyid(r.id.businessdaily); businessdaily.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent p = new intent(newspaper.this,browser.class); p.putextra("site",businessdaily); startactivity(p); } }); mwakilishi = (imageview) findviewbyid(r.id.mwakilishi); mwakilishi.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent p = new intent(newspaper.this,browser.class); p.putextra("site",mwakilishi); startactivity(p); } }); drawer = (drawerlayout) findviewbyid(r.id.drawerlayout); // drawer object assigned view mdrawertoggle = new actionbardrawertoggle(this,drawer,toolbar,r.string.drawer_open,r.string.drawer_close){ @override public void ondraweropened(view drawerview) { super.ondraweropened(drawerview); // code here execute once drawer opened( dont want happened whe drawer // open not going put here) } @override public void ondrawerclosed(view drawerview) { super.ondrawerclosed(drawerview); // code here execute once drawer closed } /* @override public void ondrawerslide(view drawerview, float slideoffset) { if(slideoffset >.55 && !isdraweropen) { ondraweropened(drawerview); isdraweropen=true; } else if (slideoffset <.45 && isdraweropen) { ondrawerclosed(drawerview); isdraweropen = false; } // super.ondrawerslide(drawerview, slideoffset); }*/ }; // drawer toggle object made drawer.setdrawerlistener(mdrawertoggle); // / drawer listener set drawer toggle getsupportactionbar().setdisplayhomeasupenabled(true); mdrawertoggle.setdrawerindicatorenabled(true); } @override public void onpostcreate(bundle savedinstancestate) { super.onpostcreate(savedinstancestate); mdrawertoggle.syncstate(); } @override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); mdrawertoggle.onconfigurationchanged(newconfig); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_news, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if(mdrawertoggle.onoptionsitemselected(item)) { return true; } //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } } package com.news254.inception.newsapp; import android.support.v7.widget.recyclerview; import android.support.v7.widget.recyclerview; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.imageview; import android.widget.textview; import android.widget.toast; /** * created hp1 on 28-12-2014. */ public class myadapter extends recyclerview.adapter<myadapter.viewholder> { private static final int type_footer = 2; // declaring variable understand view being worked on // if view under inflation , population header or item private static final int type_item = 1; private static final int type_header= 0; private string mnavtitles[]; // string array store passed titles value mainactivity.java private int micons[]; // int array store passed icons resource value mainactivity.java private int mfooter[]; private string mtitlefoot[]; int holderid; // creating viewholder extends recyclerview view holder // viewholder used to store inflated views in order recycle them public static class viewholder extends recyclerview.viewholder { int holderid; textview textview; imageview imageview; imageview about; textview name; public viewholder(view itemview,int viewtype) { // creating viewholder constructor view , viewtype parameter super(itemview); // here set appropriate view in accordance the view type passed when holder object created if(viewtype == type_item) { textview = (textview) itemview.findviewbyid(r.id.rowtext); // creating textview object id of textview item_row.xml imageview = (imageview) itemview.findviewbyid(r.id.rowicon);// creating imageview object id of imageview item_row.xml holderid = 1; // setting holder id 1 object being populated of type item row } else if(viewtype == type_footer){ = (imageview) itemview.findviewbyid(r.id.image); // creating text view object header.xml name name = (textview) itemview.findviewbyid(r.id.text); // creating text view object header.xml email // creating image view object header.xml profile pic holderid =2; // setting holder id = 0 object being populated of type header view } else if(viewtype == type_header) { holderid =0; } } } myadapter(string titles[],int icons[]){ // myadapter constructor titles , icons parameter // titles, icons, name, email, profile pic passed main activity mnavtitles = titles; //have seen earlier micons = icons; //here assign passed values values declared here //in adapter } //below first ovverride method oncreateviewholder called when viewholder //created, in method inflate item_row.xml layout if viewtype type_item or else inflate header.xml // if viewtype type_header // , pass view holder @override public myadapter.viewholder oncreateviewholder(viewgroup parent, int viewtype) { if (viewtype == type_item) { view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.listview,parent,false); //inflating layout viewholder vhitem = new viewholder(v,viewtype); //creating viewholder , passing object of type view return vhitem; // returning created object //inflate layout , pass view holder } else if (viewtype == type_header) { view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.header, parent, false); //inflating layout viewholder vhheader = new viewholder(v, viewtype); //creating viewholder , passing object of type view return vhheader; //returning object created } else if (viewtype == type_footer) { view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.footer, parent, false); //inflating layout viewholder vhheader = new viewholder(v, viewtype); //creating viewholder , passing object of type view return vhheader; //returning object created } return null; } //next override method called when item in row needed displayed, here int position // tells item @ position being constructed displayed , holder id of holder object tell // view type being created 1 item row @override public void onbindviewholder(myadapter.viewholder holder, int position) { if(holder.holderid ==1) { // list view going called after header view decrement // position 1 , pass holder while setting text , image holder.textview.settext(mnavtitles[position-2]); // setting text array of our titles holder.imageview.setimageresource(micons[position-2]);// settimg image array of our icons } else if (holder.holderid ==2){ holder.about.setimageresource(micons[position]); // set resources header view holder.name.settext(mnavtitles[position]); } else if (holder.holderid ==0){ } } // method returns number of items present in list @override public int getitemcount() { return mnavtitles.length+2; // number of items in list +1 titles including header view. } // witht following method check type of view being passed @override public int getitemviewtype(int position) { if (position == 0) { return type_header; } else if (position ==2) { return type_footer ; } else { return type_item; } } private boolean ispositionheader(int position) { return position == 0; } }
change this
@override public int getitemviewtype(int position) { if (position == 0) { return type_header; } else if (position ==2) { return type_footer ; } else { return type_item; } }
to
@override public int getitemviewtype(int position) { if(position==0){ return type_header; } else if((mnavtitles.length+ 1) == position){ return type_footer; }else{ return type_item; } }
Comments
Post a Comment