DialogFragment中的ViewPager

时间:2016-02-11 17:42:46

标签: android android-fragments android-viewpager android-tablayout

所以我知道这已被多次回答,例如Fragment Inside Fragment。我正在使用帖子中建议的getChildFragmentManager()支持库。问题在于,尽管被夸大了,我的viewpager片段仍然没有出现。我的标签(带图标)显示完美。以下是相关代码:

DialogFragment:

public class DialogFragment extends DialogFragment {

    private static final String ARG_PAGE = "ARG_PAGE";

    private int mPage;
    private HotSpot hotSpot;
    private Gson gson = new Gson();
    private Map nonEmptyPerks;

    private TabLayout tabLayout;
    private ViewPager viewPager;

    public DialogFragment() {
        // Empty constructor required for DialogFragment
    }

    public static PerkDetailDialogFragment newInstance(int page, String hotSpotJson) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        args.putString("HOTSPOTDETAILED", hotSpotJson);
        PerkDetailDialogFragment fragment = new PerkDetailDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPage = getArguments().getInt(ARG_PAGE);
        String json = getArguments().getString("HOTSPOTDETAILED");
        hotSpot = gson.fromJson(json, HotSpot.class);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dialog, parent, false);

        // Get the ViewPager and set it's PagerAdapter so that it can display items
        viewPager = (ViewPager) view.findViewById(R.id.viewpager);
        tabLayout = (TabLayout) view.findViewById(R.id.perkSelector);

        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        nonEmptyPerks = getNonEmptyPerks(hotSpot.getPerks());

        PerkFragmentPagerAdapter pagerAdapter =
                new PerkFragmentPagerAdapter(getChildFragmentManager(), nonEmptyPerks.size());
        viewPager.setAdapter(pagerAdapter);
        // Give the TabLayout the ViewPager
        tabLayout.setupWithViewPager(viewPager);

        // ..... More code here
    }

    @Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().setLayout(
                RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    }

    public class PerkFragmentPagerAdapter extends FragmentPagerAdapter {
        private int pageCount;


        public PerkFragmentPagerAdapter(FragmentManager fm, int pageCount) {
            super(fm);
            this.pageCount = pageCount;
        }

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

        @Override
        public Fragment getItem(int position) {
            String perk = "temp";

            return PerkFragment.newInstance(position + 1, perk);
        }
    }
}

PerkFragment:

public class PerkFragment extends Fragment {

    private static final String ARG_PAGE = "ARG_PAGE";
    private static final String ARG_PERK_DESCRIPTION = "PERK_DESCRIPTION";

    private int mPage;
    private String perkDescription;

    public static PerkFragment newInstance(int page, String perk) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        args.putString(ARG_PERK_DESCRIPTION, perk);
        PerkFragment fragment = new PerkFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPage = getArguments().getInt(ARG_PAGE);
        perkDescription = getArguments().getString(ARG_PERK_DESCRIPTION);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_perk, container, false);
        TextView perkDescriptionTextView = (TextView) view.findViewById(R.id.perkDescription);
        perkDescriptionTextView.setText(perkDescription);
        return view;
    }
}

fragment_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/hotSpotImg"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:scaleType="centerCrop"
        android:src="@drawable/img_venue_placeholder" />

    <ImageView
        android:id="@+id/iconBackArrow"
        android:layout_width="50dp"
        android:layout_height="60dp"
        android:layout_alignStart="@+id/hotSpotImg"
        android:layout_alignTop="@+id/hotSpotImg"
        android:src="@drawable/img_back_arrow_shadow" />

    <ImageView
        android:id="@+id/iconGPS"
        android:layout_width="50dp"
        android:layout_height="60dp"
        android:layout_alignBottom="@+id/hotSpotImg"
        android:layout_alignStart="@+id/hotSpotImg"
        android:src="@drawable/img_gps" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/hotSpotImg">

        <TextView
            android:id="@+id/memberSpace"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"
            android:text="@string/member_space"
            android:textAllCaps="true"
            android:textColor="@color/grey_2"
            android:textSize="16sp" />

        <com.test.test.helpers.LetterSpacingTextView
            android:id="@+id/hotSpotName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/memberSpace"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="10dp"
            android:text="That filler info"
            android:textAlignment="center"
            android:textAllCaps="true"
            android:textColor="@color/black"
            android:textSize="22sp" />

        <android.support.design.widget.TabLayout
            android:id="@+id/perkSelector"
            style="@style/MyCustomTabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/hotSpotName"
            android:layout_marginBottom="10dp"
            app:tabGravity="center" />

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

        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/checkIn"
            android:layout_below="@id/perkSelector"
            android:layout_centerHorizontal="true">

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingEnd="20dp"
                android:paddingStart="20dp">

                <TextView
                    android:id="@+id/hotSpotHours"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="20dp"
                    android:text="@string/hours"
                    android:textColor="@color/grey"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/hotSpotDescription"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/hotSpotHours"
                    android:layout_marginTop="20dp"
                    android:lineSpacingExtra="10dp"
                    android:text="FILLER DESCRIPTION"
                    android:textAlignment="center"
                    android:textColor="@color/grey"
                    android:textSize="16sp" />
            </RelativeLayout>
        </ScrollView>

        <TextView
            android:id="@+id/checkIn"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/text_view_button"
            android:clickable="true"
            android:text="@string/check_in"
            android:textAlignment="center"
            android:textAllCaps="true" />
    </RelativeLayout>
</RelativeLayout>

fragment_perk.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/perkDescription"
    style="@style/MyCustomTextAppearance"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="PERK FILLER"
    android:textColor="@color/magnises_grey"
    android:textSize="18sp" />

1 个答案:

答案 0 :(得分:0)

问题是viewpager应该在viewpager标记内包含tablayout。这解决了这个问题。

e.g:

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="fill_parent" >
          <android.support.design.widget.TabLayout
            android:id="@+id/sliding_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/MY.DIALOG"
            card_view:tabTextColor="#666666"
            card_view:tabSelectedTextColor="#666666" />
        </android.support.v4.view.ViewPager>