具有TABBED活动的Android Fragment Data Pass

时间:2018-04-04 08:48:16

标签: android

当我尝试从服务器检索多个选项卡的类别列表时,首次将适配器设置为Toast两个ID。假设,当前选项卡ID为ZERO,则显示下一个增量ID。enter image description here < / p>

就像屏幕截图一样,它在标签0中显示20。

基本上,当我滑动标签时,我会动态地想要类别ID。

public class Tabtest extends AppCompatActivity {

    TabLayout tabLayout;
    ViewPager viewPager;
    Toolbar toolbar;
    int i;
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tabtest);

        toolbar=(Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        tabLayout=(TabLayout)findViewById(R.id.tabs);
        viewPager=(ViewPager)findViewById(R.id.container);

        getCategoriesList();


        tabLayout.setupWithViewPager(viewPager);

    }

    public void getCategoriesList(){
        Retrofit retrofit = newsClient.getClient();
        apiNews service = retrofit.create(apiNews.class);
        Call<List<Category>> call = service.categorriesList();
        call.enqueue(new Callback<List<Category>>() {

            @Override
            public void onResponse(Call<List<Category>> call, Response<List<Category>> response) {
                if (response.isSuccessful()){
                    for (int i=0;i<response.body().size();i++){
                            MenuBurgersFragment pageFragment=  new MenuBurgersFragment();
                            Bundle bundle = new Bundle();
                            adapter.addFragment(pageFragment, response.body().get(i).getId()+"");
                            bundle.putString("welcome", response.body().get(i).getId()+"" );
                            viewPager.setAdapter(adapter);
                            pageFragment.setArguments(bundle);
                            adapter.notifyDataSetChanged();
                    }
                }
            }
            @Override
            public void onFailure(Call<List<Category>> call, Throwable t) {
                Toast.makeText(Tabtest.this, "safsavas", Toast.LENGTH_SHORT).show();
            }
        });
    }

}

ViewPagerAdapter

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

片段

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

        View rootView = inflater.inflate(R.layout.fragment_menu_list_items, container, false);

        bundle = this.getArguments();
        if(bundle!=null) {
            storeText = bundle.getString("welcome");
            Toast.makeText(getContext().getApplicationContext(), storeText, Toast.LENGTH_SHORT).show();
        }

1 个答案:

答案 0 :(得分:0)

在View Pager Bydefault中加载了上一个和下一个片段数据,这就是为什么你在吐司中获得下一个片段值的原因。

因为你必须在viewpager中设置OffscreenPageLimit以限制在View Pager中加载上一个和下一个片段 setOffscreenPageLimit()的set值为1。

希望以上内容能为您提供帮助。