簡體   English   中英

片段內的ViewPager而不使用嵌套片段

[英]ViewPager inside Fragment without using nested fragment

我設法實現這個答案 對於viewpager僅使用單個XML,但是我想要的是在片段內而不是Activity內實現此功能。 我嘗試在Fragment中實現它,但是viewpager什么也沒顯示。

這是我的PagerAdapter:

class WizardPagerAdapter extends PagerAdapter {

    public Object instantiateItem(ViewGroup collection, int position) {
        Log.i("instantiateItem", "instantiateItem");
        int resId = 0;
        switch (position) {
            case 0:
                resId = R.id.llPageOne;
                break;
            case 1:
                resId = R.id.llPageTwo;
                break;
        }
        return collection.findViewById(resId); // I tried replacing this with pager.findViewById(resId)
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup parent, int position, Object object) {
        View view = (View) object;
        parent.removeView(view);
    }
} 

這就是我的實現方式:

    WizardPagerAdapter adapter = new WizardPagerAdapter();
    pager = (ViewPager) view.findViewById(R.id.pager);
    pager.setAdapter(adapter);

這是我的XML布局:

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <LinearLayout
        android:id="@+id/llPageOne"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="PAGE ONE IN" />

        <EditText
            android:id="@+id/etSample"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <EditText
            android:id="@+id/etSample2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/llPageTwo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="PAGE TWO IN" />

        <EditText
            android:id="@+id/etTwo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</android.support.v4.view.ViewPager>

因此,基本上我想要的是為Fragment內部的ViewPager的所有視圖實現一個XML文件。 自從我學習為android編寫代碼以來只有幾個月,所以我很難讓它工作。 提前致謝。

注意:如果可能,不要使用嵌套片段

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM