如何在对话框片段的viewPager内的片段中填充适配器?

时间:2018-12-21 16:40:55

标签: android android-fragments android-dialogfragment

我正在尝试创建以下场景:

用户点击Button>会显示DialogFragment DialogFragment包含一个ViewPager,其中有2个标签-一个显示一个颜色选择器片段,另一个显示一个符号选择器片段 每个选取器片段都包含一个适配器,该适配器可以将颜色/符号阵列转换成漂亮的网格。

但是,它当前显示为空白-DialogFragment与片段的其他部分一起出现,但是没有适配器视图。

这就是我所拥有的:

初始活动称为EditActivity。它有一个Button,单击该函数会调用此函数以显示DialogFragmentPickers

public void showPickers() {
        if(mDialogFragmentPickers == null) return;
        mDialogFragmentPickers.setColourSet(mColourSet);
        mDialogFragmentPickers.show(mFragmentManager, "Pickers");
    }

DialogFragmentPickersonCreate的{​​{1}}方法中初始化,如下所示:

EditActivity

传入的mDialogFragmentPickers = new DialogFragmentPickers(); 基本上只是要在选择器中显示的颜色的数组。 ColourSet应该在初始化时传递给DialogFragmentPickers

FragmentColourPicker类如下所示(我只显示了与DialogFragmentPickers相关的部分,但是ColourPicker的工作方式相同):

SymbolPicker

此布局文件包含:

public class DialogFragmentPickers extends DialogFragment {

    private PickersPagerAdapter mPagerAdapter;
    private ViewPager           mViewPager;
    private ColourSet           mColourSet;
    private FragmentColourPicker    mColourPicker;

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

        // Inflate layout
        View view = inflater.inflate(R.layout.dialog_fragment_pickers, container, false);

        mPagerAdapter = new PickersPagerAdapter(getChildFragmentManager());

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

        return view;

    }

    public void setColourSet(ColourSet colourSet) {
        mColourSet = colourSet;
        if(mColourPicker == null) getColourPickerFragment();
        else mColourPicker.setColourSet(mColourSet);
    }

    private void getColourPickerFragment() {
        if (mPagerAdapter == null) return;
        mColourPicker = (FragmentColourPicker) mPagerAdapter.getItem(0);
        if(mColourSet != null) mColourPicker.setColourSet(mColourSet);
    }

    public class PickersPagerAdapter extends FragmentPagerAdapter
    {

        PickersPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch(position) {
                case 0:
                    return new FragmentColourPicker();
                case 1:
                    return new FragmentSymbolPicker();
                default:
                    return null;
            }
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return getResources().getString(R.string.colours);
                case 1:
                    return getResources().getString(R.string.symbols);
            }
            return null;
        }
    }

此寻呼机中使用的第一个片段是<android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Current + adjacent page titles...--> <android.support.v4.view.PagerTitleStrip android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:background="#33b5e5" android:paddingBottom="4dp" android:paddingTop="4dp" android:textColor="#fff" /> </android.support.v4.view.ViewPager> 。定义如下:

FragmentColourPicker

public class FragmentColourPicker extends Fragment { GridView mColourGrid; Context mContext; ColourSet mColourSet; ChartColourAdapter mColourAdapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_colour_picker, container, false); mColourGrid = view.findViewById(R.id.gridview_colourPicker); initialiseWithColourSet(); // Returns without doing anything if colourSet is not set up yet return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mContext = getActivity(); } @Override public void onDestroy(){ super.onDestroy();} public void setColourSet(ColourSet thisColourSet) { mColourSet = thisColourSet; initialiseWithColourSet(); // Returns without doing anything if the colourGrid is not set up yet } public void initialiseWithColourSet() { // Make sure that the colourSet and the colourGrid have both been initialised if(mColourSet == null) return; if(mColourGrid == null) return; // Now set up ChartColourAdapter to display colours in grid // Initialise currently highlighted colour to default // Set on click listener for each colour mColourAdapter = new ChartColourAdapter(mContext, R.layout.adapter_pattern_colour_cell_layout, mColourSet.getColours()); mColourGrid.setAdapter(mColourAdapter); } } 的布局文件(fragment_colour_picker.xml):

FragmentColourPicker

当我运行所有这些命令时,单击按钮会显示<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/colour_picker_fragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/fui_transparent" android:orientation="vertical"> <GridView android:id="@+id/gridview_colourPicker" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorBg" android:paddingBottom="@dimen/activity_standard_margin" android:columnWidth="@dimen/grid_cell_column_width" android:gravity="center" android:horizontalSpacing="10dp" android:numColumns="auto_fit" android:padding="@dimen/activity_small_margin" android:stretchMode="columnWidth" android:verticalSpacing="10dp" /> <TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:text="@string/prompt_swipe_for_symbols" android:padding="@dimen/activity_small_margin" android:background="@color/colorBg" /> </LinearLayout> ,并显示DialogFragment。我可以滑动到FragmentColourPicker并返回。但是,在ColourPicker中显示的唯一内容是FragmentSymbolPicker,它提示用户滑动查看符号。 TextView中没有显示实际颜色。

在调试器中,我认为问题在于GridView似乎只设置了initialiseWithColourSetcolourSet,而从未设置过。这样就可以返回,而无需尝试设置颜色适配器。

如果从colourGrid调用了initialiseWithColourSet,则设置colourGrid,但不设置colourSet(因为尚未传递)。这足够合理。但是,当稍后(从DialogFragment)传入colourSet时,colourGrid已恢复为空,因此该函数再次返回而未执行任何操作。

我显然有一些错误的顺序,或者正在重新初始化colourGrid / ColourPicker,而没有意识到。

1 个答案:

答案 0 :(得分:1)

在我看来,这是订单问题。为了确保您在正确的时间将数据传输到Fragment,我将通过构造函数将片段所需的数据传递到Adapter中。我会在那里保留它们的引用,然后使用静态newInstance(... requiredData)方法实例化这些片段。然后,可以确定当适配器“创建”它们时,它们将具有必需的信息。

类似这样的东西:

您的片段类:

...

Object myRequiredData;

static FragmentColourPicker newInstance(... requiredData) {
   FragmentColourPicker f = new FragmentColourPicker();
    f.setRequiredData(requiredData);        

    return f;
}

public void setRequiredData(Object reqData){
    this.myRequiredData = reqData;
}

...

您的适配器类:

public class PickersPagerAdapter extends FragmentPagerAdapter {

        Object reqData0;
        Object reqData1;

        PickersPagerAdapter(FragmentManager fm, Object reqData0, Object reqData1) {
            super(fm);
            this.reqData0 = reqData0;
            this.reqData1 = reqData1;
        }

        @Override
        public Fragment getItem(int position) {
            switch(position) {
                case 0:
                    return FragmentColourPicker.newInstance(reqData0);
                case 1:
                    return FragmentSymbolPicker.newInstance(reqData1);
                default:
                    return null;
            }
        }

    ...

}