Let's say that I have a ViewPager that will have 2 Fragments. From those Fragments, I create a PublishSubject which I would like to subscribe to within the MainActivity. However, the issue is during the onCreate(), I would not be able to grab that specific PublishSubject from the Fragment since it seems like it hasn't been created yet. Therefore, I end up with an NPE if I tried to make a subscription for it.
Is there a way to pass the PublishSubject once the Fragment is created by the ViewPager to the MainActivity? Or should I be trying to do something else instead. I've tried looking through other issues, but I couldn't find anything that is similar with something like this.
Some Code to illustrate what I mean:
Fragment
public class ExampleFragment extends Fragment {
PublishSubject<SomeObject> mPublishSubject = PublishSubject.create();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance){
// Regular fragment stuff here
}
public PublishSubject<SomeObject> getPublishSubject() {
return mPublishSubject;
}
}
ViewPager
public class ExamplePagerAdapter extends FragmentStatePagerAdapter {
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new ExampleFragment();
case 1:
return new SomeOtherFragment();
default:
return null;
}
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
private PublishSubject<SomeObject> mSubjectFromTheFragment;
private ExamplePagerAdapter mPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("tab1"));
tabLayout.addTab(tabLayout.newTab().setText("tab2"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
mViewPager = (ViewPager) findViewById(R.id.view_pager);
mPagerAdapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
mViewPager.setAdapter(mPagerAdapter);
// Set listener
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
// Code to implement the methods
});
// When or where do I set PublishSubject
mPublishSubject = ?
// The subscription
mPublishSubject.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(/** Some new Action1<> etc. **/ )
}
}
Any help or pointing me to some kind of direction is really appreciated!
Related
Following is my onCreate code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "Inside Base Drawer...");
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager){
#Override
public void onTabSelected(TabLayout.Tab tab){
tabPosition = tab.getPosition();
}
});
}
And following is my FragmentPageAdapter, for sliding tab layouts
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
Tab1Fragment tab1 = new Tab1Fragment();
return tab1;
case 1:
Tab1Fragment tab2 = new Tab2Fragment();
return tab2;
case 2:
Tab1Fragment tab3 = new Tab3Fragment();
return tab3;
default:
return null;
}
}
#Override
public int getCount() {
return 3;
}
}
Following is the tabFragment code:
public class Tab1Fragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState){
return inflater.inflate(R.layout.tab_activity_sheet_details_tab1, viewGroup, false);
}
}
I have a custom autocompleteTextView inside the tab layout (tab_activity_sheet_details_tab1)
Following is the code for the same:
<com.example.user.package.TabLayoutActivity.AutoCompleteAdapter.HistoryAutoCompleteTextView
android:id="#+id/history_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapCharacters"
android:imeOptions="flagNoExtractUi|actionSearch"/>
However, I'm not being able to access this element from TabLayoutActivity. What is the best way to access this after the fragment view is inflated?
And do note that there are similar items in all the three Tab Layouts.
Is there a need of a separate adapter holding all the three fragment layouts and access them? If so, from which lifecycle element can I access them?
P.S.: I'm looking for a non-adapter access to fragments, if possible (possibly by "layout id")?
Something like this would expose the view to the outside world:
public class Tab1Fragment extends Fragment {
View mView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState){
mView = inflater.inflate(R.layout.tab_activity_sheet_details_tab1, viewGroup, false);
return mView;
}
public HistoryAutoCompleteTextView getTextView() {
return (HistoryAutoCompleteTextView)mView.findViewById(R.id.history_text);
}
}
Then you can get it from the fragment:
textView = ((Tab1Fragment)fragment).getTextView()
But from a software design point of view, you should probably not do that and consider instead managing this view entirely in the Fragment and only expose the data collected.
I am having trouble with below code, i have 5 tabs in my application and when i am trying to switch through them i just noticed that onCreateView is called multiple times. Now first i did saw multiple post about similar issue where multiple times onCreateView is called, but mine is lit bit different, in my application onCreateView is called based on number of tabs i am switching. For example if i am DOWNLOAD tab and i switch to FAVORITE, onCreateView will be called 3 times. If i do same action from settings it will be called 4 times. Same thing happens with CANDIDATE and other tab.
Similar Posts -
1 - OnCreateView called multiple times / Working with ActionBar and Fragments
2 - Android fragment OnCreateView called twice
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private Boolean exit = false;
private static final int REQUEST = 112;
private Context mContext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
setSupportActionBar(toolbar);
mTitle.setText(toolbar.getTitle());
getSupportActionBar().setDisplayShowTitleEnabled(false);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
mContext = MainActivity.this;
setupTabIcons();
}
#Override
protected void onResume()
{
super.onResume();
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(getResources().getDrawable(R.drawable.settings));
tabLayout.getTabAt(1).setIcon(getResources().getDrawable(R.drawable.download));
tabLayout.getTabAt(2).setIcon(getResources().getDrawable(R.drawable.register));
tabLayout.getTabAt(3).setIcon(getResources().getDrawable(R.drawable.profile));
tabLayout.getTabAt(4).setIcon(getResources().getDrawable(R.drawable.favwhite));
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new SettingsFragment(), getResources().getString(R.string.settings_tab));
adapter.addFragment(new DownloadFragment(), getResources().getString(R.string.download_tab));
adapter.addFragment(new RegisterFragment(), getResources().getString(R.string.register_tab));
adapter.addFragment(new ProfileFragment(), getResources().getString(R.string.profile_tab));
adapter.addFragment(new ProfileFragment(), getResources().getString(R.string.favorites_tab));
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(0);
}
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 addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
Update -
To add more when it is run 4 times the data on screen is 4 times which is duplicate.
Fragement Code -
public class RegisterFragment extends Fragment{
public RegisterFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_register, container, false);
}
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
getFragmentManager().beginTransaction().detach(this).attach(this).commit();
}
}
}
By default view pager load the fragment in the following order.
Currently selected position fragment. Visible
Cache for the (selected position - 1) fragment, if any. Not visible
Cache for the (selected position + 1) fragment, if any. Not visible
Reason for this is, to make smooth animation from one fragment to another without lagging, view pager caches the previous and the next fragment. To confirm this log the position in the getItem() method in the view pager adapter.
As a result of above. When launching activity your viewpager is loading position 0 and 1. ie loading 2 fragment. There is no fragment in -1 position.
I have this Layout:
1) An AppCompatActivity with a ToolBar and a fragment container
2) Some simple Fragments
3) A Fragment that coantains TabLayout with ViewPager
When i use the fragments that are supposed to be used on the tabs the fragments are showing nicely.
One of the fragments that are supposed to be on the tabs has inside a ViewPager.
This is how i try to implement the TabLayout inside the Fragment
The Fragment with the TabLayout and the ViewPager:
public class StaffProfileFragment : Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
// return inflater.Inflate(Resource.Layout.YourFragment, container, false);
var view = inflater.Inflate(Resource.Layout.StaffProfilesWithTabs, container, false);
var fragments = new Fragment[]
{
new StaffList(),
new MessagesFragment(),
new SuggestionsMessagesFragment()
};
var titles = CharSequence.ArrayFromStringArray(new[]
{
"Profile",
"Thanks",
"Suggestions"
});
var viewPager = view.FindViewById<ViewPager>(Resource.Id.ProfileViewpager);
viewPager.Adapter = new TabsFragmentPagerAdapter(ChildFragmentManager, fragments, titles);
//var myActivity = (MainActivity)this.Activity;
//myActivity.SetActionBarTitle("Select Your Location");
// Give the TabLayout the ViewPager
var tabLayout = view.FindViewById<TabLayout>(Resource.Id.SlidingTabsTabLayout);
tabLayout.SetupWithViewPager(viewPager);
return view;
}
}
}
And the Adapter i use:
public class TabsFragmentPagerAdapter : FragmentPagerAdapter
{
private readonly Fragment[] fragments;
private readonly ICharSequence[] titles;
public TabsFragmentPagerAdapter(FragmentManager fm, Fragment[] fragments, ICharSequence[] titles) : base(fm)
{
this.fragments = fragments;
this.titles = titles;
}
public override int Count
{
get
{
return fragments.Length;
}
}
public override Fragment GetItem(int position)
{
return fragments[position];
}
public override ICharSequence GetPageTitleFormatted(int position)
{
return titles[position];
}
}
(Im putting only this code for now if it is enough for the problem to be solved, else i will put the xmls and the Fragments code.)
The problem is that the fragment appears but it doesnt load the data correctly, tha tabs cannot be swyped and if i click on the fragment somewhere the app crash with crushing in Debugger.
What am i doing wrong? is it bad idea to implement tablayout inside a fragment? maybe i should implement it on the AppCompatActivity and show/hide the elements i want to use on every occasion?
Two solutions that work as i have test them are:
Create a seperate Activity with toolbar and tabhost.
In the activity with the toolbar to add tabhost and when i want to show only one fragment, i would use tabhost and set visibility to gone.
But im trying to understand which is the best way to do this,
Thanks
I have a TabLayout with 3 tab.They are a fragment.
Test.java->PagerAdapter.java->TabFragment1+TabFragment2+TabFragment3
I want to call TabFragment1.helloworld() function. How can i call this function? Thanks.
Test.java created 3 tabs in below
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("tab1"));
tabLayout.addTab(tabLayout.newTab().setText("tab2"));
tabLayout.addTab(tabLayout.newTab().setText("tab3"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setOffscreenPageLimit(3);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(),
tabLayout.getTabCount());
viewPager.setAdapter(adapter);
PagerAdapter.java
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabFragment1 tab1 = new TabFragment1();
return tab1;
case 1:
TabFragment2 tab2 = new TabFragment2();
return tab2;
case 2:
TabFragment3 tab3 = new TabFragment3();
return tab3;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
You can access the desired fragment's method through your PagerAdapter:
((TabFragment1)adapter.getItem(0)).helloworld();
It's a bit trickier than usual with ViewPagers, because you can't use the preferred findFragmentByTag() method. Here's something you could try - it's untested, but should be ok:
Create an interface, which all Fragments in your ViewPager implement:
public interface ViewPagerFragmentBase {
void callCommonMethod(int code);
}
Implement it in all your ViewPager Fragments:
public FragmentOne implements ViewPagerFragmentBase {
#Override
public void callCommonMethod(int code) {
if (code == 1) {
//Run code in this Fragment
}
}
public FragmentTwo implements ViewPagerFragmentBase {
#Override
public void callCommonMethod(int code) {
if (code == 2) {
//Run code in this Fragment
}
}
Inside your Activity class, grab all Fragments currently in your Adapter, then cycle through each one, calling the callCommonMethod() along with a code which indicates the Fragment you are trying to reach:
public class MainActivity extends AppCompatActivity {
private void callCommonMethodInFragments(int code) {
for (int i = 0; i < viewPagerAdapter.getCount(); i++) {
((ViewPagerFragmentBase)viewPagerAdapter.getItem(i)).callCommonMethod(code);
}
}
//To call it...
callCommonMethodInFragments(1); //This will run the method in FragmentOne only
}
If the code matches the one in the Fragment you are trying to reach, then the method will run.
Something else you could also try though is an EventBus - this would allow you to achieve your goal more directly, and with less typing: https://github.com/greenrobot/EventBus
To call fragment method from parent activity:
ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
fragment.callFunction();
I had the same issue, and this worked for me:
You have to add the fragments via your adapter and then you can access the fragment functions:
myViewPager = (ViewPager) findViewById(R.id.viewpager);
MyAdapterClass adapter = new MyAdapterClass(getSupportFragmentManager());
adapter.addFragment(new TabOneFragment(), "");
adapter.addFragment(new TabTwoFragment(), "");
adapter.addFragment(new TabThreeFragment(), "");
myViewPager.setAdapter(adapter);
myTabLayout = (TabLayout) findViewById(R.id.tab_layout);
myTabLayout.setupWithViewPager(myViewPager);
// to access fragment functions:
TabOneFragment tabOneFragment = (TabOneFragment) adapter.getItem(0);
tabOneFragment.functionWhatSoEver();
I'm looking at a guide on how to use TabLayout. This is how they show to attach the PagerAdapter class.
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(),
MainActivity.this));
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
}
However, their TabLayout is in an Activity, and mine is in a Fragment. I'm having trouble adapting their code to my use case, because I don't fully understand the practical differences between activities and fragments. Right now, when I try and swipe between tabs, nothing happens. Here is my code:
public class CandidateListFragment extends Fragment {
public static final String TAG = "candidates";
#InjectView(R.id.tabLayout) TabLayout tabLayout;
public CandidateListFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_candidatelist, container, false);
initInstances(v);
return v;
}
private void initInstances(View v){
//TODO: Change to support below API level 21
getActivity().findViewById(R.id.toolbar_actionbar).setElevation(R.dimen.toolbar_candidatelist_elevation);
ViewPager viewPager = (ViewPager) v.findViewById(R.id.viewpager);
viewPager.setAdapter(new TabsFragmentPagerAdapter(getActivity().getSupportFragmentManager(),
getActivity()));
ButterKnife.inject(this, v);
tabLayout.setupWithViewPager(viewPager);
}
#Override public void onDestroyView() {
super.onDestroyView();
ButterKnife.reset(this);
}
I figured it out!!!
I had my xml layout file for the fragment set to RelativeLayout instead of LinearLayout, so my ViewPager wasn't on the screen, which meant I couldn't swipe. It's the simple mistakes that get me