Android-如何从ViewPager的内部片段中关闭DialogFragment?

时间:2018-07-10 16:39:40

标签: android android-fragments android-viewpager android-dialogfragment

我想知道,我在DialogFragment内有一个ViewPager,我想知道如何将DialogFragment从ViewPager内的Fragment中解雇?这是我到目前为止的代码:

DialogFragment代码:

public class ReportDialog extends DialogFragment {
    public static ReportDialog newInstance() {
        ReportDialog rd = new ReportDialog();
        return rd;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_TITLE, android.R.style.Theme_Black_NoTitleBar_Fullscreen);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.report_dialog, container);
        ViewPager viewPager = (ViewPager) view.findViewById(R.id.pager);
        List<Fragment> fragments = getFragments();
        ReportAdapter reportAdapter = new ReportAdapter(getChildFragmentManager(), fragments);
        viewPager.setAdapter(reportAdapter);
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        return view;
    }

    private List<Fragment> getFragments(){
        List<Fragment> fList = new ArrayList<Fragment>();
        fList.add(MainReport.newInstance());
        //to add another fragment soon
        return fList;
    }
}

ViewPager代码:

public class CustomPager extends ViewPager {

    private View mCurrentView;

    public CustomPager(Context context) {
        super(context);
    }

    public CustomPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mCurrentView == null) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            return;
        }

        int height = 0;
        mCurrentView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int h = mCurrentView.getMeasuredHeight();
        if (h > height) height = h;
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void measureCurrentView(View currentView) {
        mCurrentView = currentView;
        requestLayout();
    }

    public int measureFragment(View view) {
        if (view == null) {
            return 0;
        }
        view.measure(0, 0);
        return view.getMeasuredHeight();
    }
}

MainReport代码:

public class MainReport extends Fragment {

    public View reportView;

    public static final MainReport newInstance() {
        MainReport mr = new MainReport();
        return mr;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View pageView = inflater.inflate(R.layout.activity_report, null);
        ImageButton ib;
        ib = (ImageButton) pageView.findViewById(R.id.done);
        ib.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //need to dismiss DialogFragment here
            }
        });

        ib = (ImageButton) pageView.findViewById(R.id.export);
        ib.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

            }
        });

        ib = (ImageButton) pageView.findViewById(R.id.ptview);
        ib.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

            }
        });
        return pageView;
    }
}

编辑-显示DialogFragment的代码:

//Located in main activity class
void showReportDialog() {
    DialogFragment reportDialog = ReportDialog.newInstance();
    reportDialog.show(getFragmentManager(), "report_dialog");
}

如何从ViewPager内部的片段中消除DialogFragment?

2 个答案:

答案 0 :(得分:0)

在您的onClick中添加以下内容

$(".results").text("Duration: " +
               response.rows[0].elements[0].duration.text);

在您的dialogFragment中添加以下内容

        Intent intent = new Intent(BROADCAST_ACTION);
    LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

答案 1 :(得分:0)

您可能可以执行以下操作:

void showReportDialog() {
    Fragment Transaction ft = getFragmentManager().beginTransaction();
    ft.addToBackStack("report_dialog");
    DialogFragment reportDialog = ReportDialog.newInstance();
    reportDialog.show(ft, "report_dialog");
}

然后在MainReport中:

getFragmentManager().popBackStack("report_dialog", FragmentManager.POP_BACK_STACK_INCLUSIVE);