我想知道如何向TabLayout中的每个片段添加一些不同的数据,如数组? 我的意思是fragment1显示array1,fragment2显示array2,fragment3显示array3
以下是我的Tablayout代码:
public class PoshtibaniActivity extends AppCompatActivity {
private Toolbar toolbar;
private ViewPager mPager;
private SlidingTabLayout mTabs;
private TabLayout tabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_poshtibani);
toolbar = (Toolbar) findViewById(R.id.app_bar_empty);
//setSupportActionBar(toolbar);
toolbar.setTitle("پشتیبانی");
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mPager = (ViewPager) findViewById(R.id.pager);
setupViewPager(mPager);
mPager.setCurrentItem(2);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FragmentOne(), "پیامهای دریافتی");
adapter.addFragment(new FragmentTwo(), "پیامهای ارسالی");
adapter.addFragment(new FragmentThree(), "ارسال پیام");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
答案 0 :(得分:1)
你应该为每个片段使用SetArguement。
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FragmentOne(), "پیامهای دریافتی",MY_ARRAY_1,MY_SEC_ARRAY_1);
adapter.addFragment(new FragmentTwo(), "پیامهای ارسالی",MY_ARRAY_2,MY_SEC_ARRAY_2);
adapter.addFragment(new FragmentThree(), "ارسال پیام",MY_ARRAY_3,MY_SEC_ARRAY_3);
viewPager.setAdapter(adapter);
}
然后在您的适配器中:
public void addFragment(Fragment fragment, String title,Object data,ArrayList<MY_DATA_MODEL> array2) {
Bundle mBundle=new Bundle();
mBundle.put("dataArray",data);
mBundle.put("dataArray2",array2);
fragment.setArgument(mBundle);
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
现在,在每个片段中,您应该覆盖setArguments并从中获取数据。
如果您想以捆绑和意图传递数据模型,请不要忘记将数据模型设为可分割。
答案 1 :(得分:0)
查看寻呼机文件
public class SamplePagerAdapter extends FragmentPagerAdapter {
String[] title = {
"stringA",
"stringB",
"stringC",
}
public SamplePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
/** Show a Fragment based on the position of the current screen */
if (position == 0) {
return FragmentA.newInstance(title);
}
else if(position==1)
{
return new FragmentB.newInstance(title);
}
else { return FragmentC.newInstance(title);}
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
片段文件
public class FragmentA extends Fragment {
public static FragmentA newInstance(String [] s) {
FragmentA A = new FragmentA();
Bundle args = new Bundle();
args.putStringArrayList("elist",s);
A.setArgumentas(args);
return A;
}
}
public View onCreateView(LayoutInflater inflater,
ViewGroup containerObject,
Bundle savedInstanceState){
//here is your arguments
Bundle bundle=getArguments();
//here is your list array
String[] myStrings=bundle.getStringArray("elist");
}
public class FragmentB extends Fragment {
public static FragmentB newInstance(String [] s) {
FragmentB B = new FragmentB();
Bundle args = new Bundle();
args.putStringArrayList("elist",s);
B.setArgumentas(args);
return B;
}
}
public View onCreateView(LayoutInflater inflater,
ViewGroup containerObject,
Bundle savedInstanceState){
//here is your arguments
Bundle bundle=getArguments();
//here is your list array
String[] myStrings=bundle.getStringArray("elist");
}
public class FragmentC extends Fragment {
public static FragmentC newInstance(String [] s) {
FragmentC C = new FragmentC();
Bundle args = new Bundle();
args.putStringArrayList("elist",s);
C.setArgumentas(args);
return C;
}
}
public View onCreateView(LayoutInflater inflater,
ViewGroup containerObject,
Bundle savedInstanceState){
//here is your arguments
Bundle bundle=getArguments();
//here is your list array
String[] myStrings=bundle.getStringArray("elist");
}