I have used SlidingTabLayout and Viewpager to make sliding Tab in fragement. And i have implemented it successfully.
My code for adding the fragment under view pager
void setUpFragementPager(View view) {
List<Fragment> fragments = new Vector<Fragment>();
MyFragmentAdapter mPagerAdapter;
//for each fragment you want to add to the pager
Bundle page = new Bundle();
page.putString("url", "d");
fragments.add(Fragment.instantiate(getActivity(),
AppFragement.class.getName(), page));
fragments.add(Fragment.instantiate(getActivity(),
CatFragement.class.getName(), page));
fragments.add(Fragment.instantiate(getActivity()
,FavFragement.class.getName(),page));
//after adding all the fragments write the below lines
mViewPager=(ViewPager)view.findViewById(R.id.viewpager);
mPagerAdapter = new
MyFragmentAdapter(super.getFragmentManager(), fragments);
mViewPager.setAdapter(mPagerAdapter);
mSlidingTabLayout = (SlidingTabLayout)
view.findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setViewPager(mViewPager);
if (mParam1.toString().equals("1")){
mViewPager.setCurrentItem(1);
mPagerAdapter.notifyDataSetChanged();
}
else
mViewPager.setCurrentItem(0);
}
Now when i click on MyLibrary button (Image 2)
under drawer the Tab should change to CATEGORY but what happen is currently now is that , white strip is going under the category tab but the CatFragement is not loading under the same tab(category tab). You can see it in the snapshot (image 3)
I used
mViewPager.setCurrentItem(1);
mPagerAdapter.notifyDataSetChanged();
but it leading me to the my desired result . The Desired Result should be look like image4 which you can see in the snapshot too.
Please help me where i am doing wrong.
Related
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
Right now, I have five fragments (tabs) in my app as the image shows. And they are all listed as members of the tab bar in the MainActivity.java
public class MainActivity extends AppCompatActivity implements InputTab.SendMessage, FollowingTab.SendMessage, FollowerTab.SendMessage, ProfileTab.SendMessage {
private SectionsPageAdapter pageAdapter;
ViewPager viewPager;
public static String currentUser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new ProfileTab());
fragmentTransaction.commit();
pageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
// Sets up the ViewPager with the sections adapter
viewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(viewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager((viewPager));
}
// Adds fragments to SectionsPageAdapter and gives names for the corresponding tab
private void setupViewPager(ViewPager viewPager) {
SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new InputTab(), "Search");
adapter.addFragment(new ProfileTab(), "Profile");
adapter.addFragment(new GithubTab(), "Github Repos");
adapter.addFragment(new FollowerTab(), "Followers");
adapter.addFragment(new FollowingTab(), "Followings");
viewPager.setAdapter(adapter);
}
Since I implemented a search tab, I was wondering if it's possible to create another page(or fragment) that displays the result of the search, and when I click the result, I want to make it direct to the corresponding tabs (Profile for users, and Github Repos for repo search).
In short, how can I create a page that is not part of the tab and still display it when I press the 'search' button?
Implement your TabLayout and ViewPager inside a fragment instead of the activiry and replace that fragment with the results fragment.
it works fine for fragments swiping page. But I want to spipe my pages using activity. So please help me how can I rewrite the following code for Activity class?
private void initialisePaging() {
// TODO Auto-generated method stub
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, AboutFragment.class.getName()));
fragments.add(Fragment.instantiate(this, ServiceFragment.class.getName()));
fragments.add(Fragment.instantiate(this,ProcessFragment.class.getName()));
mPagerAdapter =new PagerAdapter(this.getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
pager.setAdapter(mPagerAdapter);
}
But I want like this,
List<ActionBarActivity> ac=new Vector<ActionBarActivity>();
ac.add(ActionBarActivity.INPUT_SERVICE(this, AboutActivity.class.getName()));
ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
pager.setAdapter(mPagerAdapter);
this code is not correct. Please Help
In my opinion,there is no regular way to use activity instead of fragment in ViewPager.
Fragment has nearly the same ability as activity, why you don't use Fragment ?
I am using google's SlidingTabLayout in my view, but i want to add dynamically new tabs to it.
I'm using this http://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.common/view/SlidingTabLayout.html
This is my code which initialize it:
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
//this ^ is a FragmentStatePagerAdapter
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// Assiging the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(mViewPager);
I have absolutely no idea how to add a Tab.
I thought about changing the ViewPager but in the SlidingTabLayout there is a comment:
/**
* Sets the associated view pager. Note that the assumption here is that the pager content
* (number of tabs and tab titles) does not change after this call has been made.
*/
public void setViewPager(ViewPager viewPager) {
There's probably a nicer, more efficient way, but simply calling setViewPager() every time you add a page to your ViewPager will work.
Here is a nice tutorial. http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html
In short, you can supply them with your Pager Adapter. Override the getPageTitle method in order to supply something custom. E.g.
#Override
public CharSequence getPageTitle(int position) {
return _items[position].getDisplayName();
}
I have a FragmentActivity and some fragment in my app.
i need to have Swip ActionBar-Tabs Just inside one fragment , but when i use ActionBar and ViewPager in my project to make Swip ActionBar Tab , make ActionBar in all of fragment No just in one,
hove i can make ActionBar just inside one fragment that not shown in other fragment?
actionabar = getActionBar();
actionabar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
viewpager = (ViewPager) findViewById(R.id.pager);
FragmentManager fm = getSupportFragmentManager();
/** Defining a listener for pageChange */
ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener(){
#Override
public void onPageSelected(int position) {
super.onPageSelected(position);
actionabar.setSelectedNavigationItem(position);
}
};
/** Setting the pageChange listner to the viewPager */
viewpager.setOnPageChangeListener(pageChangeListener);
/** Creating an instance of FragmentPagerAdapter */
MyFragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(fm);
viewpager.setAdapter(fragmentPagerAdapter);
actionabar.setDisplayShowTitleEnabled(true);
Tab tab = actionabar.newTab().setText("My Friends").setTabListener(tabListener);
actionabar.addTab(tab);
tab = actionabar.newTab().setText("Android Version").setTabListener(tabListener);
actionabar.addTab(tab);
tab = actionabar.newTab().setText("Android Phones").setTabListener(tabListener);
actionabar.addTab(tab);
Use TabHost (http://developer.android.com/reference/android/support/v4/app/FragmentTabHost.html) instead of ActionBar navigation mode.