滑动视图和片段android

时间:2013-08-12 20:30:05

标签: android facebook android-activity fragment swipe

我目前有以下代码,我作为示例找到并稍作修改:

package com.boy.test;

import java.util.Locale;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
     * will keep every loaded fragment in memory. If this becomes too memory
     * intensive, it may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;

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

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a DummySectionFragment (defined as a static inner class
            // below) with the page number as its lone argument.
            Fragment fragment = new Facebook();
            Bundle args = new Bundle();
            args.putInt(Facebook.ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
                case 0:
                    return "Welcome".toUpperCase(l);
                case 1:
                    return "Facebook".toUpperCase(l);
                case 2:
                    return "Twitter".toUpperCase(l);
                case 3:
                    return "dashboard".toUpperCase(l);
            }
            return null;
        }
    }

    /**
     * A dummy fragment representing a section of the app, but that simply
     * displays dummy text.
     */
    public static class Facebook extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        public static final String ARG_SECTION_NUMBER = "section_number";
        private WebView faceBook;


        public Facebook() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.activity_facebook, container, false);


            return rootView;
        }
    }

}

我想要做的是将facebook添加到第二个标签中。这是我在另一个类中执行的代码:

public class Facebook extends Activity {
    private WebView faceBook;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_facebook);
        faceBook = new WebView(this);
        faceBook.getSettings().setJavaScriptEnabled(true);
        final Activity act = this;
        faceBook.setWebViewClient(new WebViewClient(){
            public void onRecievedError(WebView view,int errorCode,String description,String failingUrl){
                Toast.makeText(act,description,Toast.LENGTH_SHORT).show();
            }
        });

        faceBook.loadUrl("http://m.facebook.com/pages/hi");
        setContentView(faceBook);
    }

我尝试将其复制到我的静态facebook片段中并出错。我也尝试过扩展片段活动而不是片段,并获得更多错误。谁能告诉我如何最好地接近这个?

谢谢。

1 个答案:

答案 0 :(得分:0)

首先,您不需要Facebook活动,您必须在Facebook类中覆盖Fragment的{​​{1}}方法才能返回onCreateView()

试试这个,

你的片段应如下所示,

WebView

,您的适配器将

public final class MyTab extends Fragment {

    public static MyTab newInstance(int type) {
        MyTab tab = new MyTab();
        tab.mType = type;
        return fragment;
    }

    private int mType = -1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        /*
         * Create a web view here, and on the basis of mType decide what to load.
         * and return that web view. 
         */
    } 
}