android - 在ViewPager中使用相同的片段类

时间:2015-08-07 10:49:55

标签: android android-fragments android-viewpager

我有一个var el = $('system-message-container'); (function(){ el.tween('height', [20, 200]); }) .delay(2000); (function(){ el.fade('out').get('tween', {duration: 'long'}) }) .delay(4000); ,我希望在每个页面中使用相同的ViewPager并使用不同的变量。但是当我这样做时,只有一页ViewPager正在查看,其他页面是空白的。我怎样才能做到这一点?我无法为每个标签创建不同的布局,因为我想动态添加它们。

ViewPagerAdapter:

Fragment

包含ViewPager的主片段:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

class FragmentStoreTabPageAdapter extends FragmentPagerAdapter {
    String[] pageTitles = {"Y","Z","X"};
    public FragmentStoreTabPageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return FragmentStorePages.newInstance(pageTitles[position],position);
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        return pageTitles[position];
    }
}

ViewPager中使用的片段:

public class FragmentStore extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_store,container,false);
        ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager_store);
        viewPager.setAdapter(new FragmentStoreTabPageAdapter(getChildFragmentManager()));
        TabPageIndicator tpi = (TabPageIndicator) view.findViewById(R.id.indicator_store);
        tpi.setViewPager(viewPager);
        return view;
    }

}

1 个答案:

答案 0 :(得分:0)

使用此PagerAdapter

class FragmentStoreTabPageAdapter extends FragmentPagerAdapter {
    String[] pageTitles = {"Y","Z","X"};
    public FragmentStoreTabPageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        FragmentStorePages fsp = new FragmentStorePages();
        Bundle args = new Bundle();
        args.putString("categoryName", categoryName);
        args.putInt("categoryId", categoryId);
        fsp.setArguments(args);
        return fsp;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        return pageTitles[position];
    }
}

使用此Fragment

public class FragmentStorePages extends Fragment implements ReplaceListener {
    String categoryName;
    int categoryId;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_store_pages,container,false);
        Bundle args = getArguments();
        if(args!=null && args.containsString("categoryName")&& args.containsString("categoryId")){
        categoryName = args.getString("categoryName");
        categoryId = args.getInt("categoryId");
        }           
        return view;
    }

    @Override
    public void onReplace(Bundle args) {

    }
}