2

I am facing the very weird problem,i found the similar kind of problem over the net but none of the solution helps me to get rid of this.

Problem ::

I am having a one fragment which have 3 tabs,(tabs created using fragment tab host).Now every tabs have different fragment.

From first tab,i have set one fragment where i have used view pager and circle page indicator to display the images with dots(circle).

Now when i swipe the image from 1st to 2nd then 3rd and so on,it works fine. But after reaching on image 2nd,when i click on tab2 and again came back to tab1,my images lost.

Your guidance or any help/links will be appreciated..
Many many thanks in advance.

For your understanding here i am posting code below ::

FragmentwithTabHost ::

public class FragmentwithTabHost extends Fragment{

    private View rootView;
     private FragmentTabHost mTabHost;
     private Context mContext;
    public FragmentwithTabHost() {
    }

    public FragmentwithTabHost(Context ctx) {
        mContext=getActivity();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.mycardswithtabs, container,
                false);

        mTabHost = (FragmentTabHost) rootView.findViewById(android.R.id.tabhost);

        mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);
        mTabHost.setBackground(getActivity().getResources().getDrawable(R.drawable.normal));
        mTabHost.addTab(
                mTabHost.newTabSpec("tab1").setIndicator("tab1", null),
                Fragment1.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec("tab2").setIndicator("tab2", null),
                Fragment2.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec("tab3").setIndicator("tab3", null),
                Fragment3.class, null);
        return rootView;
    }
}

Fragment1 ::

public class Fragment1 extends Fragment{

    //  private Context mContext;
    private View rootView;

    SlidesFragmentAdapter mAdapter;
    ViewPager mPager;
    PageIndicator mIndicator;
    LinearLayout llSponseredImage,llSponserdByText;
    ImageView imgsponser1,imgsponser2,imgsponser3,imgsponser4;
    public Fragment1() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.my_card, container,false);

        mAdapter = new SlidesFragmentAdapter(getActivity()
                .getSupportFragmentManager());

        llSponseredImage=(LinearLayout)rootView.findViewById(R.id.mycardbottomViewSponseredImage);
        llSponserdByText=(LinearLayout)rootView.findViewById(R.id.mycardbottomsponsredby);

        imgsponser1=(ImageView)rootView.findViewById(R.id.imgsponser1);
        imgsponser2=(ImageView)rootView.findViewById(R.id.imgsponser2);
        imgsponser3=(ImageView)rootView.findViewById(R.id.imgsponser3);
        imgsponser4=(ImageView)rootView.findViewById(R.id.imgsponser4);

        mPager = (ViewPager) rootView.findViewById(R.id.pager);
        mPager.setOffscreenPageLimit(4);
        mPager.setClipToPadding(false);
        mPager.setPageMargin(12);
        mPager.setAdapter(mAdapter);

        mIndicator = (CirclePageIndicator) rootView.findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);
        ((CirclePageIndicator) mIndicator).setSnap(true);

        mPager.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("3242353652523");
                MyCardsWithOffersFragment myCardsWithOffersFragment=new MyCardsWithOffersFragment();
            }
        });
        mIndicator
        .setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                /*Toast.makeText(getActivity(),
                        "Changed to page " + position,
                        Toast.LENGTH_SHORT).show();*/
                if(position==0 || position==2){
                    llSponseredImage.setVisibility(View.GONE);
                    llSponserdByText.setVisibility(View.GONE);
                }else if(position==1){
                    llSponseredImage.setVisibility(View.VISIBLE);
                    llSponserdByText.setVisibility(View.VISIBLE);

                    imgsponser1.setImageResource(R.drawable.sponser1);
                    imgsponser2.setImageResource(R.drawable.sponser2);
                    imgsponser3.setImageResource(R.drawable.sponser3);
                    imgsponser4.setImageResource(R.drawable.sponser4);
                }else if(position==3){
                    llSponseredImage.setVisibility(View.VISIBLE);
                    llSponserdByText.setVisibility(View.VISIBLE);

                    imgsponser1.setImageResource(R.drawable.sponser5);
                    imgsponser2.setImageResource(R.drawable.sponser6);
                    imgsponser3.setImageResource(R.drawable.sponser7);
                    imgsponser4.setImageResource(R.drawable.sponser8);
                }
            }

            @Override
            public void onPageScrolled(int position,
                    float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        return rootView;
    }

    public class SlidesFragmentAdapter extends FragmentStatePagerAdapter implements IconPagerAdapter {

        private int[] Images = new int[] { R.drawable.first_image, R.drawable.second_image,
                R.drawable.third_image, R.drawable.four_image

        };

        private int[] ICONS = new int[] { R.drawable.first_image, R.drawable.second_image,
                R.drawable.third_image, R.drawable.four_image };

        private int mCount = Images.length;

        public SlidesFragmentAdapter(android.support.v4.app.FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        @Override
        public Fragment getItem(int position) {
            return new DemoFragment(Images[position]);
        }

        @Override
        public int getCount() {
            return mCount;
        }

        @Override
        public int getIconResId(int index) {
            return ICONS[index % ICONS.length];
        }
        public float GetPageWidth (int position) {
            return 0.93f;
        }  
        public void setCount(int count) {
            if (count > 0 && count <= 10) {
                mCount = count;
                notifyDataSetChanged();
            }
        }
    }
}
AndroidLearner
  • 4,500
  • 4
  • 31
  • 62
  • you can check this http://stackoverflow.com/questions/19331492/how-to-maintain-fragments-state-in-the-application?answertab=votes#tab-top – droid kid Dec 31 '14 at 08:58

1 Answers1

0

I think this can be achieved using show() or add() method. If it is a Activity without Tabhost then its easy. This case while tab switching we have to apply the same logic.

Check this solution

I guess this will give you some idea.

Community
  • 1
  • 1
droid kid
  • 7,569
  • 2
  • 32
  • 37