ADT のデフォルトの「固定タブ + スワイプ」アクティビティに基づいてFragmentActivity
、FragmentPagerAdapter
.
これFragementPagerAdapter
は非常に単純で (呼び出された位置に基づいて特定のフラグメントを返すだけです)、現時点では、FragmentActivity
それ自体も Eclipse が提供する「サンプル」コードからかなりデフォルトになっています。「面白い」部分は最後に付けますが、こんな感じです(もちろん質問用に簡略化しています)
現在、さまざまなフラグメントの 1 つに、いくつかの情報と画像を表示するボタンがあります。タブ ビューには実際には属さないものです。
これに最適な方法は何ですか?
新しいアクティビティを開始するかどうかを考えましたが、フラグメントを使用して構築することをお勧めしないため、そのオプションを破棄しました。次に、表示する「ランダムな」フラグメントを作成できますが、ページャーに関してそれを実装する方法がわかりません->これが実際には最良の方法かもしれません。
私が思いついたのは、DialogFragment
. 私は実際にこれを次のように実装しました:
最初にこれで DialogFragment を作成しますonCreateView
:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LinearLayout v = (LinearLayout) inflater.inflate(R.layout.entry, container, false);
//set image
return v;
}
次のように表示します。
fragment.show(getSupportFragmentManager(), "dialog");
xml は、stackoverflow で提案された単純な linearLayout です。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="1000dp"
android:minHeight="1000dp">
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/image_desc"
/>
</LinearLayout>
ここでの問題は、これは問題なく表示されていますが、画像の上に何か (ページャーのタイトル バーかどうかはわかりません) が追加されているように見えることです。
見にくいですが、pager-fragment-titles の一番下に直接並んでいると思いますので、いくつかの機能を「継承」しているように見えますか?
現在のベースコード
の onCreate からFragmentActivity
setContentView(R.layout.activity_main);
...
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
は、いくつかのフラグメントを返すSectionsPagerAdapter
内部クラスであり、興味深いものは何もありませんFragmentPagerAdapter
、activity_main
再びかなり標準:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<!--
This title strip will display the currently visible page title, as well as the page
titles for adjacent pages.
-->
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#555555"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>