android - Activity doesn't show RecyclerView - Databinding -
i tried databinding recyclerview, main_activity doesn't show recyclerview content objects. dont know point of failure is. advice.
here simple code
class: user
public class user { private string name; private int age; public user(string name, int age) { this.name = name; this.age = age; } public string getname() { return name; } public void setname(string name) { this.name = name; } public int getage() { return age; } public void setage(int age) { this.age = age; } }
class: useradapter
public class useradapter extends recyclerview.adapter<recyclerview.viewholder> { private activity host; private arraylist<user> items; public useradapter(activity activity, arraylist<user> items) { this.host = activity; this.items = items; log.i("useradaptername", items.get(0).getname().tostring()); //log shows gordon } @override public useradapter.itemviewholder oncreateviewholder(viewgroup parent, int viewtype) { // inflate layout , retrieve binding listitemsbinding binding = databindingutil.inflate(host.getlayoutinflater(), r.layout.list_items, parent, false);; return new itemviewholder(binding); } @override public void onbindviewholder(recyclerview.viewholder holder, int position) { user item = items.get(position); itemviewholder itemviewholder = (itemviewholder)holder; itemviewholder.binditem(item); } @override public int getitemcount() { return items.size(); } static class itemviewholder extends recyclerview.viewholder { listitemsbinding binding; itemviewholder(listitemsbinding binding) { super(binding.getroot()); this.binding = binding; } void binditem(user item) { binding.setuser(item); binding.executependingbindings(); } } }
class: mainactivity
public class mainactivity extends appcompatactivity { private arraylist<user> userlist = new arraylist<user>(); private activitymainbinding binding; private useradapter adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); binding = databindingutil.setcontentview(this, r.layout.activity_main); filldata(); adapter = new useradapter(mainactivity.this,userlist); binding.recycler.setadapter(adapter); } private void filldata() { userlist.add(new user("gordon", 26)); userlist.add(new user("dick", 34)); userlist.add(new user("ivan",20)); } }
layout: activity_main
<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <relativelayout android:layout_width="match_parent" android:layout_height="match_parent"> <textview android:layout_width="match_parent" android:layout_height="match_parent" android:text="activity text"/> <android.support.v7.widget.recyclerview android:id="@+id/recycler" android:layout_width="match_parent" android:layout_height="wrap_content" /> </relativelayout>
layout: list_items
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> <data> <variable name="user" type="de.bp2go.recgo.user" /> </data> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{user.name}" android:textappearance="?android:attr/textappearancemedium" android:textcolor="@android:color/black" tools:text="name" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{user.age}" android:textcolor="@android:color/black" tools:text="age" /> </linearlayout> </layout>
for me looks forgot set proper layoutmanager recyclerview.
missing line vertical list:
binding.recycler.setlayoutmanager(new linearlayoutmanager(this));
see example: recyclerview
Comments
Post a Comment