I have Main Activity with navigation drawer
and container
,navigation drawer
has the list of options.the drawer option replaces container
with fragments
.container
loads tab
layout fragment
when i click on fragtab option,but when i try to load it second time by clicking on same options, it is not showing tab
content.
fragment xml layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardBackgroundColor="@android:color/white"
card_view:cardCornerRadius="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/linearLayout_search"
android:layout_width="match_parent"
android:layout_height="48dp">
<ImageView
android:id="@+id/image_search_back"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:padding="12dp"
android:src="@mipmap/ic_search_black_24dp" />
<EditText
android:id="@+id/edit_text_search"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff"
android:gravity="center_vertical"
android:hint="Search"
android:imeOptions="actionSearch"
android:inputType="textCapWords"
android:paddingLeft="12dp"
android:paddingRight="8dp"
android:singleLine="true"
android:textColor="@color/colorAccent"
android:textColorHint="@color/colorAccent" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:tabGravity="fill"
app:tabIndicatorColor="@android:color/white"
app:tabMode="fixed"
app:tabSelectedTextColor="#F4F1F1"
app:tabTextColor="@android:color/white" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/add_icon_white" />
Fragment class FragTabs.java
public class FragTabs extends Fragment {
public FragTabs() {
// Required empty public constructor
}
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_frag_tabs, container, false);
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
//tabLayout.setupWithViewPager(viewPager);
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (viewPager.getCurrentItem() == 0) {
getFragmentManager().beginTransaction().replace(R.id.profile_container, new Addplaymates()).commit();
} else {
getFragmentManager().beginTransaction().replace(R.id.profile_container, new Addplaygroups()).commit();
}
}
});
return view;
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
CharSequence Titles[] = {"Frag A", "Frag B"};
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
Fragment f = null;
switch (position) {
case 0:
FragA tab1 = new FragA();
f = tab1;
break;
case 1:
FragB tab2 = new FragB();
f = tab2;
break;
}
return f;
}
@Override
public int getCount() {
return 2;
}
/*public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}*/
@Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}
}}
Main Activity xml
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/conta"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<FrameLayout
android:id="@+id/profile_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/app_bar" />
</RelativeLayout>
<fragment
android:id="@+id/navigation_drawer"
android:name="com.kshediv.playforever.Profile.NaviagtionFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_naviagtion"
tools:layout="@layout/fragment_naviagtion" />
</android.support.v4.widget.DrawerLayout>
navigation drawer.
public class NaviagtionFragment extends Fragment {
public NaviagtionFragment() {
// Required empty public constructor
}
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
public void setUp(DrawerLayout dl, Toolbar toolbar) {
mDrawerLayout = dl;
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), dl, toolbar, R.string.DrawerOpn, R.string.DrawerClose) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActivity().invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActivity().invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_naviagtion, container, false);
FrameLayout home = (FrameLayout) view.findViewById(R.id.home);
home.setOnClickListener(onClickListener);
FrameLayout fragtabs = (FrameLayout) view.findViewById(R.id.fragtabs);
playmates.setOnClickListener(onClickListener);
FrameLayout settings = (FrameLayout) view.findViewById(R.id.settings);
playmates.setOnClickListener(onClickListener);
return view;
}
public View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.home:
getFragmentManager().beginTransaction().replace(R.id.profile_container, new Home()).commit();
mDrawerLayout.closeDrawers();
break;
case R.id.playmates:
getFragmentManager().beginTransaction().replace(R.id.profile_container, new PlayTabs()).commit();
mDrawerLayout.closeDrawers();
break;
case R.id.performance:
getFragmentManager().beginTransaction().replace(R.id.profile_container, new Settings()).commit();
mDrawerLayout.closeDrawers();
break;
}
}
};}
Main Activity
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
Toolbar toolBar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolBar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NaviagtionFragment nav_frg = (NaviagtionFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
nav_frg.setUp((DrawerLayout) findViewById(R.id.drawer_layout), toolBar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_profile, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}}
Thank you,
Try using
class ViewPagerAdapter extends FragmentStatePagerAdapter {
...
}
instead of
class ViewPagerAdapter extends FragmentPagerAdapter {
...
}
for viewpager Adapter implements FragmentStatePagerAdapter instead of FragmentPagerAdapter
it will load fragments in tab layout without any problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With