android - RecyclerView content not using full width of fragment parent -
so have recyclerview within fragment , when content loaded recyclerview not occupy full width of screen. weird thing using code project practically same , there no issues.
the fragment layout this
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/windowbackground" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="com.dishesteam.dishes.activities.homeactivity"> <android.support.design.widget.coordinatorlayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/coordinator"> <android.support.v4.widget.swiperefreshlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/swipecontainer" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingtop="4dp" android:paddingbottom="4dp" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <android.support.v7.widget.recyclerview android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" android:id="@+id/latest_recycler"> </android.support.v7.widget.recyclerview> </android.support.v4.widget.swiperefreshlayout> <android.support.design.widget.floatingactionbutton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/add_dish_fab" app:srccompat="@drawable/ic_add_black_24dp" app:fabsize="normal" app:backgroundtint="@color/coloraccent" android:clickable="true" android:layout_margin="16dp" android:layout_gravity="bottom|end" android:layout_alignparentbottom="true" android:layout_alignparentright="true" android:layout_alignparentend="true"/> </android.support.design.widget.coordinatorlayout> <imageview android:id="@+id/trending_img" android:layout_width="wrap_content" android:layout_height="wrap_content" app:srccompat="@drawable/ic_trending_up_black_24dp" android:layout_centerinparent="true"/> <textview android:layout_width="wrap_content" android:layout_height="20dp" android:text="nothing here @ moment" android:textsize="16sp" android:id="@+id/empty_text" android:layout_below="@+id/trending_img" android:layout_centerhorizontal="true" android:textcolor="@color/colordarkgrey" />
the layout individual items of recyceler is
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.cardview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/textcolorprimary" app:cardcornerradius="4dp" android:layout_marginstart="8dp" android:layout_marginend="8dp" android:layout_margintop="4dp" android:layout_marginbottom="4dp"> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp" android:orientation="vertical"> <relativelayout android:layout_width="match_parent" android:layout_height="wrap_content"> <de.hdodenhof.circleimageview.circleimageview android:layout_width="40dp" android:layout_height="40dp" android:id="@+id/owner_image" app:srccompat="@drawable/ic_account_circle_black_24dp" android:layout_alignparentstart="true" android:layout_alignparentleft="true"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/owner_text" android:textsize="14sp" android:fontfamily="sans-serif" android:layout_torightof="@+id/owner_image" android:layout_marginleft="3dp" android:layout_marginright="3dp"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/post_date" android:textsize="14sp" android:fontfamily="sans-serif" android:layout_alignparentend="true" android:layout_alignparentright="true"/> </relativelayout> <imageview android:layout_width="match_parent" android:layout_height="wrap_content" app:srccompat="@drawable/ic_cake_black_24dp" android:id="@+id/first_dish_image"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/dish_name" android:textsize="16sp" android:layout_marginbottom="4dp" android:fontfamily="sans-serif"/> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/dish_description" android:textsize="14sp" android:layout_marginbottom="8dp" android:fontfamily="sans-serif"/> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightsum="3"> <linearlayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="end" android:orientation="vertical" android:layout_weight="1"> <android.support.v7.widget.appcompatimagebutton android:layout_width="wrap_content" android:layout_height="20dp" android:layout_gravity="center" app:srccompat="@drawable/ic_favorite_black_24dp" android:background="@android:color/transparent" android:id="@+id/likes_btn" android:layout_marginbottom="2dp"/> <textview android:layout_width="wrap_content" android:layout_height="20dp" android:text="favourite" android:textsize="14sp" android:layout_gravity="center" android:id="@+id/likes_btn_text" android:textcolor="@color/colordarkgrey" /> </linearlayout> <linearlayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="end" android:orientation="vertical" android:layout_weight="1"> <android.support.v7.widget.appcompatimagebutton android:layout_width="wrap_content" android:layout_height="20dp" android:layout_gravity="center" app:srccompat="@drawable/ic_share_black_24dp" android:background="@android:color/transparent" android:id="@+id/share_btn" android:layout_marginbottom="2dp"/> <textview android:layout_width="wrap_content" android:layout_height="20dp" android:text="share" android:textsize="14sp" android:layout_gravity="center" android:id="@+id/share_btn_text" android:textcolor="@color/colordarkgrey" /> </linearlayout> <linearlayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="end" android:orientation="vertical" android:layout_weight="1"> <android.support.v7.widget.appcompatimagebutton android:layout_width="wrap_content" android:layout_height="20dp" android:layout_gravity="center" app:srccompat="@drawable/ic_chat_bubble_black_24dp" android:background="@android:color/transparent" android:id="@+id/comment_btn" android:layout_marginbottom="2dp"/> <textview android:layout_width="wrap_content" android:layout_height="20dp" android:text="comment" android:textsize="14sp" android:layout_gravity="center" android:id="@+id/comment_btn_text" android:textcolor="@color/colordarkgrey" /> </linearlayout> </linearlayout> </linearlayout>
and create view method in fragment following, refactoring code use butterknife view initilization , events.
@bindview(r.id.empty_text) textview empty_text; @bindview(r.id.trending_img) imageview trending_img; private floatingactionbutton add_dish_fab; private latestdishesadapter latestdishesadapter; private recyclerview recycler_view; private arraylist<dishserver> dishes_list = new arraylist<>(); private swiperefreshlayout swipecontainer; private sharedpreferences sharedpreferences; private eventbus eventbus = eventbus.getdefault(); public static latestfragment newinstance() { return new latestfragment(); } private subscription subscription; @override public void oncreate(bundle savedinstancestate) { sharedpreferences = preferencemanager.getdefaultsharedpreferences(getactivity()); super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment super.oncreateview(inflater, container, savedinstancestate); view view = inflater.inflate(r.layout.fragment_latest, container, false); butterknife.bind(this, view); add_dish_fab = (floatingactionbutton) view.findviewbyid(r.id.add_dish_fab); add_dish_fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent add_dish_activity = new intent( getactivity(), adddishactivity.class); startactivity(add_dish_activity); } }); recycler_view = (recyclerview) view.findviewbyid(r.id.latest_recycler); recycler_view.sethasfixedsize(true); linearlayoutmanager linearlayoutmanager = new linearlayoutmanager(getactivity()); recycler_view.setlayoutmanager(linearlayoutmanager); endlessrecyclerviewscrolllistener scrolllistener = new endlessrecyclerviewscrolllistener(linearlayoutmanager) { @override public void onloadmore(int page, int totalitemscount, recyclerview view) { // triggered when new data needs appended list // add whatever code needed append new items bottom of list swipecontainer.setrefreshing(true); getlatest(totalitemscount+15); } }; recycler_view.addonscrolllistener(scrolllistener); latestdishesadapter = new latestdishesadapter(dishes_list, new latestdishclicklistener() { @override public void onshareclick(dishserver dishserver) { } @override public void oncommentclick(dishserver dishserver) { intent intent = new intent(getactivity(), dishactivity.class); intent.putextra("dish", dishserver.getdish()); intent.putextra("show_comments", "show_comments"); startactivity(intent); } @override public void onlikeclick(dishserver dishserver) { addfav(dishserver.getdish().get_id()); } @override public void onviewdishclick() { } @override public void onprofileclick() { } }); recycler_view.setadapter(latestdishesadapter); swipecontainer = (swiperefreshlayout) view.findviewbyid(r.id.swipecontainer); swipecontainer.setonrefreshlistener(new swiperefreshlayout.onrefreshlistener() { @override public void onrefresh() { getlatest(15); } }); return view; } @override public void onviewcreated(view view, @nullable bundle savedinstancestate) { getlatest(15); super.onviewcreated(view, savedinstancestate); }
this result not occupy full width of screen
my adapter code
private list<dishserver> content = new arraylist<>(); private latestdishclicklistener latestdishclicklistener; public latestdishesadapter(list<dishserver> content, latestdishclicklistener latestdishclicklistener) { this.content = content; this.latestdishclicklistener = latestdishclicklistener; } @override public latestdishesviewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.dish_item_layout, null); return new latestdishesviewholder(view); } public void setlatestdishes_list(@nullable list<dishserver> dishes) { if(dishes==null){ return; } this.content.clear(); this.content.addall(dishes); notifydatasetchanged(); } public void addtodishes(dishserver dishserver){ if(dishserver==null){ return; } content.add(dishserver); notifydatasetchanged(); } @override public void onbindviewholder(latestdishesviewholder holder, int position) { dishserver object = content.get(position); holder.bind(object, this.latestdishclicklistener, position); } @override public int getitemcount() { return content.size(); }
}
viewholder code -
private circleimageview owner_image; private textview owner_text, post_date, dish_name, dish_description; private imageview dish_first_image; private appcompatimagebutton likes_btn, share_btn, comments_btn; public latestdishesviewholder(view itemview) { super(itemview); owner_image = (circleimageview) itemview.findviewbyid(r.id.owner_image); owner_text = (textview) itemview.findviewbyid(r.id.owner_text); post_date = (textview) itemview.findviewbyid(r.id.post_date); dish_name = (textview)itemview.findviewbyid(r.id.dish_name); dish_description = (textview) itemview.findviewbyid(r.id.dish_description); dish_first_image = (imageview) itemview.findviewbyid(r.id.first_dish_image); likes_btn = (appcompatimagebutton) itemview.findviewbyid(r.id.likes_btn); share_btn = (appcompatimagebutton) itemview.findviewbyid(r.id.share_btn); comments_btn = (appcompatimagebutton) itemview.findviewbyid(r.id.comment_btn); } public void bind(final dishserver dishserver, final latestdishclicklistener latestdishclicklistener, int position){ if(dishserver.getowner().getimage_url()!=null&&!dishserver.getowner().getimage_url().isempty()){ //handle user image glide.with(itemview.getcontext()) .load(r.string.image_base_url + dishserver.getowner().getimage_url()) .placeholder(r.drawable.ic_account_circle_black_24dp) .into(this.owner_image); } if(!dishserver.getowner().getfirstname().isempty()){ owner_text.settext(dishserver.getowner().getfirstname() +" "+dishserver.getowner().getlastname()); } dish_name.settext(dishserver.getdish().getname()); dish_description.settext(dishserver.getdish().getdescription()); if(!dishserver.getdish().getsteps().get(0).getimage().isempty()){ //handle user image glide.with(itemview.getcontext()) .load(r.string.image_base_url + dishserver.getdish().getsteps().get(0).getimage()) .centercrop() .placeholder(r.drawable.ic_cake_black_24dp) .into(this.dish_first_image); } likes_btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { latestdishclicklistener.onlikeclick(dishserver); } }); share_btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { latestdishclicklistener.onshareclick(dishserver); } }); comments_btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { latestdishclicklistener.oncommentclick(dishserver); } }); }
the problem in oncreateviewholder
@override public latestdishesviewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.dish_item_layout, null); return new latestdishesviewholder(view); }
it important here not supply true, supply parent:
view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.dish_item_layout, parent, false);
supplying parent
view lets inflater know layoutparams
use
Comments
Post a Comment