Android在对话框片段中启动片段

时间:2016-08-19 07:11:35

标签: android android-fragments

我有一个对话框片段,我需要在此对话框片段中启动一个片段。这意味着我的片段应该占据与对话框片段相同的窗口。我该怎么做呢?这是我的对话框片段代码 -

public class CallDialogFragment extends DialogFragment {
    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.call_dialog_fragment, container, false);


            return view;
        }
}

2 个答案:

答案 0 :(得分:0)

只需使用片段事务并添加另一个对话框片段。并添加到后台。

答案 1 :(得分:0)

你可以使用对话框片段的协调器布局..当你将布局拖到上面时,它将是你的新片段。当你向下拖动时,它将是你的对话框片段。

这是xml代码..

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <android.support.design.widget.AppBarLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:local="http://schemas.android.com/apk/res-auto"
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/detail_backdrop_height"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true"
        >

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            local:contentScrim="?attr/colorPrimary"
            local:expandedTitleMarginEnd="64dp"
            local:expandedTitleMarginBottom="22dp"
            local:expandedTitleMarginStart="20dp"
            local:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Title"
            local:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ProgressBar
                android:id="@+id/progressfull"
                android:visibility="visible"
                android:layout_width="match_parent"
                android:layout_marginTop="80dp"
                android:layout_height="24dp" />

            <ImageView
                android:id="@+id/image_preview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                local:layout_collapseMode="parallax"
                />

            <View
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_gravity="bottom"
                android:layout_alignBottom="@+id/image_preview"
                android:background="?attr/colorPrimary"
                local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbarIcon"
                android:layout_height="?attr/actionBarSize"
                android:layout_width="match_parent"
                local:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                local:layout_collapseMode="pin"/>


        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/addbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        local:borderWidth="0dp"
        android:layout_marginRight="@dimen/fab_margin"
        android:layout_marginLeft="@dimen/fab_margin"
        android:layout_marginTop="@dimen/fab_margin"
        android:layout_marginBottom="25dp"
        android:src="@drawable/ic_add" />

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"
        android:background="@color/colorPrimaryDark"
        local:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingTop="24dp">

            <android.support.v7.widget.CardView
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"

                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                </LinearLayout>

            </android.support.v7.widget.CardView>

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>


</android.support.design.widget.CoordinatorLayout>
  1. fragment_image_slider.xml

      
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </android.support.v4.view.ViewPager>
    
    </LinearLayout>
    
  2. 这是java代码..

    public class SlideshowDialogFragment extends DialogFragment {
    
        private String TAG = SlideshowDialogFragment.class.getSimpleName();
        private ArrayList<ItemCategories> images;
        private ViewPager viewPager;
        private MyViewPagerAdapter myViewPagerAdapter;
        private String lblCount, lblTitle, lblDate;
        private int selectedPosition = 0;
        private CardView cardView;
        private Context mContext;
    
        public static SlideshowDialogFragment newInstance() {
            SlideshowDialogFragment fragmentFullScreen = new SlideshowDialogFragment();
            return fragmentFullScreen;
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
            View v = inflater.inflate(R.layout.fragment_image_slider, container, false);
            viewPager = (ViewPager) v.findViewById(R.id.viewpager);
    
            setHasOptionsMenu(true);
    
            ViewCompat.setOnApplyWindowInsetsListener(viewPager,
                    new OnApplyWindowInsetsListener() {
                        @Override
                        public WindowInsetsCompat onApplyWindowInsets(View v,
                                                                      WindowInsetsCompat insets) {
                            insets = ViewCompat.onApplyWindowInsets(v, insets);
                            if (insets.isConsumed()) {
                                return insets;
                            }
    
                            boolean consumed = false;
                            for (int i = 0, count = viewPager.getChildCount(); i <  count; i++) {
                                ViewCompat.dispatchApplyWindowInsets(viewPager.getChildAt(i), insets);
                                if (insets.isConsumed()) {
                                    consumed = true;
                                }
                            }
                            return consumed ? insets.consumeSystemWindowInsets() : insets;
                        }
                    });
    
    
            images = (ArrayList<ItemCategories>) getArguments().getSerializable("images");
            selectedPosition = getArguments().getInt("position");
    
            Log.e(TAG, "position: " + selectedPosition);
            Log.e(TAG, "images size: " + images.size());
    
            myViewPagerAdapter = new MyViewPagerAdapter();
            viewPager.setAdapter(myViewPagerAdapter);
            viewPager.addOnPageChangeListener(viewPagerPageChangeListener);
    
            setCurrentItem(selectedPosition);
    
            getArguments().remove("position");
            getArguments().remove("images");
    
            return v;
        }
    
            private void setCurrentItem(int position) {
                viewPager.setCurrentItem(position, false);
                displayMetaInfo(selectedPosition);
            }
    
            //  page change listener
            ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {
    
            @Override
            public void onPageSelected(int position) {
                displayMetaInfo(position);
            }
    
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
    
            }
    
            @Override
            public void onPageScrollStateChanged(int arg0) {
    
            }
        };
    
        private void displayMetaInfo(int position) {
    
            lblCount = (position + 1) + " of " + images.size();
            ItemCategories image = images.get(position);
            lblTitle = "" + image.getCategoryItem();
            lblDate = "" + image.getUrlThumb();
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setStyle(DialogFragment.STYLE_NORMAL, R.style.MyMaterialThemeFull);
        }
    
        @Override
        public void onDetach() {
            super.onDetach();
        }
    
        // adapter
        public class MyViewPagerAdapter extends PagerAdapter {
    
            private LayoutInflater layoutInflater;
    
    
            public MyViewPagerAdapter() {
            }
    
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
    
                layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View view = layoutInflater.inflate(R.layout.image_fullscreen_preview, container, false);
    
                ImageView imageViewPreview = (ImageView) view.findViewById(R.id.image_preview);
    
                Toolbar toolbarIcon = (Toolbar) view.findViewById(R.id.toolbarIcon);
                ((AppCompatActivity) getActivity()).setSupportActionBar(toolbarIcon);
                ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
                CollapsingToolbarLayout collapsingToolbar =
                        (CollapsingToolbarLayout) view.findViewById(R.id.collapsing_toolbar);
    
    
                AppBarLayout appBarLayout = (AppBarLayout) view.findViewById(R.id.appbar);
    
                DisplayMetrics displaymetrics = new DisplayMetrics();
                getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
                int screenHeight = displaymetrics.heightPixels;
    
                CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)appBarLayout.getLayoutParams();
                lp.height = screenHeight;
    
                toolbarIcon.setNavigationOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Fragment prev = getActivity().getSupportFragmentManager().findFragmentByTag("slideshow");
                        if (prev != null) {
                            DialogFragment dialogFullScreen = (DialogFragment) prev;
                            dialogFullScreen.dismiss();
                        }
                    }
                });
    
    
                ItemCategories image = images.get(position);
    
    
                Glide.with(getActivity()).load(image.getUrlThumb())
                        .thumbnail(0.5f)
                        .crossFade()
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(imageViewPreview);
    
                collapsingToolbar.setTitle(lblTitle);
                container.addView(view);
    
                return view;
            }
    
    
    
            @Override
            public int getCount() {
                return images.size();
            }
    
            @Override
            public boolean isViewFromObject(View view, Object obj) {
                return view == ((View) obj);
            }
    
    
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
    
        }
    }
    

    只需复制此代码,您就会知道我在说什么