ViewPager and his fragments2019 Community Moderator ElectiononActivityResult is not being called in FragmentfindViewById in FragmentViewPager and fragments — what's the right way to store fragment's state?Using context in a fragmentBest practice for instantiating a new Android FragmentHow to determine when Fragment becomes visible in ViewPagerWhy fragments, and when to use fragments instead of activities?Dilemma: when to use Fragments vs Activities:Disable Tabs in TabLayoutOnClickListener not working with fragments

Are Roman Catholic priests ever addressed as pastor

Why does a Star of David appear at a rally with Francisco Franco?

How do I change two letters closest to a string and one letter immediately after a string using Notepad++?

New passport but visa is in old (lost) passport

How should I state my peer review experience in the CV?

What exactly is this small puffer fish doing and how did it manage to accomplish such a feat?

"of which" is correct here?

How to make healing in an exploration game interesting

Have the tides ever turned twice on any open problem?

Why do passenger jet manufacturers design their planes with stall prevention systems?

Bach's Toccata and Fugue in D minor breaks the "no parallel octaves" rule?

Examples of transfinite towers

What is the significance behind "40 days" that often appears in the Bible?

Why is the President allowed to veto a cancellation of emergency powers?

Violin - Can double stops be played when the strings are not next to each other?

If I can solve Sudoku, can I solve the Travelling Salesman Problem (TSP)? If so, how?

Why did it take so long to abandon sail after steamships were demonstrated?

This word with a lot of past tenses

Why Choose Less Effective Armour Types?

Print a physical multiplication table

What is the purpose or proof behind chain rule?

How do I hide Chekhov's Gun?

A single argument pattern definition applies to multiple-argument patterns?

Why do tuner card drivers fail to build after kernel update to 4.4.0-143-generic?



ViewPager and his fragments



2019 Community Moderator ElectiononActivityResult is not being called in FragmentfindViewById in FragmentViewPager and fragments — what's the right way to store fragment's state?Using context in a fragmentBest practice for instantiating a new Android FragmentHow to determine when Fragment becomes visible in ViewPagerWhy fragments, and when to use fragments instead of activities?Dilemma: when to use Fragments vs Activities:Disable Tabs in TabLayoutOnClickListener not working with fragments










0















Like a lot of other people on the stackoverflow, for example:
https://ru.stackoverflow.com/questions/571704/%D0%A0%D0%B0%D0%B7%D0%BD%D1%8B%D0%B9-%D0%BA%D0%BE%D0%BD%D1%82%D0%B5%D0%BD%D1%82-%D0%BD%D0%B0-%D1%81%D1%82%D1%80%D0%B0%D0%BD%D0%B8%D1%86%D0%B0%D1%85-viewpager



I'm trying to create ViewPager with different layouts, but with:
https://github.com/ogaclejapan/SmartTabLayout



Expectation: beautiful ViewPager with 3 tabs, which can switch different layouts/fragments by clicking on the buttons or by swipe.



Reality: beautiful ViewPager with 3 tabs, which are doing nothing.



Looking for your help, thanks.



What I have done in MainActivity class:



public class MainActivity extends AppCompatActivity

protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setElevation(0); //toolbar shadow

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

ViewGroup tab = findViewById(R.id.linear_test);
tab.addView(LayoutInflater.from(this).inflate(R.layout.activity_main, tab, false));

ViewPager viewPager = findViewById(R.id.viewpager);
SmartTabLayout viewPagerTab = findViewById(R.id.viewpagertab);
setup(viewPagerTab);

FragmentPagerItems pages = new FragmentPagerItems(this);

pages.add(FragmentPagerItem.of("Вкладка 1", DemoFragment.class));
pages.add(FragmentPagerItem.of("Вкладка 2", DemoFragment.class));
pages.add(FragmentPagerItem.of("Вкладка 3", DemoFragment.class));

FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
getSupportFragmentManager(), pages);

viewPager.setAdapter(adapter);
viewPagerTab.setViewPager(viewPager);

public void setup(final SmartTabLayout layout)
//Do nothing.




What I have done in my DemoFragment:



public class DemoFragment extends Fragment 

static final String ARGUMENT_PAGE_NUMBER = "arg_page_number";

private int page;

static DemoFragment newInstance(int page)
DemoFragment pageFragment = new DemoFragment();
Bundle arguments = new Bundle();
arguments.putInt(ARGUMENT_PAGE_NUMBER, page);
pageFragment.setArguments(arguments);
return pageFragment;


@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState)
// View view = inflater.inflate(R.layout.activity_main, container, false);
page = getArguments().getInt("someInt", 0);
return inflater.inflate(R.layout.activity_main, container, false);
// return view;


@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
int position = FragmentPagerItem.getPosition(getArguments());
getItem(position);

public Fragment getItem(int position)
switch (position)
case 0: // Fragment # 0 - This will show FirstFragment
return DemoFragment.newInstance(1);
case 1: // Fragment # 0 - This will show FirstFragment different title
return DemoFragment.newInstance(2);
case 2: // Fragment # 1 - This will show SecondFragment
return DemoFragment.newInstance(3);
default:
return null;






Here is activity_main.xml:



<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">



<LinearLayout
android:id="@+id/linear_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints"
android:background="@color/colorPrimary"
android:orientation="horizontal" >



<com.ogaclejapan.smarttablayout.SmartTabLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/viewpagertab"
android:layout_width="match_parent"
android:layout_height="@dimen/tab_height_large"
app:stl_customTabTextLayoutId="@layout/test_layout"
app:stl_customTabTextViewId="@id/custom_tab_text"
app:stl_distributeEvenly="true"
app:stl_defaultTabTextAllCaps="false"
app:stl_indicatorInterpolation="linear"
app:stl_defaultTabTextColor="@color/white"
app:stl_indicatorColor="@color/colorBlue"
app:stl_underlineColor="@color/colorBlue"
app:stl_defaultTabBackground="@color/colorPrimary"
app:stl_indicatorThickness="3dp"
app:stl_underlineThickness="1dp"
/>

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/viewpagertab"
/>


</LinearLayout>


<android.support.v7.widget.RecyclerView
android:id="@+id/playlist_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linear_test" />

<TextView
android:id="@+id/dl_params"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:visibility="gone"
android:layout_weight="1"
tools:ignore="MissingConstraints"/>



</android.support.constraint.ConstraintLayout>


And finally test_layout which one is filling those SmartTabLayout:



<?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:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
>

<android.support.v4.widget.Space
android:id="@+id/center_anchor"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent="true"
/>

<aectann.pr1.TintableImageView

android:id="@+id/custom_tab_icon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_above="@id/center_anchor"
android:layout_centerHorizontal="true"
android:scaleType="center"
android:src="@drawable/custom_icon"
/>

<TextView
android:id="@+id/custom_tab_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/center_anchor"
android:layout_centerHorizontal="true"
android:layout_marginTop="2dp"
android:maxLines="1"
android:textSize="14sp"
android:textColor="@color/white"
/>


</RelativeLayout>


Any ideas, guys?










share|improve this question
























  • You should use different fragment not the demofragment. switch (position) case 0: return xFragment case 1: return yFragment case 2: return zFragment default: return null;

    – Arutha
    Mar 6 at 20:56












  • @Arutha, don't understand:( DemoFragment - this is just class which extends Fragment. Can you explain my problem in other way?

    – Alexander Vasilchuk
    Mar 6 at 21:03











  • any ideas, guys?)

    – Alexander Vasilchuk
    Mar 7 at 19:21















0















Like a lot of other people on the stackoverflow, for example:
https://ru.stackoverflow.com/questions/571704/%D0%A0%D0%B0%D0%B7%D0%BD%D1%8B%D0%B9-%D0%BA%D0%BE%D0%BD%D1%82%D0%B5%D0%BD%D1%82-%D0%BD%D0%B0-%D1%81%D1%82%D1%80%D0%B0%D0%BD%D0%B8%D1%86%D0%B0%D1%85-viewpager



I'm trying to create ViewPager with different layouts, but with:
https://github.com/ogaclejapan/SmartTabLayout



Expectation: beautiful ViewPager with 3 tabs, which can switch different layouts/fragments by clicking on the buttons or by swipe.



Reality: beautiful ViewPager with 3 tabs, which are doing nothing.



Looking for your help, thanks.



What I have done in MainActivity class:



public class MainActivity extends AppCompatActivity

protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setElevation(0); //toolbar shadow

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

ViewGroup tab = findViewById(R.id.linear_test);
tab.addView(LayoutInflater.from(this).inflate(R.layout.activity_main, tab, false));

ViewPager viewPager = findViewById(R.id.viewpager);
SmartTabLayout viewPagerTab = findViewById(R.id.viewpagertab);
setup(viewPagerTab);

FragmentPagerItems pages = new FragmentPagerItems(this);

pages.add(FragmentPagerItem.of("Вкладка 1", DemoFragment.class));
pages.add(FragmentPagerItem.of("Вкладка 2", DemoFragment.class));
pages.add(FragmentPagerItem.of("Вкладка 3", DemoFragment.class));

FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
getSupportFragmentManager(), pages);

viewPager.setAdapter(adapter);
viewPagerTab.setViewPager(viewPager);

public void setup(final SmartTabLayout layout)
//Do nothing.




What I have done in my DemoFragment:



public class DemoFragment extends Fragment 

static final String ARGUMENT_PAGE_NUMBER = "arg_page_number";

private int page;

static DemoFragment newInstance(int page)
DemoFragment pageFragment = new DemoFragment();
Bundle arguments = new Bundle();
arguments.putInt(ARGUMENT_PAGE_NUMBER, page);
pageFragment.setArguments(arguments);
return pageFragment;


@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState)
// View view = inflater.inflate(R.layout.activity_main, container, false);
page = getArguments().getInt("someInt", 0);
return inflater.inflate(R.layout.activity_main, container, false);
// return view;


@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
int position = FragmentPagerItem.getPosition(getArguments());
getItem(position);

public Fragment getItem(int position)
switch (position)
case 0: // Fragment # 0 - This will show FirstFragment
return DemoFragment.newInstance(1);
case 1: // Fragment # 0 - This will show FirstFragment different title
return DemoFragment.newInstance(2);
case 2: // Fragment # 1 - This will show SecondFragment
return DemoFragment.newInstance(3);
default:
return null;






Here is activity_main.xml:



<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">



<LinearLayout
android:id="@+id/linear_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints"
android:background="@color/colorPrimary"
android:orientation="horizontal" >



<com.ogaclejapan.smarttablayout.SmartTabLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/viewpagertab"
android:layout_width="match_parent"
android:layout_height="@dimen/tab_height_large"
app:stl_customTabTextLayoutId="@layout/test_layout"
app:stl_customTabTextViewId="@id/custom_tab_text"
app:stl_distributeEvenly="true"
app:stl_defaultTabTextAllCaps="false"
app:stl_indicatorInterpolation="linear"
app:stl_defaultTabTextColor="@color/white"
app:stl_indicatorColor="@color/colorBlue"
app:stl_underlineColor="@color/colorBlue"
app:stl_defaultTabBackground="@color/colorPrimary"
app:stl_indicatorThickness="3dp"
app:stl_underlineThickness="1dp"
/>

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/viewpagertab"
/>


</LinearLayout>


<android.support.v7.widget.RecyclerView
android:id="@+id/playlist_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linear_test" />

<TextView
android:id="@+id/dl_params"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:visibility="gone"
android:layout_weight="1"
tools:ignore="MissingConstraints"/>



</android.support.constraint.ConstraintLayout>


And finally test_layout which one is filling those SmartTabLayout:



<?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:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
>

<android.support.v4.widget.Space
android:id="@+id/center_anchor"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent="true"
/>

<aectann.pr1.TintableImageView

android:id="@+id/custom_tab_icon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_above="@id/center_anchor"
android:layout_centerHorizontal="true"
android:scaleType="center"
android:src="@drawable/custom_icon"
/>

<TextView
android:id="@+id/custom_tab_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/center_anchor"
android:layout_centerHorizontal="true"
android:layout_marginTop="2dp"
android:maxLines="1"
android:textSize="14sp"
android:textColor="@color/white"
/>


</RelativeLayout>


Any ideas, guys?










share|improve this question
























  • You should use different fragment not the demofragment. switch (position) case 0: return xFragment case 1: return yFragment case 2: return zFragment default: return null;

    – Arutha
    Mar 6 at 20:56












  • @Arutha, don't understand:( DemoFragment - this is just class which extends Fragment. Can you explain my problem in other way?

    – Alexander Vasilchuk
    Mar 6 at 21:03











  • any ideas, guys?)

    – Alexander Vasilchuk
    Mar 7 at 19:21













0












0








0








Like a lot of other people on the stackoverflow, for example:
https://ru.stackoverflow.com/questions/571704/%D0%A0%D0%B0%D0%B7%D0%BD%D1%8B%D0%B9-%D0%BA%D0%BE%D0%BD%D1%82%D0%B5%D0%BD%D1%82-%D0%BD%D0%B0-%D1%81%D1%82%D1%80%D0%B0%D0%BD%D0%B8%D1%86%D0%B0%D1%85-viewpager



I'm trying to create ViewPager with different layouts, but with:
https://github.com/ogaclejapan/SmartTabLayout



Expectation: beautiful ViewPager with 3 tabs, which can switch different layouts/fragments by clicking on the buttons or by swipe.



Reality: beautiful ViewPager with 3 tabs, which are doing nothing.



Looking for your help, thanks.



What I have done in MainActivity class:



public class MainActivity extends AppCompatActivity

protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setElevation(0); //toolbar shadow

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

ViewGroup tab = findViewById(R.id.linear_test);
tab.addView(LayoutInflater.from(this).inflate(R.layout.activity_main, tab, false));

ViewPager viewPager = findViewById(R.id.viewpager);
SmartTabLayout viewPagerTab = findViewById(R.id.viewpagertab);
setup(viewPagerTab);

FragmentPagerItems pages = new FragmentPagerItems(this);

pages.add(FragmentPagerItem.of("Вкладка 1", DemoFragment.class));
pages.add(FragmentPagerItem.of("Вкладка 2", DemoFragment.class));
pages.add(FragmentPagerItem.of("Вкладка 3", DemoFragment.class));

FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
getSupportFragmentManager(), pages);

viewPager.setAdapter(adapter);
viewPagerTab.setViewPager(viewPager);

public void setup(final SmartTabLayout layout)
//Do nothing.




What I have done in my DemoFragment:



public class DemoFragment extends Fragment 

static final String ARGUMENT_PAGE_NUMBER = "arg_page_number";

private int page;

static DemoFragment newInstance(int page)
DemoFragment pageFragment = new DemoFragment();
Bundle arguments = new Bundle();
arguments.putInt(ARGUMENT_PAGE_NUMBER, page);
pageFragment.setArguments(arguments);
return pageFragment;


@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState)
// View view = inflater.inflate(R.layout.activity_main, container, false);
page = getArguments().getInt("someInt", 0);
return inflater.inflate(R.layout.activity_main, container, false);
// return view;


@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
int position = FragmentPagerItem.getPosition(getArguments());
getItem(position);

public Fragment getItem(int position)
switch (position)
case 0: // Fragment # 0 - This will show FirstFragment
return DemoFragment.newInstance(1);
case 1: // Fragment # 0 - This will show FirstFragment different title
return DemoFragment.newInstance(2);
case 2: // Fragment # 1 - This will show SecondFragment
return DemoFragment.newInstance(3);
default:
return null;






Here is activity_main.xml:



<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">



<LinearLayout
android:id="@+id/linear_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints"
android:background="@color/colorPrimary"
android:orientation="horizontal" >



<com.ogaclejapan.smarttablayout.SmartTabLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/viewpagertab"
android:layout_width="match_parent"
android:layout_height="@dimen/tab_height_large"
app:stl_customTabTextLayoutId="@layout/test_layout"
app:stl_customTabTextViewId="@id/custom_tab_text"
app:stl_distributeEvenly="true"
app:stl_defaultTabTextAllCaps="false"
app:stl_indicatorInterpolation="linear"
app:stl_defaultTabTextColor="@color/white"
app:stl_indicatorColor="@color/colorBlue"
app:stl_underlineColor="@color/colorBlue"
app:stl_defaultTabBackground="@color/colorPrimary"
app:stl_indicatorThickness="3dp"
app:stl_underlineThickness="1dp"
/>

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/viewpagertab"
/>


</LinearLayout>


<android.support.v7.widget.RecyclerView
android:id="@+id/playlist_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linear_test" />

<TextView
android:id="@+id/dl_params"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:visibility="gone"
android:layout_weight="1"
tools:ignore="MissingConstraints"/>



</android.support.constraint.ConstraintLayout>


And finally test_layout which one is filling those SmartTabLayout:



<?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:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
>

<android.support.v4.widget.Space
android:id="@+id/center_anchor"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent="true"
/>

<aectann.pr1.TintableImageView

android:id="@+id/custom_tab_icon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_above="@id/center_anchor"
android:layout_centerHorizontal="true"
android:scaleType="center"
android:src="@drawable/custom_icon"
/>

<TextView
android:id="@+id/custom_tab_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/center_anchor"
android:layout_centerHorizontal="true"
android:layout_marginTop="2dp"
android:maxLines="1"
android:textSize="14sp"
android:textColor="@color/white"
/>


</RelativeLayout>


Any ideas, guys?










share|improve this question
















Like a lot of other people on the stackoverflow, for example:
https://ru.stackoverflow.com/questions/571704/%D0%A0%D0%B0%D0%B7%D0%BD%D1%8B%D0%B9-%D0%BA%D0%BE%D0%BD%D1%82%D0%B5%D0%BD%D1%82-%D0%BD%D0%B0-%D1%81%D1%82%D1%80%D0%B0%D0%BD%D0%B8%D1%86%D0%B0%D1%85-viewpager



I'm trying to create ViewPager with different layouts, but with:
https://github.com/ogaclejapan/SmartTabLayout



Expectation: beautiful ViewPager with 3 tabs, which can switch different layouts/fragments by clicking on the buttons or by swipe.



Reality: beautiful ViewPager with 3 tabs, which are doing nothing.



Looking for your help, thanks.



What I have done in MainActivity class:



public class MainActivity extends AppCompatActivity

protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setElevation(0); //toolbar shadow

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

ViewGroup tab = findViewById(R.id.linear_test);
tab.addView(LayoutInflater.from(this).inflate(R.layout.activity_main, tab, false));

ViewPager viewPager = findViewById(R.id.viewpager);
SmartTabLayout viewPagerTab = findViewById(R.id.viewpagertab);
setup(viewPagerTab);

FragmentPagerItems pages = new FragmentPagerItems(this);

pages.add(FragmentPagerItem.of("Вкладка 1", DemoFragment.class));
pages.add(FragmentPagerItem.of("Вкладка 2", DemoFragment.class));
pages.add(FragmentPagerItem.of("Вкладка 3", DemoFragment.class));

FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
getSupportFragmentManager(), pages);

viewPager.setAdapter(adapter);
viewPagerTab.setViewPager(viewPager);

public void setup(final SmartTabLayout layout)
//Do nothing.




What I have done in my DemoFragment:



public class DemoFragment extends Fragment 

static final String ARGUMENT_PAGE_NUMBER = "arg_page_number";

private int page;

static DemoFragment newInstance(int page)
DemoFragment pageFragment = new DemoFragment();
Bundle arguments = new Bundle();
arguments.putInt(ARGUMENT_PAGE_NUMBER, page);
pageFragment.setArguments(arguments);
return pageFragment;


@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState)
// View view = inflater.inflate(R.layout.activity_main, container, false);
page = getArguments().getInt("someInt", 0);
return inflater.inflate(R.layout.activity_main, container, false);
// return view;


@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
int position = FragmentPagerItem.getPosition(getArguments());
getItem(position);

public Fragment getItem(int position)
switch (position)
case 0: // Fragment # 0 - This will show FirstFragment
return DemoFragment.newInstance(1);
case 1: // Fragment # 0 - This will show FirstFragment different title
return DemoFragment.newInstance(2);
case 2: // Fragment # 1 - This will show SecondFragment
return DemoFragment.newInstance(3);
default:
return null;






Here is activity_main.xml:



<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">



<LinearLayout
android:id="@+id/linear_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints"
android:background="@color/colorPrimary"
android:orientation="horizontal" >



<com.ogaclejapan.smarttablayout.SmartTabLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/viewpagertab"
android:layout_width="match_parent"
android:layout_height="@dimen/tab_height_large"
app:stl_customTabTextLayoutId="@layout/test_layout"
app:stl_customTabTextViewId="@id/custom_tab_text"
app:stl_distributeEvenly="true"
app:stl_defaultTabTextAllCaps="false"
app:stl_indicatorInterpolation="linear"
app:stl_defaultTabTextColor="@color/white"
app:stl_indicatorColor="@color/colorBlue"
app:stl_underlineColor="@color/colorBlue"
app:stl_defaultTabBackground="@color/colorPrimary"
app:stl_indicatorThickness="3dp"
app:stl_underlineThickness="1dp"
/>

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/viewpagertab"
/>


</LinearLayout>


<android.support.v7.widget.RecyclerView
android:id="@+id/playlist_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linear_test" />

<TextView
android:id="@+id/dl_params"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:visibility="gone"
android:layout_weight="1"
tools:ignore="MissingConstraints"/>



</android.support.constraint.ConstraintLayout>


And finally test_layout which one is filling those SmartTabLayout:



<?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:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
>

<android.support.v4.widget.Space
android:id="@+id/center_anchor"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent="true"
/>

<aectann.pr1.TintableImageView

android:id="@+id/custom_tab_icon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_above="@id/center_anchor"
android:layout_centerHorizontal="true"
android:scaleType="center"
android:src="@drawable/custom_icon"
/>

<TextView
android:id="@+id/custom_tab_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/center_anchor"
android:layout_centerHorizontal="true"
android:layout_marginTop="2dp"
android:maxLines="1"
android:textSize="14sp"
android:textColor="@color/white"
/>


</RelativeLayout>


Any ideas, guys?







java android android-layout android-fragments






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 19:19







Alexander Vasilchuk

















asked Mar 6 at 20:51









Alexander VasilchukAlexander Vasilchuk

73110




73110












  • You should use different fragment not the demofragment. switch (position) case 0: return xFragment case 1: return yFragment case 2: return zFragment default: return null;

    – Arutha
    Mar 6 at 20:56












  • @Arutha, don't understand:( DemoFragment - this is just class which extends Fragment. Can you explain my problem in other way?

    – Alexander Vasilchuk
    Mar 6 at 21:03











  • any ideas, guys?)

    – Alexander Vasilchuk
    Mar 7 at 19:21

















  • You should use different fragment not the demofragment. switch (position) case 0: return xFragment case 1: return yFragment case 2: return zFragment default: return null;

    – Arutha
    Mar 6 at 20:56












  • @Arutha, don't understand:( DemoFragment - this is just class which extends Fragment. Can you explain my problem in other way?

    – Alexander Vasilchuk
    Mar 6 at 21:03











  • any ideas, guys?)

    – Alexander Vasilchuk
    Mar 7 at 19:21
















You should use different fragment not the demofragment. switch (position) case 0: return xFragment case 1: return yFragment case 2: return zFragment default: return null;

– Arutha
Mar 6 at 20:56






You should use different fragment not the demofragment. switch (position) case 0: return xFragment case 1: return yFragment case 2: return zFragment default: return null;

– Arutha
Mar 6 at 20:56














@Arutha, don't understand:( DemoFragment - this is just class which extends Fragment. Can you explain my problem in other way?

– Alexander Vasilchuk
Mar 6 at 21:03





@Arutha, don't understand:( DemoFragment - this is just class which extends Fragment. Can you explain my problem in other way?

– Alexander Vasilchuk
Mar 6 at 21:03













any ideas, guys?)

– Alexander Vasilchuk
Mar 7 at 19:21





any ideas, guys?)

– Alexander Vasilchuk
Mar 7 at 19:21












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55031929%2fviewpager-and-his-fragments%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55031929%2fviewpager-and-his-fragments%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко