Android:如何获取片段ID(0,1,2 ...)

时间:2015-06-03 04:39:00

标签: java android android-studio

好吧,我正在使用带有Web视图的导航抽屉,每个不同的片段加载不同的URL,所以我做了以下内容:

Array.xml:

    <?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="nav_options">
        <item>Woffer</item>
        <item>Quiero Ofertas!</item>
        <item>Inscribir mi local</item>
        <item>Consultas</item>
    </string-array>

    <string-array name="webs">
        <item>http://woffer.com.ar</item>
        <item>http://woffer.com.ar</item>
        <item>http://woffer.com.ar/inscribir</item>
        <item>http://woffer.com.ar/contacto</item>
    </string-array>

</resources>

Fragment java:

    public class MyFragment extends Fragment {

    public final static String KEY_TEXT = "key_text";

    private String mText;
    private CharSequence mTitle;

    WebView mWebView;
    private ViewPager ViewPager;
    View rootview;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        rootview = inflater.inflate(R.layout.fragment,container,false);
        ViewPager = (ViewPager) rootview.findViewById(R.id.pager);
        mWebView = (WebView) rootview.findViewById(R.id.webview);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        String[] yourstring = getResources().getStringArray(R.array.webs);
        mWebView.loadUrl(yourstring[ViewPager.getCurrentItem()]); //I don't know how to get the "index" of the current fragment for the url array
        mWebView.setWebViewClient(new WebViewClient());

        return rootview;
    }

    private class Callback extends WebViewClient {  
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return (false);
        }
    }

}

所以基本上我任何人都可以帮助我放在url数组上,以便它匹配当前片段

谢谢!

0 个答案:

没有答案