如何在Android上的Fragment中设置onBackPress

时间:2017-09-12 13:57:54

标签: android android-fragments navigation-drawer

在我的应用程序中,我使用ViewPager将两个fragments显示为活动 在其中一个fragments我使用NavigationDrawer时,我想点击 onBackPress 关闭此NavigationDrawer

我在下面写了打开这个抽屉的代码:

reviewSerialFrag_DrawerLayout.openDrawer(Gravity.END);

我想点击onBackPress 关闭此抽屉,代码如下:

reviewSerialFrag_DrawerLayout.closeDrawer(Gravity.END);

我该怎么办?请帮帮我

5 个答案:

答案 0 :(得分:0)

procedure TContactController.UpdateComponent(Sender: TObject);
var
  I: Integer;
begin
  UpdateComponentActive := True;

  If Sender = pnlBasicInformation
  then begin
    pnlBasicInformation.Caption := UpperCase(Trim(Format('%s %s', [CurrentContact.Name, CurrentContact.FirstName])));
    If CurrentContact.HomeAddress.PostalCode.City.CityDescription[ApplicationManager.CurrentLanguageId] <> ''
    then pnlBasicInformation.Caption := Format('%s - %s',
                                               [pnlBasicInformation.Caption,
                                                UpperCase(CurrentContact.HomeAddress.PostalCode.City.CityDescription[ApplicationManager.CurrentLanguageId])]);
    pnlBasicInformation.Caption := StringReplace(pnlBasicInformation.Caption, '&', '&&', [rfReplaceAll]);
  end;

  If Sender = gbtnLock
  then begin
    If CurrentContact.Locked
    then gbtnLock.ImageIndex := 7
    else gbtnLock.ImageIndex := 19;
  end;

  If Sender = edtInternalReference
  then edtInternalReference.Text := CurrentContact.InternalReference;

  If Sender = edtExternalReference
  then edtExternalReference.Text := CurrentContact.ExternalReference;

  If Sender = edtFirstName
  then edtFirstName.Text := CurrentContact.FirstName;

  If Sender = edtName
  then edtName.Text := CurrentContact.Name;

  If Sender = edtSubName
  then edtSubName.Text := CurrentContact.SubName;

  If Sender = cbContactFunction
  then begin
    cbContactFunction.ItemIndex := -1;
    For I := 0 to cbContactFunction.Items.Count-1 do
    begin
      If TContactFunction(cbContactFunction.Items.Objects[I]).Id = CurrentContact.ContactFunctionId
      then begin
        cbContactFunction.ItemIndex := I;
        break;
      end;
    end;
  end;

  If Sender = cbLanguage
  then begin
    cbLanguage.ItemIndex := -1;
    For I := 0 to cbLanguage.Items.Count-1 do
    begin
      If TLanguage(cbLanguage.Items.Objects[I]).Id = CurrentContact.LanguageId
      then begin
        cbLanguage.ItemIndex := I;
        break;
      end;
    end;
  end;

  If Sender = cbSalutation
  then begin
    cbSalutation.ItemIndex := -1;
    For I := 0 to cbSalutation.Items.Count-1 do
    begin
      If TSalutation(cbSalutation.Items.Objects[I]).Id = CurrentContact.SalutationId
      then begin
        cbSalutation.ItemIndex := I;
        break;
      end;
    end;
  end;

  If Sender = edtCallingCodeMobilePhone
  then edtCallingCodeMobilePhone.Text := CurrentContact.CallingCodeMobilePhone;

  If Sender = edtMobilePhone
  then begin
    edtMobilePhone.Text := CurrentContact.MobilePhone;
    edtMobilePhone.OnKeyPress := OnPhoneNumberKeyPress;
  end;

  If Sender = gbtnMobilePhone
  then gbtnMobilePhone.Enabled := Trim(CurrentContact.MobilePhone) <> '';

  If Sender = gbtnMobilePhoneSms
  then gbtnMobilePhoneSms.Enabled := Trim(CurrentContact.MobilePhone) <> '';

  If Sender = edtBirthDate
  then edtBirthDate.Text := Format('dd/mm/yyyy', [CurrentContact.BirthDate]);

  If Sender = rbGenderM
  then rbGenderM.Checked := (CurrentContact.Gender = 0);

  If Sender = rbGenderV
  then rbGenderV.Checked := (CurrentContact.Gender = 1);

  If Sender = edtIdentityCardNumber
  then edtIdentityCardNumber.Text := CurrentContact.IdentityCardNumber;

  If Sender = edtNationalNumber
  then edtNationalNumber.Text := CurrentContact.NationalNumber;

  If Sender = imgProfilePhoto
  then imgProfilePhoto.Picture.Assign(ProfilePhoto.Picture.Graphic);

  If Sender = gbtnRemovePhoto
  then gbtnRemovePhoto.Enabled := PhotoManager.PhotoExists(pmContact, CurrentContact.Id);

  If Sender = edtRemarks
  then edtRemarks.Text := CurrentContact.Remarks;

  If Sender = edtInfo
  then edtInfo.Text := CurrentContact.Info;

  If Sender = edtRowVersion
  then edtRowVersion.Text := Format('dd/mm/yyyy', [CurrentContact.RowVersion]);

  UpdateComponentActive := False;
end;

答案 1 :(得分:0)

Activity onBackPressed()下面写下代码

@Overrdie
public void onBackPressed(){
Fragment currentFragment = getActivity().getFragmentManager().findFragmentById(R.id.fragment_container);

   if(currentFragment instanceof YourDrawerFragment && reviewSerialFrag_DrawerLayout.isDrawerVisible(GravityCompat.END))
        {
            reviewSerialFrag_DrawerLayout.closeDrawer(GravityCompat.END);
            return;
        }
    super.onBackPresses():
}

答案 2 :(得分:0)

您可以执行此操作以关闭背面的抽屉

@Overrdie
public void onBackPressed(){
super.onBackPresses():
if(reviewSerialFrag_DrawerLayout.isDrawerVisible(GravityCompat.END)){
        reviewSerialFrag_DrawerLayout.closeDrawer(GravityCompat.END);
 }
}

答案 3 :(得分:0)

一般情况下,我发现最简单的方法是使用Observer模式,然后将委托后向压缩事件放到片段中。这将允许您将活动和片段关注点分开。

interface OnBackPressedListener {
  public void onBackPressed();
}

然后,在您的片段中,实现此OnBackPressedListener

class MyFragment extends Fragment implements OnBackPressedListener{

  public void onBackPressed(){...}

}

最后,在您的活动中,您可以执行以下操作:

class MyActivity extends Activity {
  @Override
  public void onBackPressed(){

    // Grab all the fragments that are 'observing' the back press event
    Fragment currentFragment =
      getFragmentManager().findFragmentById(R.id.fragment_container);

    if(currentFragment != null && currentFragment instanceof OnBackPressedListener) {
      // delegate this back press event down to the fragment
      OnBackPressedListener backFragment = (OnBackPressedListener) currentFragment;
      backFragment.onBackPressed();
    }

    super.onBackPressed():
}

答案 4 :(得分:0)

在Jon的解决方案中,将始终调用super.onBackPressed。情况可能不一定如此。 为此,实现返回一个布尔值并根据结果进行操作就足够了。

我的解决方案几乎相同here

firebase deploy

请参阅more details

的链接