0

ナビゲーション ドロワー (フラグメントを使用) を備えた webview で構成されたアプリケーションを開発しています。空白のページしかないときに、サイトのホームページでアプリケーションを開始したいのですが、可能ですか?よろしくお願いします。 xml:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<!-- The main content view -->

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<!-- The navigation drawer -->

<ListView
    android:id="@+id/drawer_list"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#F3F3F4"
    android:choiceMode="singleChoice"
    android:divider="#d9d9d9"
    android:dividerHeight="1dp" />

フラグメントの layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

メイン Java:

public class MainActivity extends Activity {


private DrawerLayout mDrawerLayout;

private ListView mDrawerList;

private ActionBarDrawerToggle mDrawerToggle;

private String mTitle = "";

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTitle = "";
    getActionBar().setTitle(mTitle);


    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    mDrawerList = (ListView) findViewById(R.id.drawer_list);


    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, R.string.drawer_open,
            R.string.drawer_close) {

        /** Called when drawer is closed */
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu();

        }

        /** Called when a drawer is opened */
        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle("");
            invalidateOptionsMenu();
        }

    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);


    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), 
            R.layout.drawer_list_item, getResources().getStringArray(R.array.menus));


    mDrawerList.setAdapter(adapter);


    getActionBar().setHomeButtonEnabled(true);


    getActionBar().setDisplayHomeAsUpEnabled(true);


    mDrawerList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {


            String[] menuItems = getResources().getStringArray(R.array.menus);


            mTitle = menuItems[position];


            WebViewFragment rFragment = new WebViewFragment();


            Bundle data = new Bundle();
            data.putInt("position", position);
            data.putString("url", getUrl(position));
            rFragment.setArguments(data);



            FragmentManager fragmentManager = getFragmentManager();


            FragmentTransaction ft = fragmentManager.beginTransaction();


            ft.replace(R.id.content_frame, rFragment);


            ft.commit();


            mDrawerLayout.closeDrawer(mDrawerList);

        }
    });
}

protected String getUrl(int position) {
    switch (position) {
    case 0:
        return "http://";
    case 1:
        return "http://";
    case 2:
        return "http://";
    case 3:
        return "http://";
    case 4:
        return "http://";
    case 5:
        return "http://";
    case 6:
        return "http://";
    default:
        return "http://";
    }
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}


@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // If the drawer is open, hide action items related to the content view
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);

    menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

4

1 に答える 1

0

マニフェストにインターネット アクセス許可を設定しましたか?

 <uses-permission android:name="android.permission.INTERNET" />

編集

初期バージョンをセットアップしようとしている場合は、次のことだけを行う必要はありません。

String[] menuItems = getResources().getStringArray(R.array.menus);

        mTitle = menuItems[0];

        WebViewFragment rFragment = new WebViewFragment();

        Bundle data = new Bundle();
        data.putInt("position", position);
        data.putString("url", getUrl(position));
        rFragment.setArguments(data);

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.replace(R.id.content_frame, rFragment);
        ft.commit();
于 2013-12-02T11:03:15.663 に答える