我正在使用此仓库在Android应用中创建滑动标签:

https://github.com/ogaclejapan/SmartTabLayout

我使用了演示代码并将其减少到我的需要,现在我只想拥有一个Fragment类来处理所有应使用滑动选项卡的活动。因此,在我想使用滑动标签的特定应用程序中,有活动CardsWalkthrough

Cards的代码是这样的:

import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItem;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItemAdapter;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;

public class Cards extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);

        Demo demo = Demo.valueOf(String.valueOf(Demo.CARDS));

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle(demo.titleResId);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        ViewGroup tab = (ViewGroup) findViewById(R.id.tab);
        tab.addView(LayoutInflater.from(this).inflate(demo.layoutResId, tab, false));
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        SmartTabLayout viewPagerTab = (SmartTabLayout) findViewById(R.id.viewpagertab);
        demo.setup(viewPagerTab);

        FragmentPagerItems pages = new FragmentPagerItems(this);
        for (int titleResId : demo.tabs()) {
            pages.add(FragmentPagerItem.of(getString(titleResId), DemoFragment.class));
        }

        FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
                getSupportFragmentManager(), pages);

        viewPager.setAdapter(adapter);
        viewPagerTab.setViewPager(viewPager);
    }


    public enum Demo {
        CARDS(R.string.window_cards, R.layout.activity_slide);

        public final int titleResId;
        public final int layoutResId;

        Demo(int titleResId, int layoutResId) {
            this.titleResId = titleResId;
            this.layoutResId = layoutResId;
        }

        public static int[] tabTitle() {
            return new int[] {
                    R.string.title_cards1,
                    R.string.title_cards2,
                    R.string.title_cards3,
                    R.string.title_cards4,
                    R.string.title_cards5,
                    R.string.title_cards6
            };
        }
        public void setup(final SmartTabLayout layout) { }
        public int[] tabs() { return tabTitle(); }
    }
}


另一个类称为DemoFragment

import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItem;

public class DemoFragment extends Fragment {
    private static final String TAG = "Cards";

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_demo, container, false);

    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        int position = FragmentPagerItem.getPosition(getArguments());

        WebView browser = (WebView) view.findViewById(R.id.webView);
        browser.loadUrl("file:///android_asset/"+title+position+".html");
    }
}


现在,我只想以这种方式使用此DemoFragment类,它会根据我现在正在进行的活动来加载Webview。但是我不知道如何获取变量,例如本例中的Cards,只是使用int position = FragmentPagerItem.getPosition(getArguments());选项卡的当前位置。

最佳答案

我没有使用过这个库,但是看起来您可以使用Bundle的重载版本为Fragment提供自己的参数FragmentPagerItem.of()

for (int titleResId : demo.tabs()) {
    Bundle args = new Bundle();
    args.putString("page_source", ...);
    pages.add(FragmentPagerItem.of(getString(titleResId), DemoFragment.class, args));
}

WebView browser = (WebView) view.findViewById(R.id.webView);
browser.loadUrl(getArguments().getString("page_source"));

09-26 06:48