4

ActionBarタブとViewPagerを使用して、タブ間のスワイプを実装したいと考えていました。私はここの例をうまく使用しました:

http://developer.android.com/reference/android/support/v4/view/ViewPager.html

ただし、xmlのViewPagerの外部にフレームレイアウトを追加することも必要でした。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/FrameLayout1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </android.support.v4.view.ViewPager>   

    <FrameLayout
        android:id="@+id/frame_layout"
        android:name="com.banagas.calculator.ButtonFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

ViewPagerとFrameLayoutの両方を使用してsetContentView()をレイアウトに設定すると、ViewPagerの機能が停止します。

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("MainActivity", "onCreate");  
        mViewPager = new ViewPager(this);
        mViewPager.setId(R.id.FrameLayout1);
        ActionBar bar = getSupportActionBar();
        mViewPager.setOffscreenPageLimit(4);

        bar.setDisplayHomeAsUpEnabled(false);
        bar.setDisplayShowTitleEnabled(false);
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        mTabsAdapter = new TabsAdapter(this, mViewPager);

        mTabsAdapter.addTab(bar.newTab().setText("Display"), DisplayFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("Y="), YFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("Graph"), GraphFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("Table"), TableFragment.class, null);

        if (findViewById(R.id.frame_layout) != null) 
        {
            if (savedInstanceState != null) 
            {
                return;
            }
            getSupportFragmentManager().beginTransaction().add(R.id.frame_layout, new ButtonFragment()).commit();
            Log.i("MainActivity", "add.ButtonFragment");
        }
    }

ViewPager内にはタブだけが表示されます。私のフレームレイアウトは完全にうまく機能します。ViewPagerをこれで動作させるにはどうすればよいですか?

4

1 に答える 1

1

Why you do mViewPager = new ViewPager(this); mViewPager.setId(R.id.FrameLayout1); ?

The ViewPager is already on your layout.

Shoud do something like mViewPager = (ViewPager)findViewById(R.id.FrameLayout1);

From the sample code they are creating the controls on runtime, while you have them in the xml.

于 2013-02-12T01:25:29.877 に答える