片段不是全屏

时间:2018-05-23 07:12:24

标签: android android-fragments

我有一个从活动(活动A)调用的片段,然后我需要创建另一个重用该片段的活动(活动B)。该片段已成功显示在 activity B 中,但它不是全屏,并且不会替换父级,如下所示

enter image description here

这就是我显示片段的方式

2018/05/23 - 附上全班

public class InvMainDashboardDetailUkm extends AppCompatActivity implements MainDashboardFragment.OnFragmentInteractionListener {

@BindView(R.id.invDetailUkmImage)
ImageView invDetailUkmImage;

@BindView(R.id.invDetailUkmName)
EditText invDetailUkmName;

@BindView(R.id.invDetailUkmUsaha)
EditText invDetailUkmUsaha;

@BindView(R.id.invDetailUkmAddress)
EditText invDetailUkmAddress;

@BindView(R.id.invDetailUkmProvince)
EditText invDetailUkmProvince;

@BindView(R.id.invDetailUkmCity)
EditText invDetailUkmCity;

@BindView(R.id.invDetailUkmDistrict)
EditText invDetailUkmDistrict;

@BindView(R.id.invDetailUkmSubDistrict)
EditText invDetailUkmSubDistrict;

@BindView(R.id.invDetailUkmPostalCode)
EditText invDetailUkmPostalCode;

@BindView(R.id.invDetailUkmPhone)
EditText invDetailUkmPhone;

@BindView(R.id.btnInvGetAndShare)
Button btnInvGetAndShare;

@BindView(R.id.investorDetailUkm)
RelativeLayout investorDetailUkm;

Button btnInvViewReportBalance;

String ukmId;

private Fragment fragment;

ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.investor_detail_ukm);
    ButterKnife.bind(this);

    ukmId = getIntent().getExtras().getString("viewDetailUkmId");
    getInvestorDetailUkm(ukmId);

    btnInvViewReportBalance = findViewById(R.id.btnInvViewReportBalance);
    btnInvViewReportBalance.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Toast.makeText(InvMainDashboardDetailUkm.this, "test", Toast.LENGTH_SHORT).show();

            //investorDetailUkm.setVisibility(View.GONE);

            Bundle detailUkmBundle = new Bundle();
            detailUkmBundle.putString("viewUkmId", ukmId);
            fragment = new MainDashboardFragment();
            fragment.setArguments(detailUkmBundle);

            FragmentTransaction fragmentManager = InvMainDashboardDetailUkm.this.getSupportFragmentManager().beginTransaction();
            fragmentManager.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            fragmentManager.addToBackStack(null);

            fragmentManager.replace(R.id.investorDetailUkm, fragment).commit();
            //hideSystemUI();

        }
    });

    btnInvGetAndShare.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String invId = ConstantVar.investorId;
            Intent intent = new Intent(getApplicationContext(), InvestorOffering.class);
            intent.putExtra("investorOfferingInvId", invId);
            intent.putExtra("investorOfferingUkmId", ukmId);
            startActivity(intent);

        }
    });
}

public void getInvestorDetailUkm(String ukmId){
    Call<ViewInvestorUkmDetailResponse> call = apiService.invViewUkmDetail(ukmId);
    call.enqueue(new Callback<ViewInvestorUkmDetailResponse>() {
        @Override
        public void onResponse(Call<ViewInvestorUkmDetailResponse> call, Response<ViewInvestorUkmDetailResponse> response) {
            if(response.body().getCode().equals(1000)){
                if(response.body().getData().getPhotoUkm()!=""){
                    Picasso.get().load(response.body().getData().getPhotoUkm()).into(invDetailUkmImage);
                }
                invDetailUkmName.setText(response.body().getData().getUkmName());
                //invDetailUkmUsaha.setText(response.body().getData().get());
                invDetailUkmAddress.setText(response.body().getData().getAddress());
                invDetailUkmProvince.setText(response.body().getData().getProvince());
                invDetailUkmCity.setText(response.body().getData().getCity());
                invDetailUkmDistrict.setText(response.body().getData().getKecamatan());
                invDetailUkmSubDistrict.setText(response.body().getData().getKelurahan());
                invDetailUkmPostalCode.setText(response.body().getData().getPostCode().toString());
                invDetailUkmPhone.setText(response.body().getData().getPhoneNumber());
            }else{
                Toast.makeText(InvMainDashboardDetailUkm.this, "Failed Get Data", Toast.LENGTH_SHORT).show();
            }

        }

        @Override
        public void onFailure(Call<ViewInvestorUkmDetailResponse> call, Throwable t) {
            Toast.makeText(InvMainDashboardDetailUkm.this, "Error Get Data", Toast.LENGTH_SHORT).show();
        }
    });
}

@Override
public void onFragmentInteraction(Uri uri) {

}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        Toast.makeText(this, "test", Toast.LENGTH_SHORT).show();
        hideSystemUI();
    }
}

private void hideSystemUI() {
    if (hasWindowFocus()) {
        Toast.makeText(this, "hahaha", Toast.LENGTH_SHORT).show();
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_IMMERSIVE
                        // Set the content to appear under the system bars so that the
                        // content doesn't resize when the system bars hide and show.
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        // Hide the nav bar and status bar
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN);  
        }
    }
}

2018/05/23 - AddFragment Code

public class MainDashboardFragment extends Fragment {

@BindView(R.id.dashboardChartRecycle)
RecyclerView dashboardChartRecycle;

private ArrayList<DashboardResponseData> dataList;
private UkmMainDataAdapter ukmMainDataAdapter;

ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);

private OnFragmentInteractionListener mListener;


@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 view = inflater.inflate(R.layout.fragment_main_dashboard, container, false);
    ButterKnife.bind(this, view);

    String addId = getArguments().getString("viewUkmId");

    getHomeScreen(addId);

    return view;
}


@Override
public void onAttach(Context context) {
    super.onAttach(context);
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

@Override
public void onStart() {
    super.onStart();
    try {
        mListener = (MainDashboardFragment.OnFragmentInteractionListener) getActivity();
    } catch (ClassCastException e) {
        throw new ClassCastException(getActivity().toString()
                + " must implement OnFragmentInteractionListener");
    }
}

public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}

public void getHomeScreen(String ukmId){
    final DashboardRequest dashboardRequest = new DashboardRequest(ukmId);
    Call<DashboardResponse> call = apiService.getUkmHomescreen(dashboardRequest);
    call.enqueue(new Callback<DashboardResponse>() {
        @Override
        public void onResponse(Call<DashboardResponse> call, Response<DashboardResponse> response) {
            if(response.body().getData() != null){
                ArrayList<DashboardResponseData> dashboardResponseData = response.body().getData();

                dataList = response.body().getData();
                ukmMainDataAdapter = new UkmMainDataAdapter(dataList);
                RecyclerView.LayoutManager eLayoutManager = new LinearLayoutManager(getContext());
                dashboardChartRecycle.setLayoutManager(eLayoutManager);
                dashboardChartRecycle.setAdapter(ukmMainDataAdapter);

            }else{
                Toast.makeText(getContext(), "Failed Get Dashboard Data", Toast.LENGTH_SHORT).show();
            }

        }

        @Override
        public void onFailure(Call<DashboardResponse> call, Throwable t) {
            Toast.makeText(getContext(), "Failed Get Dashboard Data", Toast.LENGTH_SHORT).show();
            Log.e("Error Get Chart : ", String.valueOf(t));
        }
    });
  }
}

2 个答案:

答案 0 :(得分:1)

如果它只是一个简单的Fragment类,那么你需要全屏显示整个Activity。将其粘贴到Activity类:

@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            hideSystemUI();
        }
    }

    private void hideSystemUI() {
        if (hasWindowFocus()) {
            // Enables regular immersive mode.
            // For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
            // Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_IMMERSIVE
                            // Set the content to appear under the system bars so that the
                            // content doesn't resize when the system bars hide and show.
                            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            // Hide the nav bar and status bar
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN);
        }
    }

但是如果你使用DialogFragment,你可以简单地粘贴它:

@Override
    public void onStart() {
        super.onStart();

        Dialog dialog = getDialog();
        if (dialog != null && dialog.getWindow() != null) {
            dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
    }

答案 1 :(得分:0)

int flags = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_FULLSCREEN;

if (isImmersiveAvailable()) {
    flags |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
             View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
}

activity.getWindow().getDecorView().setSystemUiVisibility(flags);