android - Sliding Tabs With RecyclerView -
i want display text , images in sliding tabs using recyclerview. think have setup correctly point , think last piece of puzzle layoutmanager gives me error under oncreateview made variable not sure how go on further.
this how must @ end
main activity
public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); viewpager pager = (viewpager) findviewbyid(r.id.viewpager); pager.setadapter(new mypageradapter(getsupportfragmentmanager())); pagerslidingtabstrip tabs = (pagerslidingtabstrip) findviewbyid(r.id.tabs); tabs.setbackgroundcolor(getresources().getcolor(r.color.colorprimarydark)); tabs.setindicatorcolor(getresources().getcolor(r.color.coloraccent)); tabs.setdividercolor(getresources().getcolor(r.color.colorprimary)); tabs.settextcolor(getresources().getcolor(r.color.colortest)); tabs.setviewpager(pager); } class mypageradapter extends fragmentpageradapter { string[] adapter_one_tabs; public mypageradapter(fragmentmanager fm) { super(fm); adapter_one_tabs = getresources().getstringarray(r.array.adapter_one_tabs); } @override public fragment getitem(int position) { return myfragment.getinstance(position); } @override public charsequence getpagetitle(int position) { return adapter_one_tabs[position]; } @override public int getcount() { return 4; } } public static class myfragment extends fragment { public static final string tab_position_key = "tab_position_key"; private recyclerview mrecyclerview; private recyclerview.layoutmanager mlayoutmanager; private recyclerview.adapter madapter; public static myfragment getinstance(int position) { myfragment myfragment = new myfragment(); bundle args = new bundle(); args.putint(tab_position_key, position); myfragment.setarguments(args); return myfragment; } @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { view layout = inflater.inflate(r.layout.recyclerview_fragment, container, false); bundle bundle = getarguments(); int position = bundle.getint(tab_position_key); mrecyclerview = (recyclerview) layout.findviewbyid(r.id.recycler_view); switch (position) { case 0: madapter = new adapterone(); mrecyclerview.setadapter(madapter); break; case 1: madapter = new adaptertwo(); mrecyclerview.setadapter(madapter); break; case 2: madapter = new adapterthree(); mrecyclerview.setadapter(madapter); break; case 3: madapter = new adapterfour(); mrecyclerview.setadapter(madapter); break; } return layout; } } }
adapterone
public class adapterone extends recyclerview.adapter<adapterone.viewholder> { list<adapterdata> mitems; public adapterone() { super(); mitems = new arraylist<adapterdata>(); adapterdata data = new adapterdata(); data.setthumbnail(r.drawable.afghanistan); data.setname("afghanistan"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.albania); data.setname("albania"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.algeria); data.setname("algeria"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.american_samoa); data.setname("american samoa"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.andorra); data.setname("andorra"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.angola); data.setname("angola"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.anguilla); data.setname("anguilla"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.antigua_and_barbuda); data.setname("antigua , barbuda"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.argentina); data.setname("argentina"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.armenia); data.setname("armenia"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.aruba); data.setname("aruba"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.australia); data.setname("australia"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.austria); data.setname("austria"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.azerbaijan); data.setname("azerbaijan"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.bahamas); data.setname("bahamas"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.bahrain); data.setname("bahrain"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.bangladesh); data.setname("bangladesh"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.barbados); data.setname("barbados"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.belarus); data.setname("belarus"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.belgium); data.setname("belgium"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.belize); data.setname("belize"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.benin); data.setname("benin"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.bermuda); data.setname("bermuda"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.bhutan); data.setname("bhutan"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.bolivia); data.setname("bolivia"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.bosnia_and_herzegovina); data.setname("bosnia , herzegovina"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.botswana); data.setname("botswana"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.brazil); data.setname("brazil"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.british_virgin_islands); data.setname("british virgin islands"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.brunei); data.setname("brunei"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.bulgaria); data.setname("bulgaria"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.burkina_faso); data.setname("burkina faso"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.burundi); data.setname("burundi"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.cambodia); data.setname("cambodia"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.cameroon); data.setname("cameroon"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.canada); data.setname("canada"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.cape_verde); data.setname("cape verde"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.cayman_islands); data.setname("cayman islands"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.central_african_republic); data.setname("central african republic"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.chad); data.setname("chad"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.chile); data.setname("chile"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.china); data.setname("china"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.colombia); data.setname("colombia"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.comoros); data.setname("comoros"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.cook_islands); data.setname("cook islands"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.costa_rica); data.setname("costa rica"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.cote_divoire); data.setname("cote d'ivoire"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.croatia); data.setname("croatia"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.cuba); data.setname("cuba"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.cayman_islands); data.setname("cayman island"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.curacao); data.setname("curacao"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.cyprus); data.setname("cyprus"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.czech_republic); data.setname("czech republic"); mitems.add(data); data = new adapterdata(); data.setthumbnail(r.drawable.democratic_republic_of_the_congo); data.setname("democratic republic of congo"); mitems.add(data); } @override public viewholder oncreateviewholder(viewgroup viewgroup, int i) { view v = layoutinflater.from(viewgroup.getcontext()) .inflate(r.layout.recycler_view_card_item, viewgroup, false); return new viewholder(v); } @override public void onbindviewholder(viewholder viewholder, int i) { adapterdata movie = mitems.get(i); viewholder.natthumbnail.setimageresource(movie.getthumbnail()); viewholder.natname.settext(movie.getname()); } @override public int getitemcount() { return mitems.size(); } class viewholder extends recyclerview.viewholder{ public imageview natthumbnail; public textview natname; public viewholder(view itemview) { super(itemview); natthumbnail = (imageview)itemview.findviewbyid(r.id.layoutthumbnail); natname = (textview)itemview.findviewbyid(r.id.layoutname); } } }
activity_main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:ads="http://schemas.android.com/apk/res-auto" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@color/colorbackground" android:orientation="vertical" tools:context=".mainactivity"> <android.support.v7.widget.toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:background="@color/colorprimary" android:minheight="?attr/actionbarsize" > </android.support.v7.widget.toolbar> <com.astuetz.pagerslidingtabstrip android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="48dip" /> <android.support.v4.view.viewpager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="0dp" /> </linearlayout>
recyclerview_fragment.xml
<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:orientation="vertical" tools:context=".mainactivity"> <android.support.v7.widget.recyclerview android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent"/> </linearlayout>
recycler_view_card_item.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.cardview android:layout_width="match_parent" android:layout_height="wrap_content" android:foreground="?android:attr/selectableitembackground" card_view:cardcornerradius="3dp" card_view:cardusecompatpadding="true"> <relativelayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ddd"> <imageview android:id="@+id/layoutthumbnail" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginleft="10dp" android:layout_marginright="10dp" android:src="@drawable/afghanistan" android:gravity="center_vertical" android:layout_centerhorizontal="true" /> <textview android:id="@+id/layoutname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="country name" android:textsize="@dimen/abc_text_size_body_1_material" android:layout_below="@id/layoutthumbnail" android:layout_centerinparent="true" /> </relativelayout> </android.support.v7.widget.cardview> </linearlayout>
all right error :
the log no suitable constructor found gridlayoutmanager(myfragment, int)
.
the available constructor gridlayoutmanager :
// constructor used when layout manager set in xml recyclerview attribute "layoutmanager". gridlayoutmanager(context context, attributeset attrs, int defstyleattr, int defstyleres) // creates vertical gridlayoutmanager gridlayoutmanager(context context, int spancount) gridlayoutmanager(context context, int spancount, int orientation, boolean reverselayout)
as can see first parameter context
not fragment
:)
so change this
context :
mlayoutmanager = new gridlayoutmanager(getactivity(), 3); mrecyclerview.setlayoutmanager(mlayoutmanager);
and work :)
edit
i put code lines can put :
public static class myfragment extends fragment { ... @override public void oncreate (bundle savedinstancestate) { super.oncreate(savedinstancestate); // here initialize grid layout manager mlayoutmanager = new gridlayoutmanager(getactivity(), 3); } @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { view layout = inflater.inflate(r.layout.recyclerview_fragment, container, false); bundle bundle = getarguments(); int position = bundle.getint(tab_position_key); mrecyclerview = (recyclerview) layout.findviewbyid(r.id.recycler_view); switch (position) { ... } // here set recycler view mrecyclerview.setlayoutmanager(mlayoutmanager); return layout; } }
Comments
Post a Comment