I'm using PagerSlidingTabStrip library for ViewPager https://github.com/astuetz/PagerSlidingTabStrip. I want to change the fragments while scrolling. I achieved that with the below code.
MainActivity.java
public class MainActivity extends SherlockFragmentActivity {
private MyPagerAdapter adapter;
ViewPager pager;
private PagerSlidingTabStrip tabs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
pager = (ViewPager) findViewById(R.id.pager);
adapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
.getDisplayMetrics());
pager.setPageMargin(pageMargin);
tabs.setViewPager(pager);
}
public class MyPagerAdapter extends FragmentPagerAdapter {
private final String[] TITLES = {"First","Second","Third"};
private SherlockFragment[] fragments = new SherlockFragment[] { new FirstFragment(), new SecondFragment(), new ThirdFragment()};
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
#Override
public int getCount() {
return TITLES.length;
}
#Override
public Fragment getItem(int position) {
return fragments[position];
}
}
}
FirstFragment.java
public class FirstFragment extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_main, container, false);
Toast.makeText(getSherlockActivity(), "This is from first", Toast.LENGTH_SHORT).show();
return v;
}
}
SecondFragment.java
public class SecondFragment extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_main, container, false);
Toast.makeText(getSherlockActivity(), "SecondFragment", Toast.LENGTH_SHORT).show();
return v;
}
}
ThirdFragment.java
public class ThirdFragment extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_main, container, false);
Toast.makeText(getSherlockActivity(), "ThirdFragment", Toast.LENGTH_SHORT).show();
return v;
}
}
I got the three fragments and I'm testing them with Toasts.
When the application opens with the MainActivity, FirstFragment is attached to the Activity , but it shows two toasts, one from Firstragment and other from SecondFragment , and when I scroll to second tab, it shows the toast of ThirdFragment.
So, I figured its going like this. If I scroll from left to right , the fragment right to the current fragment is displayed and if I scroll from right to left, the fragment left to the current fragment is displayed. Please help.
Thanks.
It's because of fragments caching. When you visit some fragments, sibling fragments to it also being created (onCreate() called).
Related
Below is my screenshot and when click on plus button then add one radio button and edit text view in view.
MyAdapter myAdapter;
ViewPager viewPager;
viewPager = (ViewPager) findViewById(R.id.viewPager);
myAdapter = new MyAdapter(getSupportFragmentManager());
viewPager.setAdapter(myAdapter);
public static class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return new Fragment_question();
//return Fragment_question.newInstance(position);
// return null;
}
#Override
public int getCount() {
return PreferenceManager.Constant.create_q_array.length();
}
}
public static class Fragment_question extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.pageradapter, container, false);
findViewbyid(rootView);
return rootView;
}
}
Just create one instance in your Question Fragment and add your childView like below
public class Fragment_question extends Fragment {
private View parentView;
private LinearLayout linearLayout;
#Override public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable #Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
Bundle savedInstanceState) {
parentView = inflater.inflate(R.layout.your_fragment_xml, container, false);
linearLayout = (LinearLayout) parentView.findViewById(R.id.question_layout);
return parentView;
}
// In button Click add the view
private void addEditText() {
View view = View.inflate(getActivity(), R.layout.editext_view, null);
linearLayout.addView(view);
}
}
I am able to get DFragment on top of AFragment and on press back of DFragment I am getting AFragment. But the problem that I am facing is when AFragment is shown,BFragment and CFragment show their respective layouts. But when I traverse from A to D, then B and C show D's layouts only.
I have tried notifyDataSetChanged on the Adapter but still doesn't work properly. Please help
My MainActivity
public class MainActivity extends FragmentActivity {
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence tabTitles[] = {"A", "B", "C"};
int numTabs = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ViewPagerAdapter(getSupportFragmentManager(), tabTitles, numTabs);
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true);
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
#Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tabsScrollColor);
}
});
tabs.setViewPager(pager);
}
}
This is my ViewPagerAdapter
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
CharSequence tabTitles[];
int numTabs;
public ViewPagerAdapter(FragmentManager fm,CharSequence tabTitles[], int numTabs) {
super(fm);
this.tabTitles = tabTitles;
this.numTabs = numTabs;
}
#Override
public Fragment getItem(int position) {
if(position==0)
return new AFragment();
else
if(position==1)
return new BFragment();
else
return new CFragment();
}
#Override
public int getCount() {
return numTabs;
}
#Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}
This is my 0th fragment
public class AFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState){
View rootView =inflater.inflate(R.layout.a,container,false);
Button b = (Button)rootView.findViewById(R.id.b);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
((MainActivity) context).getSupportFragmentManager().beginTransaction().add(R.id.container, new DFragment())
.addToBackStack("").commit();
}
});
return rootView;
}
}
This is the new fragment that comes on top of my 0th fragment
public class DFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_d,null);
return rootView;
}
}
here's a portion of my code:
public class MenuExpenses extends Fragment {
private ViewPager viewPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.expenses,container, false);
viewPager = (ViewPager) root.findViewById(R.id.pager);
viewPager.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
// Bind the tabs to the ViewPager
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) root.findViewById(R.id.tabs);
tabs.setViewPager(viewPager);
return root;
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
private final String[] TITLES = { "Daily", "Recurring" };
#Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return new MenuExpensesDaily();
case 1: return new IncomeList2();
default: return new MenuExpensesDaily();
}
}
}
}
MenuExpensesDaily & IncomeList2 are both fragments. So how can I set the fragment tag for both of them here?
You can override the instantiateItem method, the save the fragment to a SparseArray/HashMap, then create a method to return the fragment with the position. Now you will be able to access your selected your fragment and set the tag to it.
I have been looking and having a hard time finding a clear cut example? I am trying to understand how to create a viewpager within a fragment that is open as a drawer item from my mainActivity...
This is my attempt but I think I am doing something wrong whether it be that I am including it incorrectly or have it in the wrong spot...
public class RandomFragment extends android.app.Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_random, container, false);
return rootView;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ViewPager mViewPager = (ViewPager) view.findViewById(R.id.random_pager);
mViewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return 4;
}
#Override
public Fragment getItem(int position) {
Bundle args = new Bundle();
args.putInt(TextViewFragment.POSITION_KEY, position);
return TextViewFragment.newInstance(args);
}
}
I use this one hope it help you too : https://github.com/thecodepath/android_guides/wiki/ViewPager-with-FragmentPagerAdapter
So you can use this one: https://github.com/astuetz/PagerSlidingTabStrip
I have a FragmentActivity which hosts a FragmentTabHost.
The goal is to have swipeable content in the first tab.
So the first tab hosts a FragmentPagerAdapter and ViewPager.
The app compiles fine but nothing is shown in the first tab and it is not swipeable.
I'm still new to android and I'm stuck. I hope that someone can help me.
Here is my relevant code:
MainActivity:
public class MainActivity extends FragmentActivity implements OnTabChangeListener
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initTabs();
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void initTabs()
{
FragmentTabHost tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(),R.id.realtabcontent);
tabHost.addTab(tabHost.newTabSpec("Übersicht").setIndicator("Übersicht"), OverviewFragment.class, null); // <- shall contain swipeable content
tabHost.addTab(tabHost.newTabSpec("Einstellungen").setIndicator("Einstellungen"), SettingsFragment.class, null); // <- 'static' content
tabHost.setOnTabChangedListener(this);
}
...
}
OverviewFragment (shall host the swipeable content):
public class OverviewFragment extends Fragment
{
ViewPager _pagerView;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
_pagerView = (ViewPager)inflater.inflate(R.layout.tab_overview, container, false).findViewById(R.id.viewpager);
initPager();
return inflater.inflate(R.layout.tab_overview, container, false);
}
private void initPager()
{
FragmentManager fm = getChildFragmentManager();
PageAdapter pageAdapter = new PageAdapter(fm);
_pagerView.setAdapter(pageAdapter);
}
}
PageAdapter:
public class PageAdapter extends FragmentPagerAdapter
{
public PageAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int arg0)
{
MemoryFragment m1 = new MemoryFragment();
return m1;
}
#Override
public int getCount()
{
return 5;
}
}
MemmoryFragment (the fragment that should be shown as a page in the FragmentPagerController):
public class MemoryFragment extends Fragment
{
#Override
public void onCreate(Bundle savedInstanceState) {
}
#Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.memmory, container,false);
TextView tv = (TextView ) v.findViewById(R.id.textViewTest);
tv.setText("You are viewing the page Swipe Horizontally left / right");
return v;
}
}
I know that this code shows nothing meaningful. It should just show 5 swipeable pages with the same text for the start.