使用pagerAdapter在片段之间传输数据

时间:2014-09-19 01:08:45

标签: android android-fragments

当前替换的片段。

public class SlidingTabsFragment extends Fragment {

private SlidingTabLayout mSlidingTabLayout;
private ViewPager mViewPager;

public SlidingTabsFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    return inflater.inflate(R.layout.fragment_sample, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

    Context context= getActivity();
    mViewPager = (ViewPager) view.findViewById(R.id.viewpager);

    FragmentManager fm = getFragmentManager();
    mViewPager.setAdapter(new Harkayin_PagerAdapter(fm,context));

    mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
    mSlidingTabLayout.setViewPager(mViewPager);
}



 ...

这是PagerAdapter,它包含三个具有相同片段/ Fragment_Hashvehamarner()的滑动页面; /

public class Harkayin_PagerAdapter extends FragmentStatePagerAdapter {

private Context mContext;

public Harkayin_PagerAdapter(FragmentManager fm, Context context) {
    super(fm);
    mContext = context;

}

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

@Override
public CharSequence getPageTitle(int position) {
    String[] tabs= mContext.getResources().getStringArray(R.array.hashvehamar_tabs);
    return tabs[position];
}

@Override
public Fragment getItem(int position) {
    Fragment fragment = null;
    switch (position){
        case 0:
            fragment = new Fragment_Hashvehamarner();
            break;
        case 1:
            fragment = new Fragment_Hashvehamarner();
            break;
        case 2:
            fragment = new Fragment_Hashvehamarner();
            break;
        default:
            break;
    }
    if (fragment != null){
        return fragment;
    }else {
        // error in creating fragment
        Log.e("Harkayin_PagerAdapter", "Error in creating fragment");
        return null;
        }
    }
}

最后是页面持有者片段

public class Fragment_Hashvehamarner  extends Fragment {

ExpandableListAdapter listAdapter;
ExpandableListView expListView;
HashMap<String, List<String[]>> listDataChild;
ArrayList<String[]> listHeaderData;
View rootView;

public Fragment_Hashvehamarner() {}

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

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // get the listview
    expListView = (ExpandableListView) rootView.findViewById(R.id.lvExp);
    // preparing list data
    prepareListData();
    listAdapter = new ExpandableListAdapter(rootView.getContext(), listHeaderData,
            listDataChild);
    // setting list adapter
    expListView.setAdapter(listAdapter);
    // Inflate the layout for this fragment


private void prepareListData() {

    DataAdapter mDbHelper = new DataAdapter(rootView.getContext());
    mDbHelper.createDatabase();
    mDbHelper.open();
    listHeaderData = new ArrayList<String[]>(mDbHelper.getHashvehamarHeader(1));
    listDataChild = new HashMap<String, List<String[]>>(mDbHelper.getHashvehamarChildData(1));
    mDbHelper.close();
}

现在的问题是:如何从PageAdapter获取当前页面位置到当前页面持有者片段/ Fragment_Hashvehamarner()/。

1 个答案:

答案 0 :(得分:1)

将以下方法添加到Fragment_Hashvehamarner班级。

public static final String EXTRA_PAGE_POSITION = "page.position";

public static Fragment_Hashvehamarner newInstance(int id) {
        Bundle args = new Bundle();
        args.putInt(EXTRA_PAGE_POSITION, id);
        Fragment_Hashvehamarner fragment = new Fragment_Hashvehamarner();
        fragment.setArguments(args);        
        return fragment;
    }

以及onCreate的{​​{1}}方法:

Fragment_Hashvehamarner

然后在您的适配器中:

int mSelectedPosition= getArguments().getInt(EXTRA_PAGE_POSITION);