【问题标题】:How to get the current item inside FragmentPagerAdapter?如何获取 FragmentPagerAdapter 中的当前项目?
【发布时间】:2019-10-03 14:09:54
【问题描述】:

我有以下代码 -

@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;
    }
}


【问题讨论】:

    标签: android fragmentpageradapter


    【解决方案1】:

    尝试在 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() 方法

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-22
      • 2012-10-19
      • 1970-01-01
      • 2014-03-01
      • 2020-04-25
      相关资源
      最近更新 更多