在片段MainActivity中打开WebView

时间:2015-07-02 17:25:51

标签: java android android-fragments android-activity webview

我拥有的东西:导航抽屉,我发布新闻的片段,屏幕底部的两个按钮。

我想要的是什么:点击两个按钮之一,打开片段内的网页浏览。

我的档案:

MainActivity.java:

 import android.content.ActivityNotFoundException;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBar;
    import android.support.v7.app.ActionBarActivity;
    import android.view.View;
    import android.webkit.WebView;
    import android.widget.Button;

    import com.jmsliu.rssreader.model.PostDataModel;
    import com.jmsliu.rssreader.model.vo.DrawerData;
    import com.jmsliu.rssreader.model.vo.PostData;

    public class MainActivity extends ActionBarActivity
            implements NavigationDrawerFragment.NavigationDrawerCallbacks,
            PostListFragment.PostListFragmentInteractionListener{

        /**
         * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
         */
        private NavigationDrawerFragment mNavigationDrawerFragment;

        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            getSupportActionBar().setCustomView(R.layout.abs_layout);

            Button youTubeButton = (Button) findViewById(R.id.button2);
            youTubeButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            });
            mNavigationDrawerFragment = (NavigationDrawerFragment)
                    getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);

            // Set up the drawer.
            mNavigationDrawerFragment.setUp(
                    R.id.navigation_drawer,
                    (DrawerLayout) findViewById(R.id.drawer_layout));
        }  

        @Override
        public void onNavigationDrawerItemSelected(int position) {
            //PostListFragment postListFragment = (PostListFragment) getSupportFragmentManager().findFragmentById(R.id.postListView);
            PostListFragment postListFragment = (PostListFragment) getSupportFragmentManager().findFragmentByTag("postlist_fragment");

            DrawerData data = GlobalClass.instance().categoryList.get(position);
            if(data.name != "") {
                String url = String.format(GlobalClass.CATEGORY_RSSURL, data.name);
                postListFragment.rssURL = String.format(GlobalClass.CATEGORY_RSSURL, data.name);
                //Toast.makeText(this, url, Toast.LENGTH_SHORT).show();

            } else {
                postListFragment.rssURL = GlobalClass.RSSURL;
                //Toast.makeText(this, GlobalClass.instance().RSSURL, Toast.LENGTH_SHORT).show();
            }

            PostDataModel.getInstance().listData.clear();
            PostDataModel.getInstance().guidList.clear();
            postListFragment.manualRefresh();   
        }

        @Override
        public void onPostSelected(int index) {
            PostData data = PostDataModel.getInstance().listData.get(index);
            FragmentManager fragmentManager = getSupportFragmentManager();
            PostViewFragment postViewFragment = (PostViewFragment) getSupportFragmentManager().findFragmentByTag("postview_fragment");
            if(postViewFragment == null) {
                postViewFragment = PostViewFragment.newInstance(data.postLink);
            } else {
                postViewFragment.urlLink = data.postLink;
            }

            postViewFragment.title = data.postTitle;
            FragmentTransaction ft = fragmentManager.beginTransaction();
            ft.replace(R.id.container, postViewFragment, "postview_fragment");
            ft.addToBackStack(null);
            ft.commit();
        }
    }

activity_main.xml中:          

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout android:id="@+id/container" android:layout_width="match_parent"
        android:layout_height="match_parent">
        <fragment
            android:id="@+id/postlist_fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="postlist_fragment"
            android:name="com.jmsliu.rssreader.PostListFragment"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="SEGNALA"
            android:id="@+id/segnala"
            android:background="#000000"
            android:textColor="#ffffff"
            android:clickable="true"
            android:layout_gravity="center|bottom"
            android:layout_alignParentTop="false"
            android:layout_alignWithParentIfMissing="false"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentStart="false"
            android:layout_alignParentEnd="false"
            android:layout_alignParentBottom="true" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button2"
            android:text="YouTube"
            android:background="#000000"
            android:textColor="#ffffff"
            android:clickable="true"
            android:layout_gravity="right|bottom" />

    </FrameLayout>

    <!-- android:layout_gravity="start" tells DrawerLayout to treat
         this as a sliding drawer on the left side for left-to-right
         languages and on the right side for right-to-left languages.
         If you're not building against API 17 or higher, use
         android:layout_gravity="left" instead. -->
    <!-- The drawer is given a fixed width in dp and extends the full height of the container. -->
    <fragment android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
        android:layout_gravity="start" android:name="com.jmsliu.rssreader.NavigationDrawerFragment"
        tools:layout="@layout/fragment_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>

1 个答案:

答案 0 :(得分:0)

看起来很简单。只需使用webview创建新片段,并在点击监听器上的按钮内部替换片段,如此

    button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                         Fragment fragment = WebViewFragment.newInstance(string dataOrAnythingYouWillUseToPassContent)
getSupportFragmentManager().beginTransaction().replace(android.R.id.content, R.id.fragment_container,fragment).addToBackStack(stringTag).commit();
                    }
                });

在webviewfragment中,static newInstance方法将数据打包成片段参数,并在onCreate方法中或在需要时检索它。