繁体   English   中英

Android:如何在片段中按返回按钮时隐藏底页?

[英]Android: How to hide bottom sheet when back button is pressed in fragment?

我有一个片段,里面有一个底页,按下后退按钮时我想隐藏它,然后第二个后退到上一个片段,我尝试使用onBackPressed但它不起作用。 setOnKeyListener()也不起作用。 一些帮助解决方案,欢迎光临。

这可能对您有帮助!

   @Override
public void onBackPressed() {
    touchToBack++;

    if (touchToBack== 1) {
        if (bottomSheetBehavior!=null && bottomSheetBehavior.getState() !=
                BottomSheetBehavior.STATE_HIDDEN) {
            bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
            touchToBack= 0;
            return;
        }

    }

    if (touchToBack== 1 || touchToBack> 1) {
        if (bottomSheetBehavior!=null && bottomSheetBehavior.getState() ==
                BottomSheetBehavior.STATE_HIDDEN) {
                           touchToBack= 0;
                           super.onBackPressed();
        }

    }

}

这是我的代码

public class ProfileImageFragment extends Fragment {

    View view;
    private ImageView profileImg, editIconImg, ivBsDelete, ivBsGallery;
    private BottomSheetBehavior bottomSheetBehavior;
    private RelativeLayout bottomSheetLayout;
    private static final int REQUEST_READ_MEDIA = 1;
    private static final int RESULT_OK = -1;

    public ProfileImageFragment() {
       // Required empty public constructor
    }

    public static ProfileImageFragment newInstance() {return new ProfileImageFragment();}

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
         // Inflate the layout for this fragment
         view = inflater.inflate(R.layout.fragment_profile_image, container, false);
         profileImg = (ImageView) view.findViewById(R.id.profileImage);
         editIconImg = (ImageView) view.findViewById(R.id.editProfileImage); //edit option

         bottomSheetLayout = view.findViewById(R.id.bottom_sheet2);
         bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetLayout);
         bottomSheetBehavior.setHideable(true);
         bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);

         //when longpress on image appears the bottomSheet
         profileImg.setOnLongClickListener(new View.OnLongClickListener() {
         @Override
            public boolean onLongClick(View v) {
            bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
            return true;
            }
         });

         //when only clik on image hide the bottomSheet
         profileImg.setOnClickListener(new View.OnClickListener() {
         @Override
            public void onClick(View view) {
            bottomSheetBehavior.setHideable(true);//Important to add
            bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
            }
         });

         //when only clik on image appears the bottomSheet
         editIconImg.setOnClickListener(new View.OnClickListener() {
         @Override
            public void onClick(View view) {
            bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
            }
         });


         //delete option of bottomsheet
         ivBsDelete = (ImageView) view.findViewById(R.id.ivBsDelete);
         ivBsDelete.setOnClickListener(new View.OnClickListener(){
         @Override
         public void onClick(View v){
            ((MainActivity) getActivity()).imgPath = "";
            //Picasso.with(getContext()).invalidate(imagePath); //clear cache
            profileImg.setImageResource(R.drawable.user);
            ((MainActivity)getActivity()).imgProfile.setImageResource(R.mipmap.ic_launcher_round);

            bottomSheetBehavior.setHideable(true);
            bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
            }
         });

         //add image from gallery option of bottomsheet
         ivBsGallery = (ImageView) view.findViewById(R.id.ivBsGallery);
         ivBsGallery.setOnClickListener(new View.OnClickListener(){
         @Override
            public void onClick(View view){
            int permissionCheck = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);

            if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_READ_MEDIA);
            }else{
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, REQUEST_READ_MEDIA);
            }
            bottomSheetBehavior.setHideable(true);
            bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
        }
    });
    return view;
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    //GETTING IMAGE FROM GALLERY
    if (requestCode == REQUEST_READ_MEDIA && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cursor = this.getActivity().getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        ((MainActivity) getActivity()).imgPath = picturePath;
        cursor.close();

        Call<Response.LoadPictureProfile> call2 = ((MainActivity) getActivity()).getDataService.loadPictureProfile(((MainActivity) getActivity()).user.token, ((MainActivity) getActivity()).user.loadPictureProfile(picturePath));
        call2.enqueue(new Callback<Response.LoadPictureProfile>() {
            @Override
            public void onResponse(Call<Response.LoadPictureProfile> call, retrofit2.Response<Response.LoadPictureProfile> response) {
                ((MainActivity) getActivity()).responseLoadPictureProfile = response.body();
                if(((MainActivity) getActivity()).responseLoadPictureProfile.status)
                {
                    alert("Request", ((MainActivity) getActivity()).responseLoadPictureProfile.toString());

                }else{
                    alert("Mensaje", ((MainActivity) getActivity()).user.errorMsg(((MainActivity) getActivity()).responseLoadPictureProfile.error_code));
                }
            }

            @Override
            public void onFailure(Call<Response.LoadPictureProfile> call, Throwable t) {

            }
        });

        Picasso.with(getContext())
                .load(new File(((MainActivity) getActivity()).imgPath))
                .fit()
                .centerCrop()
                .into(profileImg);

        if(!((MainActivity) getActivity()).imgPath.isEmpty()){
            Picasso.with(getContext())
                    .load(new File(((MainActivity) getActivity()).imgPath))
                    .fit()
                    .centerCrop()
                    .into(((MainActivity)getActivity()).imgProfile);
        }
    }
}

protected void alert(String title,String message){
    android.app.AlertDialog alertDialog = new android.app.AlertDialog.Builder(getContext()).create();
    alertDialog.setTitle(title);
    alertDialog.setMessage(message);
    alertDialog.setButton(android.app.AlertDialog.BUTTON_NEUTRAL, "OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
    alertDialog.show();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM