如何获取 FragmentPagerAdapter 中的当前项目?

How to get the current item inside FragmentPagerAdapter?

我有以下代码 -

@Override
  public void sendDeviceIdResult(String deviceId, boolean isAlreadyExist) {

    int currentItem = viewPager.getCurrentItem();
    Fragment item = adapter.getItem(currentItem);
    if (item instanceof PhoneStateAndAgeVerificationFragment) {
      Dialog dialog = ((PhoneStateAndAgeVerificationFragment) item).getProgressDialog();
      if (dialog != null && dialog.isShowing()) {
        dialog.dismiss();
      }
    }

    Intent intent = new Intent();
    intent.putExtra(KEY_DEVICE_ID, deviceId);
    intent.putExtra(KEY_IS_SUCCESS, !isAlreadyExist);
    setResult(RESULT_OK, intent);
    finish();


  }

问题是 getItem(position) 这一行没有得到当前片段的实际引用,而是创建了一个全新的片段。

我想获取当前的引用并忽略它的 AlertDialog

我知道这个问题已经被问过很多次了,但我在阅读所有解决方案时完全迷失了方向,所以如果有人可以给我看一些与我的案例相关的东西?

这是我的 PhoneStateAndAgeVerificationFragment -


package com.onemdtalent.app.ui.verification;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.core.content.ContextCompat;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.onemdtalent.app.App;
import com.onemdtalent.app.R;
import com.onemdtalent.app.core.base.AbstractFragment;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

public class PhoneStateAndAgeVerificationFragment extends AbstractFragment{
    @BindView(R.id.frag_verification_phone_checkbox_age)
    CheckBox checkBoxAge;
    private AlertDialog.Builder mBuilder;
    private Dialog mDialog;


    public static Fragment newInstance() {
        return new PhoneStateAndAgeVerificationFragment();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mBuilder = new AlertDialog.Builder(getActivity(), R.style.DialogTheme);
    }

    @OnCheckedChanged(R.id.frag_verification_phone_checkbox_age)
    void OnAgeCheckedChanged(CompoundButton button, boolean checked) {
        if (checked) {
            button.setTextColor(ContextCompat.getColor(button.getContext(), R.color.black));
        }
    }

    @OnClick(R.id.verification_button_got_it)
    void onBtnGotItClicked(View view) {
        if (!checkBoxAge.isChecked()) {
            checkBoxAge.setTextColor(ContextCompat.getColor(checkBoxAge.getContext(), R.color.accent_red));
            return;
        }
        showProgressDialog();
        if (getContext() instanceof VerificationPageListener) {
            ((VerificationPageListener) getContext()).onPageAgreed();
        }
    }
    @Override
    protected int getLayoutResourceId() {
        return R.layout.layout_verification_phone_age;
    }


    private void showProgressDialog(){
        if (mBuilder == null) {
            mBuilder = new AlertDialog.Builder(App.getAppContext());
        }
        mBuilder.setCancelable(false);
        mBuilder.setView(R.layout.custom_proggress_dialog);
        mDialog = mBuilder.create();
        mDialog.show();
    }

    public Dialog getProgressDialog() {
        return mDialog;
    }
}


尝试在您的 FragmentPagerAdapter 中添加以下代码,这对我有用。

public class MyPagerAdapter extends FragmentPagerAdapter {

    private Fragment mCurrentFragment;

    public Fragment getCurrentFragment() {
        return mCurrentFragment;
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        if (getCurrentFragment() != object) {
            mCurrentFragment = ((Fragment) object);
        }
        super.setPrimaryItem(container, position, object);
    }
}

从您的 sendDeviceIdResult() 方法调用 getCurrentFragment() 方法

希望对您有所帮助。