2014-06-21 50 views
1

所以,我對Android開發相當陌生,於是決定 - 在開始一些擺弄之後 - 讓我的第一個完整的應用程序帶有導航抽屜的應用程序。理想情況下,我想要一個WebView,如果您在導航抽屜中選擇一個項目,URL將會更改。例如:導航抽屜由以下列表組成:Google,Stackoverflow,Facebook和Twitter。如果您點擊其中的一個,則正確的網址會被推送到網絡視圖。如何通過單擊按鈕來更改webview的url?

我開始使用Google的代碼。具有導航抽屜的示例應用程序,帶有行星圖像。我改變了一些東西,但是我目前堅持的步驟是點擊一個項目和webview改變的那一步。下面你可以看到一些代碼片段:

fragment_planet.xml

<WebView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/image" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#000000" 
    android:gravity="center" 
    android:padding="32dp" /> 

MainActivity

private void selectItem(int position) { 
    // update the main content by replacing fragments 
    Fragment fragment = new PlanetFragment(); 
    Bundle args = new Bundle(); 
    args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position); 
    fragment.setArguments(args); 

    FragmentManager fragmentManager = getFragmentManager(); 
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); 

    // update selected item and title, then close the drawer 
    mDrawerList.setItemChecked(position, true); 
    setTitle(mPlanetTitles[position]); 
    mDrawerLayout.closeDrawer(mDrawerList); 
} 

我希望有人能幫助我走出這一點,因爲我可以」找到任何文件。

回答

0

Webview類有loadUrl方法。它可以隨時調用。只需在你的方法PlanetFragment中爲其調用loadUrl方法即可。

相關問題