android - Getting empty RecyclerView -
it first time im working recyclerview , have problem. im trying view drawerfragment. drawer menu working fine cant see recyclerview there. can me?
this layout of fragment:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#dadada" tools:context="com.example.entwicklung1.designtestapp.navigationdrawerfragment" android:orientation="horizontal"> <linearlayout android:id="@+id/linid" android:layout_width="match_parent" android:layout_height="120dp" android:background="#0064a7" android:paddingleft="20dp"> <imageview android:layout_width="240dp" android:layout_height="120dp" android:elevation="20dp" android:gravity="center_vertical" android:src="@drawable/zanderlogo" /> </linearlayout> <android.support.v7.widget.recyclerview android:id="@+id/drawerlist" android:layout_width="match_parent" android:layout_height="wrap_content" ></android.support.v7.widget.recyclerview> </linearlayout>
here code:
public class navigationdrawerfragment extends fragment { private recyclerview recyclerview; public static final string pref_file_name = "testpref"; public static final string key_user_learned_drawner = "user_learned_drawer"; private actionbardrawertoggle mdrawertoggle; private drawerlayout mdrawerlayout; private myadapter adapter; private boolean muserlearneddrawer; private boolean mfromsavedinstancestate; private view containerview; public navigationdrawerfragment() { // required empty public constructor } public list<information> getdata(){ list<information> data = new arraylist<>(); int[] icons={r.drawable.ic_number1,r.drawable.ic_number2,r.drawable.ic_number3,r.drawable.ic_number4,r.drawable.ic_number5}; string[] titles ={"news", "produkte","homepage","kontakt","anfahrt"}; for(int i=0;i<titles.length; i++){ information current=new information(); current.iconid=icons[i]; current.title=titles[i]; data.add(current); } return data; } @override public void oncreate(@nullable bundle savedinstancestate) { super.oncreate(savedinstancestate); muserlearneddrawer = boolean.valueof(readfrompreferences(getactivity(), key_user_learned_drawner, "false")); mfromsavedinstancestate = savedinstancestate != null ? true : false; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view layout=inflater.inflate(r.layout.fragment_navigation_drawer, container, false); recyclerview=(recyclerview)layout.findviewbyid(r.id.drawerlist); adapter = new myadapter(getactivity(),getdata()); recyclerview.setlayoutmanager(new linearlayoutmanager(getactivity())); recyclerview.setadapter(adapter); return layout; } public void setup(int fragmentid, drawerlayout drawerlayout, final toolbar toolbar) { containerview=getactivity().findviewbyid(fragmentid); mdrawerlayout = drawerlayout; mdrawertoggle = new actionbardrawertoggle(getactivity(), drawerlayout, toolbar, r.string.drawer_open, r.string.drawer_close) { @override public void ondraweropened(view drawerview) { super.ondraweropened(drawerview); if(!muserlearneddrawer){ muserlearneddrawer=true; savetopreferences(getactivity(),key_user_learned_drawner,muserlearneddrawer+""); } getactivity().supportinvalidateoptionsmenu(); } @override public void ondrawerclosed(view drawerview) { super.ondrawerclosed(drawerview); getactivity().supportinvalidateoptionsmenu(); } }; mdrawerlayout.setdrawerlistener(mdrawertoggle); mdrawerlayout.post(new runnable() { @override public void run() { mdrawertoggle.syncstate(); if(!muserlearneddrawer && !mfromsavedinstancestate){ mdrawerlayout.opendrawer(containerview); } } }); } public static void savetopreferences(context context, string preferencename, string preferencevalue) { sharedpreferences sharedpreferences = context.getsharedpreferences(pref_file_name, context.mode_private); sharedpreferences.editor editor = sharedpreferences.edit(); editor.putstring(preferencename, preferencevalue); editor.apply(); } public static string readfrompreferences(context context, string preferencename, string defaultvalue) { sharedpreferences sharedpreferences = context.getsharedpreferences(pref_file_name, context.mode_private); return sharedpreferences.getstring(preferencename, defaultvalue); } }
and here recyclerview adapter:
public class myadapter extends recyclerview.adapter<myadapter.myviewholder>{ private layoutinflater inflater; list<information> data = collections.emptylist(); public myadapter(context context,list<information> data) { inflater=layoutinflater.from(context); } @override public myviewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = inflater.inflate(r.layout.custom_row,parent,false); myviewholder holder = new myviewholder(view); return holder; } @override public void onbindviewholder(myviewholder holder, int position) { information current = data.get(position); holder.title.settext(current.title); holder.icon.setimageresource(current.iconid); } @override public int getitemcount() { return data.size(); } class myviewholder extends recyclerview.viewholder { textview title; imageview icon; public myviewholder(view itemview) { super(itemview); title = (textview) itemview.findviewbyid(r.id.listtext); icon = (imageview)itemview.findviewbyid(r.id.listicon); } } }
what doing wrong?
you don't set data in adapter constructor. so, data empty.
list<information> data = collections.emptylist(); public myadapter(context context,list<information> data) { inflater=layoutinflater.from(context); this.data = data; ^^^^^^^^^^^^^^^^ add statement }
update:
need change orientation of top linearlayout "vertical"
Comments
Post a Comment