在viewpager中更新数据

时间:2014-03-04 16:10:27

标签: android android-viewpager

我如何在我的案例中更新数据

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;
    }
}

}

和片段代码

public class FirstFragment extends Fragment {

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

    TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
    tv.setText(getArguments().getString("msg"));

    return v;
}

public static FirstFragment newInstance(String text) {

    FirstFragment f = new FirstFragment();
    Bundle b = new Bundle();
    b.putString("msg", text);

    f.setArguments(b);

    return f;
}

}

0 个答案:

没有答案