I also declare 3 fragment page View As PageView. my app is not responding as soon as i run up there is no error. i reference to this youtube video
https://www.youtube.com/watch?v=NYjgX-GNRGo . I think this code is outdated. Can someone show me how can i correct this?
package com.example.name.scrollable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
public class MainActivity extends FragmentActivity
{
ViewPager mViewPager = null;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) mViewPager.findViewById(R.id.pager);
FragmentManager fragmentManager = getSupportFragmentManager();
mViewPager.setAdapter(new MyFragmentAdapter(fragmentManager));
}
}
class MyFragmentAdapter extends FragmentPagerAdapter
{
public MyFragmentAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int position)
{
Fragment fragment = null;
if(position == 0)
{
fragment = new Fragment_a();
}
if(position == 1)
{
fragment = new Fragment_b();
}
if (position == 2)
{
fragment = new Fragment_c();
}
return fragment;
}
#Override
public int getCount()
{
return 3;
}
}
i also make 3 fragment page show can i show with the help of ViewPager
add this one:
mViewPager = (ViewPager)findViewById(R.id.pager);
instead of this:
mViewPager = (ViewPager) mViewPager.findViewById(R.id.pager);
Related
I have a problem with android a ViewPager setup with FragmentPagerAdapter and TabLayout
TabLayout works fine. But, the code in all tabs run simultaneously. I want the code in the currently selected tab to run
This is my activity
package francesco.prisco.siamostudenti.ui;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import francesco.prisco.siamostudenti.R;
import francesco.prisco.siamostudenti.ui.fragments.CalendarFragment;
import francesco.prisco.siamostudenti.ui.fragments.ChatFragment;
import francesco.prisco.siamostudenti.ui.fragments.EmailFragment;
import francesco.prisco.siamostudenti.ui.fragments.HomeFragment;
import francesco.prisco.siamostudenti.ui.fragments.ProfiloFragment;
public class HomeTabActivity extends AppCompatActivity {
// private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_tab);
// toolbar = (Toolbar) findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private void setupTabIcons() {
int[] tabIcons = {
R.drawable.ic_home,
R.drawable.ic_calendar,
R.drawable.ic_chat,
R.drawable.ic_email,
R.drawable.ic_profile
};
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[3]);
tabLayout.getTabAt(4).setIcon(tabIcons[4]);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new HomeFragment(), "HOME");
adapter.addFrag(new CalendarFragment(), "CALENDAR");
adapter.addFrag(new ChatFragment(), "CHAT");
adapter.addFrag(new EmailFragment(), "EMAIL");
adapter.addFrag(new ProfiloFragment(), "PROFILO");
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 null to display only the icon
return null;
}
}
}
And this is code of a fragment
package francesco.prisco.siamostudenti.ui.fragments;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import francesco.prisco.siamostudenti.R;
import francesco.prisco.siamostudenti.ui.HomeTabActivity;
public class ProfiloFragment extends Fragment {
public ProfiloFragment() {}
#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.fragment_profilo, container, false);
return rootView;
}
}
Help me please
Like #Mrugesh Thaker says, all of the Fragment lifecycle functions - onCreateView(), etc. are executed every time the View of a Fragment is inflated.
Here, the View of each Fragment is inflated every time it becomes the adjacent Fragment of the current Fragment page.
So, you should move the 'code each Fragment needs to execute' when it becomes the currently selected Fragment page into an OnPageChangeListener
In your HomeTabActivity, when you set up the ViewPager, add an
OnPageChangeListener to yourViewPager
private void setupViewPager(ViewPager viewPager) {
.
.
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){
public void onPageScrollStateChanged (int state){
}
public void onPageSelected (int position){
/*
* Define the action to be performed for each page.
* Variable 'position' is the index of the page in the ViewPager
*/
}
public void onPageScrolled (int position, float positionOffset, int positionOffsetPixels){
}
});
}
Learning Resource
Actually, view pager works like that. The main aim of view pager is to enable a user to have smooth swipe , so what it does is , it will create all the fragments that you gave at a time to have smooth swiping flow. This is actually the limitation of it because all the methods of fragment lifecycle are called at a time.
When App starts it's showing first fragment and preparing second one, when I slide to second it's preparing third... When I slide back from second to third everythink is fine but when slide 1 -> 2 -> 3 -> 2 -> 1 the first one content has been reseted... What could be a problem here?
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends AppCompatActivity{
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
FoldersFragment foldersFragment = new FoldersFragment();
NowPlayingFragment nowPlayingFragment = new NowPlayingFragment();
EqFragment eqFragment = new EqFragment();
public ViewPager getMViewPager() {
return mViewPager;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
foldersFragment.setNowPlayingFragment(nowPlayingFragment);
nowPlayingFragment.setFoldersFragment(foldersFragment);
eqFragment.setMediaPlayer(nowPlayingFragment.mp);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
System.out.println("0");
return foldersFragment;
case 1:
System.out.println("1");
return nowPlayingFragment;
case 2:
System.out.println("2");
return eqFragment;
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
}
The default offscreen limit is 1 so each time you go to the next fragment it will check if there is fragment that is already been offscreen and will be restarted when you go back to it:
if you are currently at page 3
1 -> |offset| 2 -> (3) -> 4 -> |offset| 5
so page 1 and page 5 will be restarted if it is before or after the current page + the offset.
To increase the offset you can call:
mViewPager.setOffscreenPageLimit(4)
I'm dealing with and Android app using an activity and 3 fragments with a ViewPager and a FragmentPagerAdapter.
The app is working correctly but I still cannot retrieve the reference of a Fragment within the Activity.
The methods findFragmentById and findFragmentbyTag always return null.
Here's my code:
FragmentPagerAdapter:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class ViewPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public Fragment getItem(int position) {
switch (position) {
// Open FragmentTab1.java
case 0:
FragmentTab1 fragmenttab1 = new FragmentTab1();
return fragmenttab1;
// Open FragmentTab2.java
case 1:
FragmentTab2 fragmenttab2 = new FragmentTab2();
return fragmenttab2;
// Open FragmentTab3.java
case 2:
FragmentTab3 fragmenttab3 = new FragmentTab3();
return fragmenttab3;
}
return null;
}
}
Fragment implementation:
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;
public class FragmentTab1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from fragmenttab1.xml
View view = inflater.inflate(R.layout.fragmenttab1, container, false);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
}
And finally my activity:
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
// Locate the viewpager in activity_main.xml
viewPager = (ViewPager) findViewById(R.id.pager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
// Set the ViewPagerAdapter into ViewPager
viewPager.setAdapter(viewPagerAdapter);
}
#Override
protected void onStart()
{
super.onStart();
InitializeVariables();
viewPager.setCurrentItem(1);
Fragment fragment = (Fragment)getSupportFragmentManager().findFragmentByTag(makeFragmentName(viewPager.getId(), 0));
}
I need to implement Horizontally Scrollable Tabs inside a Fragment inside an already created Activity. But the Inventory class naturally extends Fragment and if I try to extend FragmentActivity from Inventory, it doesn't work.
Inside this:
Put something like this:
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Inventory extends FragmentActivity {
ViewPager viewPager = null;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.inventory);
viewPager = (ViewPager) findViewById(R.id.pager);
FragmentManager fragmentManager = getSupportFragmentManager();
viewPager.setAdapter(new MyAdapter(fragmentManager));
}
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
android.support.v4.app.Fragment fragment = null;
if (position==0){
fragment = new Fa();
}
if (position==1){
fragment = new Fb();
}
if (position==2){
fragment = new Fc();
}
return fragment;
}
#Override
public int getCount() {
return 0;
}
}
Also, I am using this tutorial to implement tabs.
I've done some research on this already, and I seem to understand the solutions suggested out there. This is not a repeat question, I'm just having trouble implementing these solutions to my viewpager/fragment code.
So here is my "FirstFragment.java" that sets up my view pager, and the fragment it calls is "UserFragment.java". Basically what I'm doing is providing a user defined number of forms created as pages in my view pager.
I of course, am experiencing the orientation change issue, where fragment gets created a couple times, and eventually lost. I would like to fix it.
My code for FirstFragment.java is:
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;
public class FirstFragment extends FragmentActivity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_viewpager);
// Check whether the activity is using the layout version with
// the fragment_container FrameLayout. If so, we must add the first fragment
int size = getIntent().getExtras().getInt("numass1");
ViewPager vp = (ViewPager) findViewById(R.id.viewPager);
if (savedInstanceState != null) {
return;
}
ArrayList<Fragment> lf = new ArrayList<Fragment>();
for(int count=0; count<size; count ++){
Fragment f = new UserFragment();
lf.add(f);
}
FragmentAdapter hello = new FragmentAdapter(getSupportFragmentManager() , lf);
vp.setAdapter(hello);
}
SparseArray<Question> questions = new SparseArray<Question>();
public void saveData(Question quest){
ViewPager vp = (ViewPager) findViewById(R.id.viewPager);
questions.put(vp.getCurrentItem(), quest);
}
public ViewPager getPager() {
ViewPager vp = (ViewPager) findViewById(R.id.viewPager);
return vp;
}
}
Just incase, my FragmentAdapter.java code is:
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;
public class FragmentAdapter extends FragmentPagerAdapter{
private List<Fragment> fragments;
public FragmentAdapter(FragmentManager fragmentManager, ArrayList<Fragment> fragments) {
super(fragmentManager);
this.fragments = fragments;
}
#Override
public Fragment getItem(int position) {
Fragment fragment = fragments.get(position);
Bundle args = new Bundle();
args.putInt("page_position", position + 1);
args.putInt("size", fragments.size());
fragment.setArguments(args);
return fragment;
}
#Override
public void destroyItem (ViewGroup container, int position, Object object)
{
super.destroyItem(container, position, object);
}
#Override
public int getCount() {
return this.fragments.size();
}
}
Please keep in mind, I am clearly a noob, and I'm trying to learn. So code snippets, and explanations would be awesome!
Thank you very much for your time and help.
EDIT: Please help ...
If you add
android:configChanges="orientation|keyboardHidden"
in <activity> node of your file AndroidManifest.xml your activity will not reset when the orientation changed.
Is it what you wanted?