自定义对话框中的ViewPager [英] ViewPager in Custom Dialog

查看:56
本文介绍了自定义对话框中的ViewPager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义对话框来显示有关如何使用该应用程序的说明.我正在自定义对话框布局中使用ViewPager.我遇到错误java.lang.IllegalArgumentException:找不到片段FragmentForInstruction1的ID的视图.

I have custom dialog to show which has instruction about how to use the app.I am using ViewPager for this inside my custom dialog layout. I am getting error java.lang.IllegalArgumentException: No view found for id for fragment FragmentForInstruction1.

我创建了一个在onCreate(Bundle savedInstanceState){}方法中调用的方法.该方法会放大对话框的布局.

I have created a method which is called in onCreate(Bundle savedInstanceState) {} method .The method inflates the layout for dialog.

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();
    }

我的查看寻呼机类:

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中调用该方法:

and i call the method in 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代码:

FragmentForInstruction1 code:

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>

推荐答案

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

You should create an adapter that extends 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():

While crating an instance of it, pass the getChildFragmentManager() instead of getSupportFragmentManager():

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

这篇关于自定义对话框中的ViewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆