B
B
Benderlidze2014-03-04 17:02:57
Android
Benderlidze, 2014-03-04 17:02:57

How to update data in viewpager?

Some hell with this pager.
I can’t figure out how to update it, they write, it’s possible, but it doesn’t seem right, everything is being rebuilt again.

@Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

Here is the complete code
public class SpaFragment extends Fragment  {

    public SpaFragment(){}
    private SpaList mSpa;
    private TabHost tabs;

    private View rootView;
    private ViewPager pager;
    private PagerAdapter pagerAdapter;

    public void setSpa(SpaList s)
    {
        mSpa=s;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.spa_tab_fragment, container, false);

        FragmentManager f = ((FragmentActivity) getActivity()).getSupportFragmentManager();
        pagerAdapter = new MyPagerAdapter( f );
        pager = (ViewPager) rootView.findViewById(R.id.pager);
        pager.setAdapter(pagerAdapter);
        pagerAdapter.notifyDataSetChanged();
        Log.d(""+mSpa.getSpaName(),"");
        return rootView;
    }

    private class MyPagerAdapter extends FragmentStatePagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Resources res = getResources();
            String[] title = res.getStringArray(R.array.title_spa_viewpager_array);
            return title[position];
        }

        @Override
        public android.support.v4.app.Fragment getItem(int pos) {
            switch(pos) {
                case 0: return FirstFragment.newInstance(mSpa.getSpaName());
                case 1: return SecondFragment.newInstance(mSpa.getSpaAddr());
                case 2: return SecondFragment.newInstance(mSpa.getCityEngName());
                default: return FirstFragment.newInstance(mSpa.getSpaPhone());
            }
        }

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


}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mintormo, 2014-03-04
@Mintormo

That's pretty much how I've done it.
Adapter for ViewPager

public class PagerAdapter extends FragmentPagerAdapter
{
    private Fragment[] fragments;
    private String[] titles;

    public PagerAdapter(FragmentManager fman, Context con)
    {
        super(fman);
        fragments = new Fragment[3];

        // Каждой страничке ViewPager соответствует свой фрагмент.
        // Тут я создаю объекты и забиваю их в массив
        fragments[0] = new SettingsFragment();
        fragments[1] = new PassGenFragment();
        fragments[2] = new HistoryFragment();

        titles = new String[3];
        titles[0] = con.getString(R.string.settings);
        titles[1] = con.getString(R.string.main_fragment);
        titles[2] = con.getString(R.string.history_fragment);
    }

    public Fragment getItem(int pos)
    {	
        return fragments[pos];
    }

    public int getCount()
    {
        return fragments.length;
    }

    public CharSequence getPageTitle(int position)
    {
        return titles[position];
    }
}

In the main activity, in the onCreate() method, I write the following code:
...
        p_adapter = new PagerAdapter(getSupportFragmentManager(),
                                     getApplicationContext());
        pager = (ViewPager)findViewById(R.id.viewpager);
        pager.setAdapter(p_adapter);
        pager.setCurrentItem(1);
...

Fragments are created as usual. Here is an example class.
public class PassGenFragment extends Fragment
{	

public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }

    public View onCreateView(LayoutInflater inf, ViewGroup cont, Bundle savedState)
    {
        View view = inf.inflate(R.layout.pass_gen_layout,null);
        return view;
    }
}

It's all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question