【问题标题】:ViewPager in Custom Dialog自定义对话框中的 ViewPager
【发布时间】:2015-11-26 05:37:44
【问题描述】:

我有自定义对话框来显示有关如何使用应用程序的说明。我在自定义对话框布局中为此使用 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();
    }

我的视图寻呼机类:

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>

【问题讨论】:

  • ViewPagerAdapter 的代码在哪里?这似乎不是 Android 组件
  • 公共类 ViewPagerAdapter 扩展 FragmentPagerAdapter
  • 它有两个方法:public ViewPagerAdapter(FragmentManager manager),public void addFrag(Fragment fragment) public ViewPagerAdapter(FragmentManager manager) { super(manager); } public void addFrag(Fragment fragment) { mFragmentList.add(fragment); }
  • 让我们看看你的第一个片段的代码。你确定要调用 onCreateView() 吗?
  • 请在对话框中查看 View Pager 的视频 youtube.com/watch?v=pJG5ZuZtPKY

标签: android android-fragments


【解决方案1】:

您应该创建一个扩展 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()));

【讨论】:

  • 显示错误:方法 getChildFragmentManager() 未为 MainActivity 类型定义。我的 MainActivity 扩展了 AppCompatActivity.Min api 版本是 14
  • 您是在 MainActivity 中还是在 DialogFragement 中创建此类?
  • 我有主要活动,我在其中调用对话框的方法,viewPagerAdapter 是视图分页器的另一个类。我没有使用 DialogFragment。如果我在没有 viewpager 和片段的情况下膨胀布局,它可以工作并显示在屏幕上,但是如果添加 viewPager.setAdapter(new CustomPagerAdapter(getSupportFragmentManager()));然后它显示错误。
  • 您应该使用 DialogFragment 来显示包含其他片段的对话框(viewPager 中的片段)
  • 我创建了一个新类 AlertDFragment 扩展了 DialogFragment,它不显示错误但不显示片段数据,我在对话框中得到了空白屏幕。
猜你喜欢
  • 1970-01-01
  • 2017-05-10
  • 2014-07-29
  • 1970-01-01
  • 2015-10-16
  • 2011-10-18
  • 2013-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多