java - RecyclerView with ToolBar -
i'm new material design , every time try use recyclerview every thing gose wrong..
can 1 know problem ?
main activity class
import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.support.v7.widget.toolbar; import android.view.menu; import android.view.menuitem; public class mainactivity extends actionbaractivity { toolbar toolbar; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar= (toolbar) findviewbyid(r.id.toool); setsupportactionbar(toolbar); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, 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(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
main activity layout
<relativelayout 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:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <include android:id="@+id/toool" layout="@layout/app_tool" /> <fragment android:id="@+id/fragment" android:name="com.example.hothyfa.recyclerandmore.recycler" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/toool" android:layout_centerhorizontal="true" tools:layout="@layout/rere" /> </relativelayout>
recycler fragment
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v7.widget.recyclerview android:id="@+id/recye" android:layout_width="match_parent" android:layout_height="wrap_content" > </android.support.v7.widget.recyclerview> </relativelayout>
recycler class
import android.os.bundle; import android.support.annotation.nullable; import android.support.v4.app.fragment; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import java.util.arraylist; import java.util.list; public class recycler extends fragment { private recyclerview recyclerview; private myrecyeadapter adapter; private view view; @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { view = inflater.inflate(r.layout.rere, container, false); recyclerview = (recyclerview) getactivity().findviewbyid(r.id.recye); adapter = new myrecyeadapter(getactivity(), getdata()); recyclerview.setadapter(adapter); recyclerview.setlayoutmanager(new linearlayoutmanager(getactivity())); return view; } public static list<info> getdata() { list<info> data = new arraylist<>(); int photos[] = { r.drawable.images, r.drawable.dreamsmal, r.drawable.images, r.drawable.dreamsmal }; string users[] = { "hothyfa", "marwan", "jamal", "alaa" }; (int = 0; < photos.length && < users.length; i++) { info current = new info(); current.iconid = photos[i]; current.names = users[i]; data.add(current); } return data; } }
recyclerview adapter class
import android.content.context; 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 java.util.collections; import java.util.list; public class myrecyeadapter extends recyclerview.adapter<myrecyeadapter.myholder> { list<info> data = collections.emptylist(); private layoutinflater inflater; public myrecyeadapter(context context, list<info> data) { inflater = layoutinflater.from(context); this.data = data; } @override public myholder oncreateviewholder(viewgroup parent, int viewtype) { view view = inflater.inflate(r.layout.custom, parent, false); myholder holder = new myholder(view); return holder; } @override public void onbindviewholder(myholder holder, int position) { info current = data.get(position); holder.imageprofile.setimageresource(current.iconid); holder.usersname.settext(current.names); } @override public int getitemcount() { return data.size(); } public class myholder extends recyclerview.viewholder { imageview imageprofile; textview usersname; public myholder(view itemview) { super(itemview); imageprofile = (imageview) itemview.findviewbyid(r.id.imageview); usersname = (textview) itemview.findviewbyid(r.id.textview); } } }
custom row layout (for recyclerview)
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <imageview android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:padding="8dp" android:src="@drawable/images" /> <textview android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:padding="8dp" android:text="large text" /> </linearlayout>
and data class recycler
public class info { int iconid; string names; }
toolbar layout
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/primarycolor" > </android.support.v7.widget.toolbar>
note: have put in build.gradle
compile 'com.android.support:recyclerview-v7:22.2.1'
and rebuild app.
error:
java.lang.runtimeexception: unable start activity componentinfo{com.example.hothyfa.recyclerandmore/com.example.hothyfa.recyclerandmore.mainactivity}: android.view.inflateexception: binary xml file line #19: error inflating class fragment @ app.activitythread.performlaunchactivity(activitythread.java:2325) @ \\\\\\\android.app.activitythread.handlelaunchactivity(activitythread.java:2390) @ android.app.activitythread.access$800(activitythread.java:151) @ android.app.activitythread$h.handlemessage(activitythread.java:1303) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5257) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698) caused by: android.view.inflateexception: binary xml file line #19: error inflating class fragment @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:763) @ android.view.layoutinflater.rinflate(layoutinflater.java:806) @ android.view.layoutinflater.inflate(layoutinflater.java:504) @ android.view.layoutinflater.inflate(layoutinflater.java:414) @ android.view.layoutinflater.inflate(layoutinflater.java:365) @ android.support.v7.app.appcompatdelegateimplv7.setcontentview(appcompatdelegateimplv7.java:249) @ android.support.v7.app.appcompatactivity.setcontentview(appcompatactivity.java:106) @ com.example.hothyfa.recyclerandmore.mainactivity.oncreate(mainactivity.java:19) @ android.app.activity.performcreate(activity.java:5990) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1106) @ android.app.activitythread.performlaunchactivity(activitythread.java:2278)
ok, found issue, in oncreateview
of recycler
recyclerview= (recyclerview) getactivity().findviewbyid(r.id.recye);
change to:
recyclerview= (recyclerview) view.findviewbyid(r.id.recye);
Comments
Post a Comment