在动态片段中滑动标签

时间:2018-11-25 21:55:08

标签: android android-fragments dynamic swipe

我想在动态生成的片段中实现滑动选项卡(它没有布局文件),我也不知道如何。我已经尝试了一些方法,并获得了一些“结果”,这意味着我可以看到选项卡的布局,但它不会“加载”片段。我怀疑问题出在声明的视图分页器上。以下是该片段的代码:

public class fg_tabs extends Fragment {

LinearLayout root;

TabLayout tab_layout;
ViewPager view_pager;
PagerAdapter pager_adaper;

public fg_tabs() {
    // Required empty public constructor
}


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

    root = new LinearLayout(getActivity());
    root.setOrientation(LinearLayout.VERTICAL);

    tab_layout = new TabLayout(getActivity());
    view_pager = new ViewPager(getActivity());
    view_pager.setCurrentItem(2);

    tab_layout.addTab(tab_layout.newTab().setText("Penguins"));
    tab_layout.addTab(tab_layout.newTab().setText("North Wind"));
    tab_layout.addTab(tab_layout.newTab().setText("Dr Octavious Brine"));


    tab_layout.setTabGravity(Gravity.FILL);
    pager_adaper = new PagerAdapter(getFragmentManager(),tab_layout.getTabCount());

    view_pager.setAdapter(pager_adaper);
    view_pager.setOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab_layout));

    tab_layout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            view_pager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

    root.addView(tab_layout);
    return root;
}

}

您可能想看看我如何称呼这个片段,看看那里是否有问题:

    fg_tabs tabs = new fg_tabs();
    FragmentManager fm= getSupportFragmentManager();
    fm.beginTransaction().replace(R.id.layout_frame,tabs).commit();

寻呼机适配器类:

公共类PagerAdapter扩展FragmentStatePagerAdapter {     int n;

public PagerAdapter(FragmentManager fm,int n) {
    super(fm);
    this.n=n;
}


@Override
public Fragment getItem(int position) {
    if(position == 0)
        return new Penguins();

    if(position == 1)
        return new NorthWind();

    if(position == 2)
        return new DrOctaviousBrine();

    return null;
}

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

}

2 个答案:

答案 0 :(得分:0)

我认为您忘记将ViewPager添加到onCreateView的视图中:

ViewGroup.LayoutParams params = LinearLayout.LayoutParams(
    ViewGroup.LayoutParams.MATCH_PARENT,
    ViewGroup.LayoutParams.MATCH_PARENT)
root.addView(view_pager, params);
return root;

答案 1 :(得分:0)

我找到了问题的答案,问题是没有为viewpager设置参数,没有给他id,也没有将其添加到布局中。对于那些正在为同一问题而苦恼的人,只需采用常规的滑动选项卡代码(https://www.youtube.com/watch?v=zcnT-3F-9JA),并记住您正在使用动态片段,就不能声明xml。

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class fg_tabs extends Fragment {

    LinearLayout root;
    TabLayout tab_layout;
    ViewPager view_pager;
    PagerAdapter pager_adapter;

    public fg_tabs() {
        // Required empty public constructor
    }


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

        root = new LinearLayout(getActivity());

        root.setOrientation(LinearLayout.VERTICAL);
        root.setBackgroundColor(Color.parseColor("#42f4e8"));

        tab_layout = new TabLayout(getActivity());

        tab_layout.addTab(tab_layout.newTab().setText("penguins"));
        tab_layout.addTab(tab_layout.newTab().setText("north wind"));
        tab_layout.addTab(tab_layout.newTab().setText("octavious brine"));
        root.addView(tab_layout);

        tab_layout.setTabGravity(Gravity.FILL);

        view_pager = new ViewPager(getActivity());
        view_pager.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.FILL_PARENT));
        view_pager.setId(View.generateViewId());
        root.addView(view_pager);

        pager_adapter = new PagerAdapter(getChildFragmentManager(),tab_layout.getTabCount());

        view_pager.setAdapter(pager_adapter);
        view_pager.setOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab_layout));

        tab_layout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                view_pager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        return root;
    }
}