自定义对话框中的ViewPager

时间:2015-11-26 05:37:45

标签: android android-fragments

我有自定义对话框,显示哪些内容有关于如何使用app的说明。我在自定义对话框布局中使用ViewPager。 我收到了错误 java.lang.IllegalArgumentException:找不到片段FragmentForInstruction1的id的视图。

我创建了一个在onCreate(Bundle savedInstanceState){}方法中调用的方法。该方法使对话框的布局膨胀。

private void showCustomDialogForInstruction() {
        final Dialog dialog = new Dialog(MainActivity.this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View dialogLayout = inflater.inflate(R.layout.dialog_layout_for_instruction_message, null, false);
        layout.setAlpha(0.2f);

        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(dialogLayout);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));


        ViewPager viewPager=(ViewPager) dialog.findViewById(R.id.pagerInstruction);
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

        adapter.addFrag(new FragmentForInstruction1());
        adapter.addFrag(new Fragme`enter code here`ntForWelcomePage2());
        adapter.addFrag(new FragmentForWelcomePage3());
        adapter.addFrag(new FragmentForWelcomePage4());
        viewPager.setAdapter(adapter);



        viewPager.addOnPageChangeListener(new OnPageChangeListener()
        {

            @Override
            public void onPageSelected(int pos)
            {


            }

            @Override
            public void onPageScrolled(int pos, float arg1, int arg2)
            {

            }

            @Override
            public void onPageScrollStateChanged(int arg0)
            {

            }
        });


        Button done = (Button) dialog.findViewById(R.id.done);
        done.setText("Got It");
        done.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        Button neverShow = (Button) dialog.findViewById(R.id.nevershow);
        neverShow.setText("Never Show Again");
        neverShow.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode,
                    KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });
        dialog.getWindow().setBackgroundDrawable(
                new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialog.show();
    }

my view pager class:

public class ViewPagerAdapter extends FragmentPagerAdapter 
{
     private final List<Fragment> mFragmentList = new ArrayList<Fragment>();

     public ViewPagerAdapter(FragmentManager manager)
     {
         super(manager);
     }

     @Override
     public Fragment getItem(int position)
     {
         return mFragmentList.get(position);
     }

     @Override
     public int getCount() 
     {
         return mFragmentList.size();
     }

     public void addFrag(Fragment fragment)
     {
         mFragmentList.add(fragment);
     }


}

我在MainActivity中调用该方法:

public class MainActivity extends AppCompatActivity  {

    SharedPreferences sharedPreferencesNeverShowAgain ;
    boolean neverShowAgain;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        sharedPreferencesNeverShowAgain = PreferenceManager.getDefaultSharedPreferences(this);
        neverShowAgain = sharedPreferencesNeverShowAgain.getBoolean("NeverShowAgain", false);
        if(!neverShowAgain){
            showCustomDialogForMessage();
        }


    }
}

并且对话框的布局是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/dialog_shape"
    android:orientation="vertical" >

    <!-- layout title -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#00000000"
        android:gravity="center"
        android:orientation="vertical" >

        <ViewFlipper
            android:id="@+id/flipper"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/top_edge_rounded"
            android:flipInterval="2000"
            android:padding="20dp" >

            <TextView
                android:id="@+id/dialog_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#00000000"
                android:gravity="center"
                android:text="Input Height"
                android:textColor="@color/textColor"
                android:textStyle="normal" />

            <TextView
                android:id="@+id/title1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#00000000"
                android:gravity="center"
                android:text="Input Height"
                android:textColor="@color/accent"
                android:textStyle="normal" />
        </ViewFlipper>

        <View
            android:id="@+id/tri"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/triangle"
            android:rotation="180" />
    </LinearLayout>

    <!-- layout dialog content -->

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="300dp" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <android.support.v4.view.ViewPager
                android:id="@+id/pagerInstruction"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <LinearLayout
                android:id="@+id/layoutIndicater"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:orientation="horizontal" >
            </LinearLayout>
        </RelativeLayout>
    </ScrollView>
    <!-- layout dialog buttons -->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_margin="10dp"
        android:background="@drawable/all_rounded_edge_plum_for_dialog" >

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_alignParentTop="true"
            android:background="@color/textColor" />

        <View
            android:id="@+id/ViewColorPickerHelper"
            android:layout_width="1dp"
            android:layout_height="fill_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:background="@color/textColor" />

        <Button
            android:id="@+id/nevershow"
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@id/ViewColorPickerHelper"
            android:background="?android:attr/selectableItemBackground"
            android:padding="5dp"
            android:text="Close"
            android:textColor="@color/textColor" />

        <Button
            android:id="@+id/done"
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@id/ViewColorPickerHelper"
            android:background="?android:attr/selectableItemBackground"
            android:padding="5dp"
            android:text="Done"
            android:textColor="@color/textColor" />
    </RelativeLayout>

</LinearLayout>

FragmentForInstruction1代码:

public class FragmentForInstruction1 extends Fragment{

    @Override
    @Nullable
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.fragmentinstructionpage1, container,false);
        return view;
    }


}

及其布局为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/backgroundColor"
    android:gravity="center" >


     <TextView 
        android:id="@+id/titleWelcomeFragment1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="@color/colorPrimary"
        android:textSize="30sp"
        android:textStyle="bold"/>  

    <TextView 
        android:id="@+id/messageWelcomeFragment1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="@color/colorPrimary"
        android:textSize="20sp"
        android:textStyle="normal"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"/>


</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您应该创建一个扩展FragmentPagerAdapter的适配器:

private class CustomPagerAdapter extends FragmentPagerAdapter {

    public CustomPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new FragmentForInstruction1();
            case 1:
                return new FragmentForInstruction2();
            case 2:
                return new FragmentForInstruction3();
            case 3:
                return new FragmentForInstruction4();
            default:
                return new FragmentForInstruction1();
        }
    }

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

在创建它的实例时,传递getChildFragmentManager()而不是getSupportFragmentManager():

viewPager.setAdapter(new CustomPagerAdapter(getChildFragmentManager()));