Android

팝업 프래그먼트 만들기

최데브 2021. 6. 24. 10:27

 

PopupFragmentDialog.java  //다이얼로그 파일 

 

public class PopupFragmentDialog extends DialogFragment implements View.OnClickListener {

    private ImageView page1;
    private ImageView page2;
    private Button popup_close_btn;


    //content to show in the fragments
    public static final String TAG = "popup_fragment_dialog";
    public PopupFragmentDialog(){
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

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

        View view = inflater.inflate(R.layout.fragment_dialog_popup, container);
        //getDialog().setTitle("Pager Dialog");
        /**
         * 백버튼, 취소버튼, 배경터치 막음
         */
        if (getDialog() != null) {
            getDialog().setCanceledOnTouchOutside(false);
            //setCancelable(false);
        }

        ViewPager pager = (ViewPager) view.findViewById(R.id.pager);
        MyFragmentAdapter adapter = new MyFragmentAdapter(getChildFragmentManager());
        pager.setAdapter(adapter);

        /**
         * 페이징 번호
         */
        page1 = view.findViewById(R.id.img_page1);
        page2 = view.findViewById(R.id.img_page2);

        //닫기 버튼
        view.findViewById(R.id.popup_close_btn).setOnClickListener(this);

        // Attach the page change listener inside the activity
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                /*
                page1.setImageDrawable(getResources().getDrawable(R.drawable.pop_page_off));
                page2.setImageDrawable(getResources().getDrawable(R.drawable.pop_page_off));
                switch (position) {
                    case 0:
                        page1.setImageDrawable(getResources().getDrawable(R.drawable.pop_page_on));
                        break;
                    case 1:
                        page2.setImageDrawable(getResources().getDrawable(R.drawable.pop_page_on));
                        break;
                }*/
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        return view;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        return;
        //super.onDismiss(dialog);
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        //팝업창이 닫힐 때
        return;

    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.popup_close_btn:
                getDialog().dismiss();
                break;
        }
    }


    /**
     * innter class
     */
    public class MyFragmentAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                // find first fragment...
                PopupFirstFragment s = new PopupFirstFragment();
                return s;
            }

            if (position == 1) {
                // find second fragment...
                PopupSecondFragment s=new PopupSecondFragment();
                return s;
            }

            return null;
        }

        @Override
        public int getCount() {
            // Show 2 total pages.
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "First Tab";
                case 1:
                    return "Second Tab";

            }
            return null;
        }
    }
}

 

PopupFirstFragment.java

public class PopupFirstFragment extends Fragment implements View.OnClickListener {

    // Store instance variables
    private ImageView btnActionUrl;

    // newInstance constructor for creating fragment with arguments
    public static PopupFirstFragment newInstance(int page, String title) {
        PopupFirstFragment fragment = new PopupFirstFragment();
        Bundle args = new Bundle();
        args.putInt("someInt", page);
        args.putString("someTitle", title);
        fragment.setArguments(args);
        return fragment;
    }

    // Store instance variables based on arguments passed
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    // Inflate the view for the fragment based on layout XML
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_popup_first, container, false);



       // view.findViewById(R.id.btn_action_url).setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View view) {
        /*
        switch (view.getId()) {
            case R.id.btn_action_url:
            {
                String url ="https://m.search.naver.com/search.naver?sm=mtp_hty.top&where=m&query=%EB%A7%88%EC%9D%B4%EB%B1%85%ED%81%AC";
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);

                //popup_fragment_dialog
                //FragmentManager fm = getActivity().getSupportFragmentManager();
                DialogFragment df = (DialogFragment)getActivity().getSupportFragmentManager().findFragmentByTag("popup_fragment_dialog");
                df.dismiss();



                break;
            }
        }*/
    }
}

PopupSecondFragment.java

 

public class PopupSecondFragment extends Fragment {
    // Store instance variables
    private String title;
    private int page;

    // newInstance constructor for creating fragment with arguments
    public static PopupSecondFragment newInstance(int page, String title) {
        PopupSecondFragment fragment = new PopupSecondFragment();
        Bundle args = new Bundle();
        args.putInt("someInt", page);
        args.putString("someTitle", title);
        fragment.setArguments(args);
        return fragment;
    }

    // Store instance variables based on arguments passed
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    // Inflate the view for the fragment based on layout XML
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_popup_second, container, false);
        return view;
    }
}

 

fragment_dialog_popup.xml

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

    <Button
        android:id="@+id/popup_close_btn"
        android:background="@drawable/close_button_vector"
        android:layout_gravity="right"
        android:layout_margin="10dp"
        android:layout_width="20dp"
        android:layout_height="20dp">
    </Button>



    <androidx.viewpager.widget.ViewPager
        android:id="@+id/pager"
        android:layout_width="320dp"
        android:layout_height="270dp"/>

    <LinearLayout
        android:background="#ffffff"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">



        <!-- 닫기 버튼 -->
        <LinearLayout
            android:orientation="vertical"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="43dp"
                android:orientation="horizontal"
                android:gravity="center">
                <!-- 페이징 -->
                <ImageView
                    android:id="@+id/img_page1"
                    android:layout_width="11dp"
                    android:layout_height="11dp"
                    android:src="@drawable/ic_arrow_expand"
                    android:layout_gravity="center_horizontal|center_vertical|center"/>
                <ImageView
                    android:id="@+id/img_page2"
                    android:layout_width="11dp"
                    android:layout_height="11dp"
                    android:src="@drawable/close_button_vector"
                    android:layout_marginLeft="10dp"
                    android:layout_gravity="center_horizontal|center_vertical|center"/>
            </LinearLayout>

            <View
                android:background="#66bdbdbd"
                android:layout_width="297dp"
                android:layout_height="1dp" android:layout_gravity="center"
                />

        </LinearLayout>
    </LinearLayout>
</LinearLayout>

 

반응형