android - ViewPager and Fragment not attached -
i trying implement tab view fragment fragment didn't appear. had linked viewpager.but still didn't work.
this mainactivity
package com.example.minkhantlu.timetableapp; import android.support.design.widget.tablayout; import android.support.v4.content.contextcompat; import android.support.v4.view.viewpager; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.layoutinflater; import android.widget.tablelayout; import com.example.minkhantlu.timetableapp.fragment.viewpageradapter; public class homeactivity extends appcompatactivity { private tablayout tablayout; private viewpager viewpager; private viewpageradapter viewadapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_home); tablayout = (tablayout) findviewbyid(r.id.tab_layout); viewpager = (viewpager) findviewbyid(r.id.my_view_pager); viewadapter = new viewpageradapter(getsupportfragmentmanager()); viewpager.setadapter(viewadapter); tablayout.tab mon = tablayout.newtab(); tablayout.tab tue = tablayout.newtab(); tablayout.tab wed = tablayout.newtab(); tablayout.tab thur = tablayout.newtab(); tablayout.tab fri = tablayout.newtab(); mon.settext("mon"); tue.settext("tue"); wed.settext("wed"); thur.settext("thur"); fri.settext("fri"); tablayout.addtab(mon, 0); tablayout.addtab(tue, 1); tablayout.addtab(wed, 2); tablayout.addtab(thur,3); tablayout.addtab(fri,4); tablayout.settabtextcolors(contextcompat.getcolorstatelist(this, r.color.coloraccent)); tablayout.setselectedtabindicatorcolor(contextcompat.getcolor(this, r.color.coloraccent)); viewpager.addonpagechangelistener(new tablayout.tablayoutonpagechangelistener(tablayout)); }
}
tried viewpageradapter
package com.example.minkhantlu.timetableapp.fragment; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter; import android.util.log; public class viewpageradapter extends fragmentpageradapter { public viewpageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { return new mondayfragment(); } @override public int getcount() { return 0; }
}
my fragment
package com.example.minkhantlu.timetableapp.fragment; import android.content.context; import android.net.uri; import android.os.bundle; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import com.example.minkhantlu.timetableapp.r; public class mondayfragment extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.fragment_monday, container, false); }
} 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" tools:context="com.example.minkhantlu.timetableapp.fragment.mondayfragment"> <!-- todo: update blank fragment layout --> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:text="@string/hello_blank_fragment" /> </relativelayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <framelayout 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" tools:context="com.example.minkhantlu.timetableapp.homeactivity"> <android.support.design.widget.tablayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorprimarydark"/> <android.support.v4.view.viewpager android:id="@+id/my_view_pager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/tab_layout" /> </framelayout>
but fragment didn't appear, plz me this.
in viewpageradapter, try changing
@override public int getcount() { return 0; }
to
@override public int getcount() { return 1; }
Comments
Post a Comment