动态创建的片段

时间:2018-02-22 07:13:05

标签: android android-fragments tabs android-viewpager

我有Activity个2个片段。 在我的ParentTabFragment中声明Activity,其中有TabLayoutViewPager

我的ParentTabFragment中有一种名为addPage的方法会在Activity中被调用。

addPage还有一个名为addFrag的方法,该方法是从ViewPagerAdapter调用views动态创建的ChildTabFragment

for loop中有一个Activity,因此根据Fragments中的项目数创建customList,创建tabs的数量1}}和Fragments中的页面。

在我的ChildTabFragment我在customList选择tab上加载了event, 当我在Tab中选择ParentTabFragment时,我会从getTabID()调用ChildTabFragment方法并发送Tab的ID,因为我加载了customList ParentTabFragment根据customList中的ID。

问题是当我滑动或选择标签时,会在Id中创建重复/错误数据。

我得到了所选标签的正确for (int j =0;j<it.size();j++){ parentTabFragment.addPage(it.get(j).getTabMenuItem(), it.get(j).getTabMenuId() , selectedMenu); } 但是当我尝试在方法中传递它时,会调用随机标识。

在我的活动类中:

public void addPage(String pagename, String pageId, String selectedMenu) {
        Bundle bundle = new Bundle();
        bundle.putString("data", pagename);
        //bundle.putString("pageID", pageId);
        childTabFragment = new ChildTabFragment();
        childTabFragment.setArguments(bundle);
        adapter.addFrag(childTabFragment, pagename, pageId);
        adapter.notifyDataSetChanged();

        if (selectedMenu == null) {
            viewPager.setCurrentItem(0);
        } else {
            for (int i = 0; i < tabLayout.getTabCount(); i++) {
                if (tabLayout.getTabAt(i).getText().equals(selectedMenu)) {
                    viewPager.setCurrentItem(i);
                }
            }
        }
        if (adapter.getCount() > 0) tabLayout.setupWithViewPager(viewPager);
        setupTabLayout();
    }

    public void setupTabLayout() {
        selectedTabPosition = viewPager.getCurrentItem();
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            tabLayout.getTabAt(i).setCustomView(adapter.getTabView(i));
        }
    }

ParentTabFragment:

public class ChildTabFragment extends Fragment {
    String childname;
    int menuItemId;
    TextView textViewChildName, txtViewItemId;
    ListView childTabMenuList;

    private ArrayList<ChildTabMenu_POJO> childTabMenuPojoList = new ArrayList<>();
    private ListView listView;
    private ChildTabMenuAdapter childTabMenuAdapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.child_tab_fragment, container, false);
        Bundle bundle = getArguments();
        childname = bundle.getString("data");
        getIDs(view); 
        return view;
    }

    private void getIDs(View view) { 
        childTabMenuList = (ListView) view.findViewById(R.id.childTabMenuList); 
        loadChildMenu();
    }

    private void loadChildMenu(menuItemId) { 

        ChildTabMenu_POJO menu_pojo4 = new ChildTabMenu_POJO(String.valueOf(menuItemId), "4");  
        childTabMenuPojoList.add(menu_pojo4);
        childTabMenuAdapter = new ChildTabMenuAdapter(childTabMenuPojoList, getActivity());
        childTabMenuList.setAdapter(childTabMenuAdapter);

    }

    public void getTabID(int tabId) {
        menuItemId = tabId; 
        Log.e("updatefrag", String.valueOf(menuItemId)); 
    }
}

ChildTabFragment:

public class ViewPagerAdapter extends FragmentStatePagerAdapter {
    private final ArrayList<Fragment> mFragmentList = new ArrayList<>();
    private final ArrayList<Adapter_POJO> mFragmentTitleList = new ArrayList<>();
    Context context;
    ViewPager viewPager;
    TabLayout tabLayout;
    Adapter_POJO adapter_pojo;

    public ViewPagerAdapter(FragmentManager manager, Context context, ViewPager viewPager,
                            TabLayout tabLayout) {
        super(manager);
        this.context = context;
        this.viewPager = viewPager;
        this.tabLayout = tabLayout;
    }

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

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


    public int getItemCount() {
        return mFragmentTitleList.size();
    }



    public void addFrag(Fragment fragment, String title, String pageId) {
        mFragmentList.add(fragment);
        adapter_pojo = new Adapter_POJO(title,pageId);
        mFragmentTitleList.add(adapter_pojo);
    }

    public View getTabView(final int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_pager_adapter_layout, null);
        TextView tabItemName = (TextView) view.findViewById(R.id.textViewTabItemName);
        tabItemName.setText(mFragmentTitleList.get(position).adapterMenuItem);
        tabItemName.setTextColor(context.getResources().getColor(android.R.color.background_light));

        return view;
    }
    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position).adapterMenuItem;
    }
    public String getPageId(int position){

        return mFragmentTitleList.get(position).adapterMenuId;
    }
}

ViewPagerAdapter:

curl -F 'client_id=<xxxxxxxxxxpIH0lKS-oDYy-FPVABjn>' \
> -F 'client_secret=<xxxxxxxxxxxxxpADzzeDLo2BYJ_pLS>' \
> -F 'grant_type=authorization_code' \
> -F 'redirect_uri=<http://localhost>' \
> -F 'code=<cxxxxxxxxxxxx8LpsNViR2ua0CP9g#_>' \
> https://login.uber.com/oauth/v2/token

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,那么你需要创建newInstance构造函数来创建带参数的片段

public static ChildTabFragment newInstance(int page, String title) {
        FirstFragment fragmentFirst = new FirstFragment();
        Bundle args = new Bundle();
        args.putInt("someInt", page);
        args.putString("someTitle", title);
        fragmentFirst.setArguments(args);
        return fragmentFirst;
    }

根据ChildTabFragment中传递的参数存储实例变量。

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        page = getArguments().getInt("someInt", 0);
        title = getArguments().getString("someTitle");
    }

现在更新onCreateView方法

中的视图
TextView tvLabel = (TextView) view.findViewById(R.id.tvLabel);
        tvLabel.setText(page + " -- " + title);

主要的是你应该从你的ChildTabFragment返回ViewPagerAdapter来显示该页面。

@Override
    public Fragment getItem(int position) {
        return ChildTabFragment.newInstance(position,"page "+String.valueOf(position));
    }