java - How to create a Tabbed Activity with ListViews - Android -
first of all, i'm beginner in android programming. i'm trying program 3 tabs each listview inside (you know e.g. whatsapp). android studio makes easy automatically create tabbed activity. question is: how can implement listview each tab? there nice tutorial on http://www.androidhive.info/2012/05/android-combining-tab-layout-and-list-view/ uses tabactivity. method deprecated , fragments should used instead. have extended main_fragment.xml (which created android studio) listview. correct way set corresponding list adapters , set them? setting them listview list_all = (listview) findviewbyid(r.id.listviewall)
in oncreate()
not work because of null object reference error. didn't find out how use rootview
returned oncreateview(layoutinflater inflater, viewgroup container,bundle savedinstancestate)
how can solve problem?
main_fragment.xml:
<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:background="@color/coloraccent" android:paddingtop="@dimen/activity_vertical_margin"> <listview android:id="@+id/listviewall" android:layout_width="match_parent" android:layout_height="match_parent"></listview> </relativelayout>
main.xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.coordinatorlayout 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" android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true" android:background="@color/white" tools:context="de.url.members"> <android.support.design.widget.appbarlayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingtop="@dimen/appbar_padding_top" android:background="@color/bg_login_dark" android:theme="@style/apptheme.appbaroverlay"> <android.support.v7.widget.toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionbarsize" android:background="@color/bg_login_dark" app:layout_scrollflags="scroll|enteralways" app:popuptheme="@style/apptheme.popupoverlay"> </android.support.v7.widget.toolbar> <android.support.design.widget.tablayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabmaxwidth="0dp" app:tabgravity="fill" app:tabmode="fixed" android:fillviewport="false" /> </android.support.design.widget.appbarlayout> <android.support.v4.view.viewpager android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> <android.support.design.widget.floatingactionbutton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end|bottom" android:layout_margin="@dimen/fab_margin" android:src="@android:drawable/ic_dialog_email" /> </android.support.design.widget.coordinatorlayout>
main.java:
public class main extends appcompatactivity { /** * {@link android.support.v4.view.pageradapter} provide * fragments each of sections. use * {@link fragmentpageradapter} derivative, keep every * loaded fragment in memory. if becomes memory intensive, * may best switch * {@link android.support.v4.app.fragmentstatepageradapter}. */ private sectionspageradapter msectionspageradapter; // {@link viewpager} host section contents. private viewpager mviewpager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); // create adapter return fragment each of 3 // primary sections of activity. msectionspageradapter = new sectionspageradapter(getsupportfragmentmanager()); // set viewpager sections adapter. mviewpager = (viewpager) findviewbyid(r.id.container); mviewpager.setadapter(msectionspageradapter); tablayout tablayout = (tablayout) findviewbyid(r.id.tabs); tablayout.setupwithviewpager(mviewpager); floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { // action here } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_members, 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.logout) { //action here } return super.onoptionsitemselected(item); } /** * placeholder fragment containing simple view. */ public static class placeholderfragment extends fragment { /** * fragment argument representing section number * fragment. */ private static final string arg_section_number = "section_number"; public placeholderfragment() { } /** * returns new instance of fragment given section * number. */ public static placeholderfragment newinstance(int sectionnumber) { placeholderfragment fragment = new placeholderfragment(); bundle args = new bundle(); args.putint(arg_section_number, sectionnumber); fragment.setarguments(args); return fragment; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { int sectionnumber = getarguments().getint(arg_section_number);//index of selected tab view rootview; if (sectionnumber == 1){ rootview = inflater.inflate(r.layout.main_fragment, container, false); }else if (sectionnumber == 2){ rootview = inflater.inflate(r.layout.main_fragment, container, false); }else if (sectionnumber == 3){ rootview = inflater.inflate(r.layout.main_fragment, container, false); }else{ rootview = inflater.inflate(r.layout.main_fragment, container, false); } return rootview; } } /** * {@link fragmentpageradapter} returns fragment corresponding * 1 of sections/tabs/pages. */ public class sectionspageradapter extends fragmentpageradapter { public sectionspageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { // getitem called instantiate fragment given page. // return placeholderfragment (defined static inner class below). return placeholderfragment.newinstance(position + 1); } @override public int getcount() { // show 3 total pages. return 3; } @override public charsequence getpagetitle(int position) { switch (position) { case 0: return getresources().getstring(r.string.title_section1); case 1: return getresources().getstring(r.string.title_section2); case 2: return getresources().getstring(r.string.title_section3); } return null; } } }
if want use listing tablayout , viewpager try way work you
public class mainactivity extends appcompatactivity { private static toolbar toolbar; private static viewpager viewpager; private static tablayout tablayout; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); viewpager = (viewpager) findviewbyid(r.id.viewpager); setupviewpager(viewpager); tablayout = (tablayout) findviewbyid(r.id.tablayout); tablayout.setupwithviewpager(viewpager);//setting tab on viewpager //implementing tab selected listener on tablayout tablayout.setontabselectedlistener(new tablayout.ontabselectedlistener() { @override public void ontabselected(tablayout.tab tab) { viewpager.setcurrentitem(tab.getposition());//setting current selected item on viewpager switch (tab.getposition()) { case 0: log.e("tag","tab1"); break; case 1: log.e("tag","tab2"); break; case 2: log.e("tag","tab3"); break; } } @override public void ontabunselected(tablayout.tab tab) { } @override public void ontabreselected(tablayout.tab tab) { } }); } //setting view pager private void setupviewpager(viewpager viewpager) { viewpageradapter adapter = new viewpageradapter(getsupportfragmentmanager()); adapter.addfrag(new dummyfragment("android"), "android"); adapter.addfrag(new dummyfragment("ios"), "ios"); adapter.addfrag(new dummyfragment("windows"), "windows"); viewpager.setadapter(adapter); } //view pager fragments setting adapter class class viewpageradapter extends fragmentpageradapter { private final list<fragment> mfragmentlist = new arraylist<>();//fragment arraylist private final list<string> mfragmenttitlelist = new arraylist<>();//title arraylist public viewpageradapter(fragmentmanager manager) { super(manager); } @override public fragment getitem(int position) { return mfragmentlist.get(position); } @override public int getcount() { return mfragmentlist.size(); } //adding fragments , title method public void addfrag(fragment fragment, string title) { mfragmentlist.add(fragment); mfragmenttitlelist.add(title); } @override public charsequence getpagetitle(int position) { return mfragmenttitlelist.get(position); } } }
for more see android-material-design-tabs-using-tablayout
or
check http://www.tutorialsbuzz.com/2015/10/android-sliding-tablayout-listview-whatsapp.html
and check https://github.com/codepath/android_guides/wiki/handling-scrolls-with-coordinatorlayout
output
Comments
Post a Comment