Replacing the Fragment in Tablayout Android - android

I used the tablayout in my project. I have to replace the fragment in the tablayout. The code is like below
Activity Class
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(Fragment1, "One");
adapter.addFragment(Fragment2, "Two ");
adapter.addFragment(Fragment3, "Three");
viewPager.setAdapter(adapter);
}
Interface in Activity
#Override
public void onInput(int input) {
Here how to replace fragment three
}

I think replacing the fragment is not a good idea. So I would like to show you another way to achieve what you want.
viewPager.setCurrentItem(position); you can use this method to change the page. Here position is page-number starting from 0 to total-pages - 1.
Use ViewModal to pass latest data (in your case "input"). So here you can set value from onInput(int input) and in each fragment of ViewPager you can subscribe/ unsubscribe ViewModal to listen changes. In this way you do not need to replace fragment and the page which is on screen will get latest data and can show.

Related

Setting View's atribute that is in child fragment

I want to access Views that are in Fragment's children that are put in ViewPager. The idea is to toggle (disable/enable) spinners when button is pressed. Spinners are placed in separate fragments that are displayed in ViewPager controlled by TabLayout:
// Snippet of code from Fragment A
final TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
final ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewPager);
Fragment childFragment = new Fragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.viewPager, childFragment).commit();
ViewPagerAdapter adapter = new ViewPagerAdapter(childFragment.getFragmentManager());
adapter.AddFragment(new fragment_unositelj(), "FragmentB");
adapter.AddFragment(new fragment_voditelj(), "FragmentC");
adapter.AddFragment(new fragment_pratitelj(), "FragmentD");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
FragmentB = viewPager.findViewById(R.id.FragmentB);
FragmentC = viewPager.findViewById(R.id.FragmentC);
FragmentD = viewPager.findViewById(R.id.FragmentD);
I don't know how to achieve to set Spinner disabled / enabled in all fragment children's spinner. I tried to set public variable in MainActivity and to control Spinners attribute based on that, but I am not sure in which lifecycle part to place it because fragment B is inflated initialy, and others are when TabItem clicked. I hope I could describe my problem properly. If anything more is needed please let me know. Thanks!
I found workaround for this problem, I am not sure if it is correct way, but for now it works for me. In FragmentB,FragmentC,FragmentD i added
#Nullable
#Override
public View getView() {
if (((MainActivity) getActivity()).getStatus() == 1) {
mySpinner.setEnabled( true );
} else
mySpinner.setEnabled( false );
return super.getView();
}
//(MainActivity) getActivity()).getStatus() - method that returns state if it shold be enabled

How can I get values of checkboxes from all the fragments of a view pager, by clicking button in the main activity?

I am developing one android application and it has a tab layout with ViewPager setup. i have totally 8 fragments in the viewpager. Each Fragment holding an RecyclerView. The recyclerview row has one text and one checkbox.
Whenever the activity started it loads 8 fragments with recyclerview data. I have a button in the MainActivity, whenever the button is clicked i need to collect the checked rows from all 8 fragments and i have to send the same to the server.
i dont have any clue about the data retrieval but my UI design has been completed.
Looking in to example you followed, you can do something like:
1. Create global objects of fragments:
private OneFragment oneFragment;
private TwoFragment twoFragment;
private ThreeFragment threeFragment;
2. Initialize them in onCreate:
oneFragment = new OneFragment();
twoFragment = new TwoFragment();
threeFragment = new ThreeFragment();
3. Update setupViewPager method:
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(oneFragment, "ONE");
adapter.addFragment(new twoFragment, "TWO");
adapter.addFragment(new threeFragment, "THREE");
viewPager.setAdapter(adapter);
}
4. Create getSelectedValues mehthod in all fragments:
Create getSelectedValues in all fragments which will return
selected items.
5. Get selected values from onClick method in MainActivity:
if (oneFragment != null) {
oneFragment.getSelectedValues(); // here you'll get selected values of oneFragment
}
if (twoFragment != null) {
twoFragment.getSelectedValues(); // here you'll get selected values of twoFragment
}
if (threeFragment != null) {
threeFragment.getSelectedValues(); // here you'll get selected values of threeFragment
}

Dynamic Tab with Fragment

total novice,but bit familiar with static tabs addition to view pager.But here I stuck in dynamic tabs which I am able to show but not the fragments of those dynamically received tabs.How would I add fragments to those dynamically received tabs from server.Pardon me spelling and could be ambiguous question
Follow this link example and change the following method.
Visit https://www.androidhive.info/2015/09/android-material-design-working-with-tabs/
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
for(int i=0;i<NUM_OF_FRAGMENT;i++){
adapter.addFragment(new YourFragment(), "Title"+i);
}
viewPager.setAdapter(adapter);
}
You need only one fragment for creating dynamic tab.

How to modify a textview of the fragment from parent activity in android

In my activity, i have created added some fragments
public class MainActivity extends AppCompatActivity implements IabBroadcastListener{
static ViewPagerAdapter adapter;
....
adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new MainFragment(), "\uE602");
adapter.addFragment(new QuestionsListFragment(), "\uE620");
adapter.addFragment(new MyQuestionsFragment(), "\uE61E");
adapter.addFragment(new NotificationsFragment(), "\uE601");
adapter.addFragment(new ProfileSettingFragment(), "\uE605");
viewPager.setAdapter(adapter);
how can i modify like a textview of the fragment from my activity ? Im trying to get the view of the MainFragment from the parent activity
TextView textview_buy_SLPro = (TextView) howtogetMainFragment.findViewById(R.id.txt_Buy_Pro);
You can create a method inside your fragment, to change view or even return it.
public class MyFragment extends Fragment {
TextView mTextView;
....
public void setTextView(String text){
mTextView.setText(text);
}
....
}
And the call it from your activity:
public class MainActivity extends AppCompatActivity implements IabBroadcastListener{
static ViewPagerAdapter adapter;
....
MainFragment mMainFragment = new MainFragment();
adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(mMainFragment, "\uE602");
adapter.addFragment(new QuestionsListFragment(), "\uE620");
adapter.addFragment(new MyQuestionsFragment(), "\uE61E");
adapter.addFragment(new NotificationsFragment(), "\uE601");
adapter.addFragment(new ProfileSettingFragment(), "\uE605");
viewPager.setAdapter(adapter);
....
//call method inside fragment
mMainFragment.setTextView("Hello World!");
...
}
Try to use onActivityCreated()
#Override
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TextView textview_buy_SLPro = (TextView) getView().findViewById(R.id.txt_Buy_Pro);
textview_buy_SLPro.setText(getActivity.getSomeText());
}
I think better solution is create method in fragment
public void setBuySLPro(String data) {
textview_buy_SLPro.setText(data);
}
And call this method from activity. It's not the best idea to get access to the fragment view from the activity.
And fields should be name according to CamelCase rule.
You are talking about a fragment that is part of a ViewPager, so you have two concerns:
Your fragment might not have an active view (if you've swiped a few pages away from it, for instance). So the fragment's TextView might not even exist.
The fragment itself might not even exist (if you haven't swiped enough pages to create the fragment, or you are subclassing FragmentStatePagerAdapter, for example).
I have found that for this situation, the best design is an event listener pattern, where the fragment sets a listener on the activity when it creates a view. Also, the text value should be stored in the activity so that when someone swipes to the page with that fragment, the fragment can set up the correct value on the TextView.
The pattern goes like this:
Create an interface for the event; ex. TextUpdateListener
Add a list of listeners to your activity
When the fragment creates its view, it adds a listener to the activity, and when it destroys the view, it unregisters the listener. The listener will update its view when it receives the event from the activity.
When the value changes, the activity goes through the list of listeners and calls their event method.
I give a pretty detailed code example in this SO answer.
There's a number of ways to do this. My favorite is to use a message bus (such as Otto, tinybys, etc) and use events to communicate between app components. It's a design pattern and will take some learning to implement but well worth the effort imho.

Android: Updating tab content

I am new to Android development. I have a ViewPager which holds TabLayout (that has fixed number of tabs). The ViewPager has SectionPagerAdapter (that has a fragment corresponding to tabs).
Now, I want to update tab content when user selects the tab and as it becomes visible (or when user wants to refresh the content).
But I am not sure which is correct way I can do this.
So far till now I found two solutions:
1) Add listener to TabLayout:
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
{
#Override
public void onTabSelected(TabLayout.Tab tab)
{
mViewPager.setCurrentItem(tab.getPosition());
// Update tab containts here
}
#Override
public void onTabUnselected(TabLayout.Tab tab)
{
}
#Override
public void onTabReselected(TabLayout.Tab tab)
{
}
});
2) Use ViewPager.OnPageChangeListener to get callbacks for when a page fragment (inside Tab) becomes visible:
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
{
#Override
public void onPageSelected(int position)
{
// Update tab containts here
}
});
Can someone expert in this please advice me?
The way a viewpager adapter works (with tabs) is that once you add your fragments to the viewpager, then you basically need to add content to your fragments and let the adapter manage which fragment is visible when a given tab is selected.
So, by simply using : OnTabSelectedListenerinterface on your tabLayout, all you need to do is `setCurrentItem(tab.getPosition()); on your viewPager reference (from your layout).
You can have a method like this to add fragments to your viewPagerAdapter:
private void setupViewPager(ViewPager viewPager){
//then create an instance of your ViewPagerAdapter class here
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
//then add fragments for each TAB here
//by calling a method inside your Adapter like this
adapter.addFrag(new FragmentOne(), "TITLE HERE");
adapter.addFrag(new FragmentTwo(), "TITLE TWO");
//Now set your adapter here
viewPager.setAdapter(adapter);
}
Now in your fragments, you can show different content accordingly.
I hope this helps!

Categories

Resources