Edittext automatically gets focus when move between pages in a view pager - android

I have an activity which hosts two fragments inside a view pager. I used the same layout to inflate those fragments. The layout has two edit texts placed inside a linear layout which is inside a relative layout. The problem is when I switch from fragment A to fragment B, he first edit text has focus in fragment A and when I return back from fragment B to fragment A, instead of the first edit text having focus, the second edit text gets the focus. How to solve it. I provide the layouts and source code below. I have not return any code inside the fragment classes.
Activity:
public class LoginActivity extends BaseActivity {
public static final String selectedTabPosition = "selectedTabPosition";
//Tab tag name
private static String TAB_1_TAG = "Email";
private static String TAB_2_TAG = "Mobile";
private TabLayout mTabLayout;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
TAB_1_TAG = getResources().getString(R.string.tab_email);
TAB_2_TAG = getResources().getString(R.string.tab_mobile);
//Initialise views
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mTabLayout = (TabLayout) findViewById(R.id.tabs);
//set tab with view pager
setupViewPager(mViewPager);
mTabLayout.setupWithViewPager(mViewPager);
setupTabIcons();
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
dismissKeyboard(mViewPager);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
/**
* Adding custom view to tab
*/
private void setupTabIcons() {
LinearLayout tabOne = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.tab_custom, null);
TextView tvIconOne = (TextView) tabOne.findViewById(R.id.tv_tab_title);
tvIconOne.setText(TAB_1_TAG);
mTabLayout.getTabAt(0).setCustomView(tabOne);
setTypeface(tvIconOne, CustomFonts.Prime_regular);
mTabLayout.getTabAt(0).getCustomView().setSelected(true);
LinearLayout tabTwo = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.tab_custom, null);
TextView tvIconTwo = (TextView) tabTwo.findViewById(R.id.tv_tab_title);
tvIconTwo.setText(TAB_2_TAG);
setTypeface(tvIconTwo, CustomFonts.Prime_regular);
mTabLayout.getTabAt(1).setCustomView(tabTwo);
}
/**
* Adding fragments to ViewPager
*
* #param viewPager The view pager
*/
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
LoginFragment loginFragmentEmail = new LoginFragment();
Bundle emailBundle = new Bundle();
emailBundle.putInt(selectedTabPosition, 0);
loginFragmentEmail.setArguments(emailBundle);
LoginFragment loginFragmentMobile = new LoginFragment();
Bundle phoneBundle = new Bundle();
phoneBundle.putInt(selectedTabPosition, 1);
loginFragmentMobile.setArguments(phoneBundle);
adapter.addFrag(loginFragmentEmail, TAB_1_TAG);
adapter.addFrag(loginFragmentMobile, TAB_2_TAG);
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
Fragment:
public class LoginFragment extends BaseFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_login, container, false);
return rootView;
}
activity_login.xml:
<?xml version="1.0" encoding="utf-8"?>
<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:background="#android:color/white"
android:orientation="vertical">
<ImageView
android:id="#+id/iv_title"
android:layout_width="200dp"
android:layout_height="45dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
/>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="#dimen/custom_tab_layout_height"
android:layout_below="#+id/iv_title"
android:layout_marginTop="#dimen/spacing_10"
app:tabGravity="fill"
app:tabIndicatorColor="#color/app_color_dark"
app:tabMode="fixed"/>
<View
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="#dimen/divider_height_small"
android:layout_below="#+id/tabs"
android:background="#color/gray_medium"/>
<com.helper.CustomNonSwipeableViewpager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/view"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</RelativeLayout>
fragment_login.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spacing_50"
android:orientation="vertical">
<EditText
android:id="#+id/et_email"
android:layout_width="match_parent"
android:layout_height="#dimen/spacing_48"
/>
<EditText
android:id="#+id/et_password"
android:layout_width="match_parent"
android:layout_height="#dimen/spacing_48"
android:layout_marginTop="#dimen/spacing_15"/>
</LinearLayout>
</RelativeLayout>
AndroidManifest.xml:
<activity
android:name=".activities.LoginActivity"
android:screenOrientation="portrait"
/>
In the AndrodManifest file, tried the
android:windowSoftInputMode = stateHidden and android:windowSoftInputMode = adjustPan
CustomNonSwipeableViewpager.java:
public class CustomNonSwipeableViewpager extends ViewPager {
public CustomNonSwipeableViewpager(Context context) {
super(context);
}
public CustomNonSwipeableViewpager(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return false;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return false;
}
}
ScreenShots:
Before moving to FragmentB:
In Fragment B:
Return back to Fragment A from Fragment B:

You can disable the focus on the EditText during runtime of the fragment:
EditText et_email_view = (EditText) rootView.findViewById(R.id.et_email);
et_email_view.setFocusable(false);

Use requestFocus attribute to your first editText which will always cause that editText to gain focus.
Make following changes in your fragment_login.xml file,
<EditText
android:id="#+id/et_email"
android:layout_width="match_parent"
android:layout_height="#dimen/spacing_48">
<requestFocus />
</EditText>

For example in my case viewPager is covering editText. I set translationZ at editText(translationZ="2") and viewPage(translationZ="1") and it helped me.

Related

Replace fragment in fragment itself in TabActivity

I am sorry on the duplicate question but I didn't get answer for my problem.
I create app with TabActivity and also trying to replace one fragment from fragment itself, I read in https://developer.android.com/training/basics/fragments/fragment-ui.html how to do it and i created interface in my fragment that i want to be replace with another,
I implement the interface in my MainActivity and still when running my app it show me container itself.
here is my code:
Main Activity:
public class MainActivity extends FragmentActivity implements New2.OnReplaceFragment {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
public static MainActivity instance = null;
public static MainActivity getInstance(){
return instance;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.frame_container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.getTabAt(0).setIcon(R.drawable.icon_info);
tabLayout.getTabAt(1).setIcon(R.drawable.icon_heart_rate_sensor_jpg);
tabLayout.getTabAt(2).setIcon(R.drawable.icon_graph_jpg);
instance = this;
}
#Override
public void onReplaceFragment(Class fragmentClass) {
Fragment fragment = null;
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container,fragment);
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch (position) {
case 0:
// New1 tab1 = new New1();
return New1.newInstance();
case 1:
return New2.newInstance();
case 2:
return New3.newInstance();
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
}
return null;
}
}
}
my fragment that I want to replace
Fragment:
public class New2 extends Fragment {
TextView name;
Button change;
ImageView image1;
Animation anime;
private OnReplaceFragment dataPasser;
public static New2 newInstance(){
New2 fragment = new New2();
return fragment;
}
public New2(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_new2, container, false);
name = (TextView) rootView.findViewById(R.id.nameTt);
change = (Button) rootView.findViewById(R.id.changeBtn);
image1 = (ImageView) rootView.findViewById(R.id.image1);
anime = AnimationUtils.loadAnimation(getActivity().getApplicationContext(),R.anim.zoom);
change.setOnClickListener(changeName);
return rootView;
}
View.OnClickListener changeName = new View.OnClickListener() {
#Override
public void onClick(View view) {
image1.startAnimation(anime);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run(){
dataPasser.onReplaceFragment(Result.class);
}
},1000);
}
};
public interface OnReplaceFragment {
public void onReplaceFragment(Class fragmentClass);
}
#Override
public void onAttach(Activity a) {
super.onAttach(a);
try {
dataPasser = (OnReplaceFragment) a;
} catch (ClassCastException e) {
throw new ClassCastException(a.toString() + " must implement onDataPass");
}
}
}
the fragment that i want to display
public class Result extends Fragment {
TextView textView;
Button btnBack;
public static Result instance = null;
public static Result getInstance(){
return instance;
}
public Result() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_result, container, false);
textView = (TextView) v.findViewById(R.id.text11);
btnBack = (Button) v.findViewById(R.id.btnBack);
textView.setText("working!!");
Toast.makeText(getActivity().getApplicationContext(),"working",Toast.LENGTH_LONG).show();
return v;
}
}
Main Activity 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"
tools:context="com.example.hercules.tadhosttutrial.MainActivity">
<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/red"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
New2 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.hercules.tadhosttutrial.New2"
android:background="#color/yellow">
<!-- TODO: Update blank fragment layout -->
<Button
android:id="#+id/changeBtn"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:text="change"/>
<ImageView
android:id="#+id/image1"
android:background="#drawable/icon_complete"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
Result 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:background="#color/colorAccent"
tools:context="com.example.hercules.tadhosttutrial.Result">
<!-- TODO: Update blank fragment layout -->
<Button
android:id="#+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="back"/>
<TextView
android:id="#+id/text11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WORKING"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="70dp"
/>
</RelativeLayout>
What i mean, is making MainActivity as a main container for all fragments, either with tabs or just a regular fragment,
1- Main Activity XML: remove ViewPager, add a FrameLayout instead (use same id)
2- Create new fragment TabsFragment with this 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.hercules.tadhosttutrial.New2"
android:background="#color/yellow">
<android.support.v4.view.ViewPager
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</RelativeLayout>
3- Move initializing SectionsPagerAdapter and ViewPager from main activity to TabsFragment:
this part:
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
and this:
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.frame_container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.getTabAt(0).setIcon(R.drawable.icon_info);
tabLayout.getTabAt(1).setIcon(R.drawable.icon_heart_rate_sensor_jpg);
tabLayout.getTabAt(2).setIcon(R.drawable.icon_graph_jpg);
4- I think moving SectionsPagerAdapter class in a new file is better too.
now if you want default view for app to be the tabs, then in MainActivity at onCreate() show TabsFragment by calling your method:
onReplaceFragment(TabsFragment.class);
now every thing should work fine, because the idea here is to replace the fragment displayed in main activity with another one
in this case TabsFragment, Result, and New2
not to replace viewpager fragments (because as i told you this is managed via the adapter) not by calling replace()
you may need to play around this, it's not a final code, just something to give you idea about it.

Android studio: how add tabs with ViewPager

I want to add in a fragment a tab with pagerview (scrollable).
public class MyFragment extends Fragment {
private FragmentTabHost tabHost;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
tabHost = new FragmentTabHost(getActivity());
tabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
tabHost.addTab(tabHost.newTabSpec("one").setIndicator("One"), OneFragment.class, null);
tabHost.addTab(tabHost.newTabSpec("two").setIndicator("Two"), TwoFragment.class, null);
return tabHost;
}
#Override
public void onDestroyView(){
super.onDestroyView();
tabHost=null;
}
}
With this layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
I tried several solutions, but do not work.
I need to use fragment, not fragmentActivity.
The code written up work.
Small Code for Tablayout + ViewPager
// find views by id
ViewPager vp= findViewById(R.id.viewpager);
TabLayout tl = findViewById(R.id.tablayout);
// attach tablayout with viewpager
tl.setupWithViewPager(vp);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
// add your fragments
adapter.addFrag(new SampleFragment(), "Tab1");
adapter.addFrag(new SampleFragment(), "Tab2");
adapter.addFrag(new SampleFragment(), "Tab3");
// set adapter on viewpager
vp.setAdapter(adapter);
XML layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.tabs.TabLayout
android:id="#+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
Note If you are not using AndroidX yet, you need to change following in layout.
Change com.google.android.material.tabs.TabLayout to android.support.design.widget.TabLayout
Chagne androidx.viewpager.widget.ViewPager to android.support.v4.view.ViewPager
But I'll strongly recommend to migrate to AndroidX, see #this answer to understand why.
And this is common ViewPagerAdapter for all your Viewpager in app.
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
public void addFrag(Fragment fragment) {
mFragmentList.add(fragment);
mFragmentTitleList.add("");
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
}
If you need to set ViewPager in Fragment, please check #this answer.
Used xml file like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent">
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabMaxWidth="0dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<!-- View pager to swipe views -->
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
and your java file is here:
public class MyFragment extends Fragment {
private View view;
private TabLayout tabLayout;
//This is our viewPager
private ViewPager viewPager;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.sticker_fragment, container, false);
tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
viewPager = (ViewPager) view.findViewById(R.id.pager);
Viewpager adapter = new Viewpager(getActivity().getSupportFragmentManager(), getActivity());
//Adding adapter to pager
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return view;
}
}
after that viewpager adapter is like this:
public class Viewpager extends FragmentStatePagerAdapter {
final int PAGE_COUNT = 2;
private String tabTitles[] = new String[]{"Local","Online"};
private Context context;
public Viewpager(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
OneFragment oneFragment=new OneFragment();
return oneFragment;
case 1:
TwoFragment twoFragment=new TwoFragment();
return twoFragment;
default:
return null;
}
}
#Override
public int getCount() {
return tabTitles.length;
}
#Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}

TabLayout - ViewPager - Fragment

I have a problem with my ViewPager and fragments it contains. I have 2 menus (A and B), my homepage is directly A. When I click on a menu B fragment which contains a TabLayout, which contains a ViewPager himself three fragments (each contains a simple TextView with Lorem ipsum).
The 3 fragments of ViewPager are correct but if I click on the menu A and I click again on the menu B I do not have any content. Nothing on the fragments 1 and 2 against with the 3rd still has the text and if I return to fragment 1 reads income (for nothing against the fragment 2).
Here is my code :
Menu B (FragmentTabLayout)
public class TabLayoutFragment extends Fragment {
private static final String ARG_TEXT = "ARG_TEXT";
private static final String ARG_COLOR = "ARG_COLOR";
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
public TabLayoutFragment() {
// Required empty public constructor
}
public static TabLayoutFragment newInstance(String text, int color) {
Bundle args = new Bundle();
args.putString(ARG_TEXT, text);
args.putInt(ARG_COLOR, color);
TabLayoutFragment tabLayoutFragment = new TabLayoutFragment();
tabLayoutFragment.setArguments(args);
return tabLayoutFragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_tab_layout, container, false);
toolbar = (Toolbar) view.findViewById(R.id.toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
toolbar.setBackgroundColor(getArguments().getInt(ARG_COLOR));
toolbar.setTitle(getArguments().getString(ARG_TEXT));
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setBackgroundColor(getArguments().getInt(ARG_COLOR));
tabLayout.setSelectedTabIndicatorColor(Color.WHITE);
return view;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.tab_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
//private final List<TabFragment> mFragmentList = new ArrayList<>();
//private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return TabFragment.newInstance("Fragment 2-1");
case 1:
return TabFragment.newInstance("Fragment 2-2");
case 2:
return TabFragment.newInstance("Fragment 2-3");
default:
return TabFragment.newInstance("Fragment Default");
}
}
#Override
public int getCount() {
//return mFragmentList.size();
return 3;
}
}
}
Fragment in ViewPager :
public class TabFragment extends Fragment {
private static final String ARG_TEXT = "ARG_TEXT";
private TextView tv;
public TabFragment() {
// Required empty public constructor
}
public static TabFragment newInstance(String text) {
Bundle args = new Bundle();
args.putString(ARG_TEXT, text);
TabFragment tabFragment= new TabFragment();
tabFragment.setArguments(args);
return tabFragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_tab, container, false);
tv = (TextView) view.findViewById(R.id.tv);
return view;
}
}
Layout FragmentTabLayout :
<android.support.design.widget.CoordinatorLayout
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:orientation="vertical">
<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.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</android.support.design.widget.CoordinatorLayout>
layout tabFragment :
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context="com.application.myapplication.TabFragment">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
>
<TextView
android:id="#+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/lorem_ipsum"
/>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
you can extends your ViewPagerAdapter
FragmentStatePagerAdapter
instead of
FragmentPagerAdapter
I have same issue and it works for me.
In my Opinion the better approach is you should try using childFragmentManager for your nested fragments
Here is the simple example:
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getChildFragmentManager());
viewPager.setAdapter(adapter);
}
Hopefully it will solve your issue.

In a fragment implementation, the ListView below EditText disappears after setting keypad element (of EditText view) to hide

this is image while useing keypad
and this image after keypad was hidden
I use EditText but below it there is ListView. After typing text inputs in EditText and then press done or click back to hide keypad. Then the part of ListView behind the keypad disappeared and replaced by white area (cleared), why ?
NOTE: this fragment is under tablayout and viewpager and the fragment which contain the edit text and listview is launched from the main fragment
this is main fragment
public class FriendsFragment extends Fragment {
public FriendsFragment() {
// Required empty public constructor
}
private FragmentActivity myContext;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onAttach(Activity activity) {
myContext=(FragmentActivity) activity;
super.onAttach(activity);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview=inflater.inflate(R.layout.friends_fragment, container, false);
viewPager = (ViewPager) rootview.findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) rootview.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(
new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
#Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
if (tab.getPosition() == 1) {
FindFriendsFragment.myfriends_list.invalidate(FindFriendsFragment.myfriends_list.getLeft(), FindFriendsFragment.myfriends_list.getTop(), FindFriendsFragment.myfriends_list.getRight(), FindFriendsFragment.myfriends_list.getBottom());
FindFriendsFragment.adapter.notifyDataSetChanged();
FindFriendsFragment.myfriends_list.clearFocus();
FindFriendsFragment.myfriends_list.postInvalidate();
}
}
});
TextView friends = (TextView) rootview.findViewById(R.id.search);
Typeface Exo_thin = Typeface.createFromAsset(myContext.getAssets(), "fonts/Exo2.0-Thin.otf");
friends.setTypeface(Exo_thin);
return rootview;
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new MyFriendsFragment(), "My Friends");
adapter.addFragment(new FindFriendsFragment(), "Find Friends");
adapter.addFragment(new TwoFragment(), "Friend Requests");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
this is xml of main fragment
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:clickable="true"
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:textAllCaps="false"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:id="#+id/rel"
android:layout_alignParentTop="true"
android:weightSum="1">
<TextView
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="#+id/search"
android:text="Friends"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"
android:textSize="25sp"
android:textColor="#color/colorVeryDarkBlue"
/>
</RelativeLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextAppearance="#android:style/TextAppearance.Widget.TabWidget"
app:tabSelectedTextColor="#color/colorLightGreen"
app:tabTextColor="#color/colorDarkGreen"
app:tabMode="scrollable"
android:background="#color/colorDarkBlue"
app:tabIndicatorColor="#color/colorDarkBlue"
app:tabGravity="center"/>
</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"/>
the fragment which contain listview and edit text
public class FindFriendsFragment extends Fragment {
public FindFriendsFragment()
{
// Required empty public constructor
}
ListView myfriends_list;
FindFriendsAdapter adapter;
ArrayList<FindFriends> arraylist ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootview= inflater.inflate(R.layout.find_friends_fragment, container, false);
EditText search=(EditText) rootview.findViewById(R.id.search);
Typeface Exo_Regular =
Typeface.createFromAsset(getActivity().getAssets(), "fonts/Exo2.0-
Regular.otf");
search.setTypeface(Exo_Regular);
arraylist = new ArrayList<FindFriends>();
arraylist.add(new FindFriends("mina fared", "hello
guys",1,"sdsdsdsds",true )) ;
adapter = new FindFriendsAdapter(getActivity(), arraylist);
myfriends_list.setAdapter(adapter);
adapter.notifyDataSetChanged();
return rootview;
}
}
And the related xml file is of the fragment which contain listview and edittext:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:background="#ffffff"
>
<LinearLayout
android:layout_height="60dp"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_marginTop="111dp"
android:id="#+id/refl"
android:background="#color/colorLightGrey"
android:layout_alignParentTop="true"
>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="#+id/search"
android:layout_marginRight="15dp"
android:layout_marginLeft="15dp"
android:background="#ffffff"
android:singleLine="true"
android:hint="Search"
android:ellipsize="start"
android:imeOptions="actionDone"
android:gravity="center_vertical|center_horizontal"
android:layout_marginTop="10dp"
android:textSize="15sp"
android:textColorHint="#color/colormyhintfindfiernds"
android:textColor="#color/colorDarkBlue"
/>
</LinearLayout>
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:layout_marginTop="1dp"
android:layout_below="#+id/refl"
android:divider="#ffffff"
android:dividerHeight="1.5dp"
/>
</RelativeLayout>
this is the parent fragment which contains all fragments
public class ProfileFragment extends Fragment {
public ProfileFragment()
{
// Required empty public constructor
}
RelativeLayout rl1;
DrawView drawView ;
DrawView drawView2;
TextView myprofile,username,notification_txt,colloection_txt,friends_txt,setting_txt,public_profile_txt;
ImageView public_profile_btn,friends;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootview=inflater.inflate(R.layout.myprofile_fragment, container,
false);
username= (TextView) rootview.findViewById(R.id.user_name_txt);
myprofile= (TextView) rootview.findViewById(R.id.myprofile);
notification_txt= (TextView) rootview.findViewById(R.id.notification_txt);
colloection_txt= (TextView) rootview.findViewById(R.id.colloection_txt);
friends_txt= (TextView) rootview.findViewById(R.id.friends_txt);
setting_txt= (TextView) rootview.findViewById(R.id.setting_txt);
public_profile_txt= (TextView) rootview.findViewById(R.id.public_profile_txt);
Typeface Exo_thin = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Exo2.0-Thin.otf");
Typeface Exo_SemiBold = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Exo2.0-SemiBold.otf");
myprofile.setTypeface(Exo_thin);
username.setTypeface(Exo_SemiBold);
notification_txt.setTypeface(Exo_SemiBold);
colloection_txt.setTypeface(Exo_SemiBold);
friends_txt.setTypeface(Exo_SemiBold);
setting_txt.setTypeface(Exo_SemiBold);
public_profile_txt.setTypeface(Exo_SemiBold);
ImageView x=(ImageView)rootview.findViewById(R.id.colloection);
ImageView y=(ImageView)rootview.findViewById(R.id.friends);
ImageView x1=(ImageView)rootview.findViewById(R.id.setting);
rl1 =(RelativeLayout)rootview.findViewById(R.id.rel2);
final View fragmentContainer = rootview.findViewById(R.id.container);
friends =(ImageView)rootview.findViewById(R.id.friends);
public_profile_btn=(ImageView)rootview.findViewById(R.id.public_profile);
public_profile_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
Fragment newFragment = new MyProfileFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(fragmentContainer.getId(), newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
friends.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
Fragment newFragment = new FriendsFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(fragmentContainer.getId(), newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
return rootview;
}
}
from manifest.xml search for your activity tag like this
<activity
android:name=".YourActivity"
android:label="#string/title_activity"
...>
</activity>
then add android:configChanges="keyboard|keyboardHidden" like that
<activity
android:name=".YourActivity"
android:configChanges="keyboard|keyboardHidden"
android:label="#string/title_activity"
...>
</activity>
EDIT
i found that my answer not completely right you may add
android:windowSoftInputMode="adjustNothing"
sorry about that

Android TabLayout ViewPager not inflating tab fragment on backstack

I've got a tab view set up that that has custom fragments for each tab using a viewpager. This is my code:
Holding Fragment
public class FragInboxMainView extends Fragment implements CGFragment {
private CGController controller;
private CGFragment thisFragment;
#Bind(R.id.inboxViewPager)ViewPager inboxViewPager;
#Bind(R.id.inboxTabs)TabLayout inboxTabLayout;
#Bind(R.id.inbox_progress_wheel)ProgressWheel inboxProgressWheel;
public FragInboxMainView(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_inbox_mainview, container, false);
ButterKnife.bind(this, rootView);
thisFragment = this;
Globals g = Globals.getInstance();
/** Show loading spinner */
this.inboxProgressWheel.setBarColor(ContextCompat.getColor(controller.getContext(), g.getUserObject().getUserThemeColor()));
this.inboxProgressWheel.setVisibility(View.VISIBLE);
/** Display the profile information based off the ID */
controller.displayInbox(thisFragment);
return rootView;
}
public void hideProgressSpinner() {
this.inboxProgressWheel.setVisibility(View.GONE);
}
public ViewPager getInboxViewPager() {
return this.inboxViewPager;
}
public TabLayout getInboxTabLayout() {
return this.inboxTabLayout;
}
}
Its layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:wheel="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="#+id/inboxTabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable" />
<com.pnikosis.materialishprogress.ProgressWheel
android:id="#+id/inbox_progress_wheel"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
wheel:matProg_barColor="#5588FF"
wheel:matProg_progressIndeterminate="true"
android:visibility="gone"/>
<android.support.v4.view.ViewPager
android:id="#+id/inboxViewPager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="#android:color/white" />
</LinearLayout>
Tab fragment and its inflation file
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:id="#+id/main_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.baoyz.widget.PullRefreshLayout
android:id="#+id/tabPullRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<view
android:id="#+id/tabRecyclerHolder"
class="android.support.v7.widget.RecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:clipToPadding="false"
android:layout_centerInParent="true"/>
</com.baoyz.widget.PullRefreshLayout>
<com.melnykov.fab.FloatingActionButton
android:id="#+id/tabFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="16dp"
android:src="#mipmap/ic_add_white"/>
</android.support.design.widget.CoordinatorLayout>
public class TabRecyclerHolder extends Fragment {
#Bind(R.id.tabRecyclerHolder) RecyclerView tabRecyclerHolder;
#Bind(R.id.tabPullRefresh) PullRefreshLayout tabPullRefresh;
#Bind(R.id.tabFab) FloatingActionButton recyclerFab;
private String tabTitle = "Title";
public TabRecyclerHolder(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_recycler_holder, container, false);
ButterKnife.bind(this, rootView);
recyclerFab.hide(false);
tabPullRefresh.setRefreshStyle(PullRefreshLayout.STYLE_MATERIAL);
return rootView;
}
public RecyclerView getTabRecyclerHolder() {
return this.tabRecyclerHolder;
}
public FloatingActionButton getRecyclerFab() {
return this.recyclerFab;
}
public String getTabTitle() {
return this.tabTitle;
}
public void setTabTitle(String title) {
this.tabTitle = title;
}
public PullRefreshLayout getTabPullRefresh() {
return this.tabPullRefresh;
}
}
My tab adapter
public class TabPagerAdapter extends FragmentStatePagerAdapter {
private CGController controller;
private List<Object> items;
public TabPagerAdapter(FragmentManager fm, CGController controller, List<Object> items) {
super(fm);
this.controller = controller;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Fragment getItem(int num) {
return (TabRecyclerHolder)items.get(num);
}
#Override
public String getPageTitle(int num){
return ((TabRecyclerHolder)items.get(num)).getTabTitle();
}
}
The processing code
public void viewInbox() {
/** Set up the views */
receivedHolder = new TabRecyclerHolder();
receivedHolder.setTabTitle(Constants.TAB_INBOX_RECEIVED);
sentHolder = new TabRecyclerHolder();
sentHolder.setTabTitle(Constants.TAB_INBOX_SENT);
tabs.add(receivedHolder);
tabs.add(sentHolder);
/** Set up the tabs */
final ViewPager inboxViewPager = inboxFragment.getInboxViewPager();
TabLayout inboxTabLayout = inboxFragment.getInboxTabLayout();
/** Set the adapter for the view pager */
inboxViewPager.setAdapter(new TabPagerAdapter(inboxFragment.getChildFragmentManager(), controller, tabs));
/** set up the tab look and feel */
inboxTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
inboxTabLayout.setTabMode(TabLayout.MODE_FIXED);
inboxViewPager.setOffscreenPageLimit(3);
inboxViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(inboxTabLayout));
inboxTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
inboxViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
/** And, display! */
inboxTabLayout.setupWithViewPager(inboxViewPager);
receivedAdapter = new RecyclerListAdapter(controller, items);
final RecyclerView receivedList = receivedHolder.getTabRecyclerHolder();
receivedList.setLayoutManager(new LinearLayoutManager(controller.getContext()));
receivedList.setAdapter(receivedAdapter);
}
There is some code i've missed out but its not pertanent to the question.
The code works perfectly when initially viewing the fragment. However since my application contains a single activity and just replaces a content view for each fragment navigated to, each fragment is added to the back stack and then popped when the back button is pressed. My issue is that when navigating back to this fragment the view inside the tab isn't being inflated, which means that no elements can be accessed (and therefore the app crashes while trying to display the data in the recyclerview etc).
I have had a look at this question: TabLayout ViewPager Not Loading When Using Backstack and implemented its suggestion (using getChildFragmentManager() when setting up the pager adapter) however that has not fixed my issue.
Help would be appreciated!
Change this public View onCreateView(LayoutInflater inflater,... to public void onViewCreated (View view, Bundle savedInstanceState)
so you are going to have something like this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab_recycler_holder, container, false);
}
then
#Override
public void onViewCreated (View view, Bundle savedInstanceState){
ButterKnife.bind(this, view);
recyclerFab.hide(false);
tabPullRefresh.setRefreshStyle(PullRefreshLayout.STYLE_MATERIAL);
...
see if it helps
Extend FragmentStatePagerAdapter in TabPagerAdapter as
FragmentStatePagerAdapter(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT)

Categories

Resources