无法在 viewpager 上获取选定选项卡的位置

Posted

技术标签:

【中文标题】无法在 viewpager 上获取选定选项卡的位置【英文标题】:Unable to get position of selected tab on viewpager 【发布时间】:2017-04-02 01:00:00 【问题描述】:

将创建多少个选项卡取决于网络服务。这意味着在调用 Web 服务之前,我无法发现要创建多少个选项卡。 选项卡包含我想在网格视图中显示的产品。

在我的项目中,我有 ShopProductsPageFragments.java,其中创建了选项卡。请看下面的代码:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    CatPosition = getArguments().getInt("CatPosition");
    StoreID = getArguments().getString("StoreID");
    System.out.println("getStoreID in ShopProductsPageFragments="+ StoreID);
    System.out.println("getCatPosition in ShopProductsPageFragments="+ CatPosition);

    try 
        ShopCategoryData = (GetProductCategoriesByStoreResponsePojo) getArguments().getSerializable("ShopCatNames");
    catch (Exception e)
        e.printStackTrace();
    
    assert ShopCategoryData != null;
    List<Datum> shopcatdata = ShopCategoryData.getData();
    for (int i = 0; i < shopcatdata.size(); i++) 
        System.out.println("ShopCategoryData in ShopProductsPageFragments "+ shopcatdata.get(i).getCatName());
    
    ShopProductsPageView = inflater.inflate(R.layout.activity_product_page_fragment ,container ,false);
    viewPager = (ViewPager)ShopProductsPageView.findViewById(R.id.product_viewpager);
    setupViewPager(viewPager);
    tabLayout = (TabLayout)ShopProductsPageView.findViewById(R.id.product_tabs);
    tabLayout.setupWithViewPager(viewPager);
    return ShopProductsPageView;


private void setupViewPager(ViewPager viewPager) 
    ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
    List<Datum> shopcatdata = ShopCategoryData.getData();
    for (int i = 0; i < shopcatdata.size(); i++) 
        CommanShopProductFragment commanShopProductFragment = CommanShopProductFragment.newInstance(i);
        String CatName = shopcatdata.get(i).getCatName();
        Bundle bundle = new Bundle();
        bundle.putString("StoreID",StoreID);
        bundle.putString("CatName",CatName);
        commanShopProductFragment.setArguments(bundle);
        System.out.println("ShopCategoryData in ShopProductsPageFragments "+ shopcatdata.get(i).getCatName());
        adapter.addFrag(commanShopProductFragment, shopcatdata.get(i).getCatName());
    
    adapter.notifyDataSetChanged();
    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(CatPosition);


class ViewPagerAdapter extends FragmentPagerAdapter 
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();
    FragmentManager fragmentManager;
    public ViewPagerAdapter(FragmentManager manager) 
        super(manager);
        fragmentManager = manager;
    

    @Override
    public Fragment getItem(int position) 
        return mFragmentList.get(position);
    

    @Override
    public int getCount() 
        return mFragmentList.size();
    

    void addFrag(Fragment fragment, String title) 
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    

    @Override
    public CharSequence getPageTitle(int position) 
        return mFragmentTitleList.get(position);
    


在这里,您可以看到标签是如何创建的。我正在使用相同的片段在选项卡中显示数据,如下所示:

    public class CommanShopProductFragment extends Fragment 
private static final String ARG_POSITION = "position";
private int position;
View CategoryTabFragmentView;
GetStoreProductsByCategoriesPresenterImpl presenter;
RestClient service;
GridView gridView;
List<Datum> shopProduct;
ProductByCategoryGridViewAdapter mAdapter;
public CommanShopProductFragment() 
    // Required empty public constructor

public static CommanShopProductFragment newInstance(int position) 
    CommanShopProductFragment f = new CommanShopProductFragment();
    Bundle b = new Bundle();
    b.putInt(ARG_POSITION, position);
    f.setArguments(b);
    return f;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);

    String CatName = getArguments().getString("CatName");
    String StoreID = getArguments().getString("StoreID");
    assert CatName != null;
    System.out.println("CommanShopProductFragment >>>>>>>> CatName="+CatName);
    assert StoreID != null;
    System.out.println("CommanShopProductFragment >>>>>>>> StoreID="+StoreID);
    CategoryTabFragmentView = inflater.inflate(R.layout.activity_category_tab_fragment ,container ,false);
    service = ((LilubiApplication) getActivity().getApplication()).getNetworkService();
    presenter = new GetStoreProductsByCategoriesPresenterImpl(this, service);
    String page = "1", itemsPerPage = "10";
    try 
        presenter.GetStoreProductsByCategories(CatName, StoreID, page, itemsPerPage);
    catch (Exception e)
        e.printStackTrace();
    
    return CategoryTabFragmentView;

public void getStoreProductsByCategories(ProductByCategoriesResponsePojo productByCategoriesResponsePojo)
    System.out.println("CategoryTabFragment in  getMessage="+productByCategoriesResponsePojo.getMessage());
    System.out.println("CategoryTabFragment in  getStatus="+productByCategoriesResponsePojo.getStatus());
    // prepared arraylist and passed it to the Adapter class
    shopProduct = productByCategoriesResponsePojo.getData();
    mAdapter = new ProductByCategoryGridViewAdapter(getActivity(),shopProduct);
    // Set custom adapter to gridview
    gridView = (GridView) CategoryTabFragmentView.findViewById(R.id.category_tab_view_grid_view);
    gridView.setAdapter(mAdapter);

现在我想要的是当用户选择一个选项卡时,应根据选项卡中选择的类别显示产品列表。 所有产品数据也来自网络服务。如果我错过了任何需要解释的事情,请告诉我。谢谢。

【问题讨论】:

【参考方案1】:

我正在编辑我之前的答案

编辑: 您可以使用viewpager.getCurrentItem() 获取当前位置 //将其包含在您的活动中与 viewpager

【讨论】:

你能分享一些例子吗。我尝试了上面的代码,但没有任何改变。 我也尝试过 viewpager.getCurrentItem() 但没有工作,实际上我对多个选项卡使用相同的片段来显示不同的数据。并且该片段根据选项卡类别调用 Web 服务。但是因为查看寻呼机它一次给我两个位置以便平滑滚动。有没有其他方法可以解决这个问题..

以上是关于无法在 viewpager 上获取选定选项卡的位置的主要内容,如果未能解决你的问题,请参考以下文章

在不更改选定选项卡的情况下动态更新 TabLayout

如何在 Kendo UI TabStrip 中获取对当前选定选项卡的引用?

jQuery UI 选项卡 - 如何获取当前选定的选项卡索引

在 Viewpager 上添加带有标记的 Google 地图

listview 没有在带有 viewpager 选项卡的片段中刷新

popBack 到选定选项卡的第一个视图控制器