android - 当某些视图的可见性状态从 View.VISIBLE 更改为 View.GONE 并返回到 View.VISIBLE 时,整个布局会消失
问题描述
当最后调用 View.VISIBLE 时,整个布局消失。
这在我将项目升级到 androidx 后开始发生。我仅在牛轧糖版本的 android 上面临这个问题。
我正在更改 id 为“@+id/recyclerview_container”的 ContraintLayout 和 id 为“@+id/done_button”的按钮的可见性状态
我在下面分享了我的 XML 文件的内容。
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context="com.ts.marham_connect.fragments.extendedprofile.AreaOfInterestFragment">
<ScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="@dimen/zero"
app:layout_constraintBottom_toTopOf="@+id/done_button"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/recyclerview_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/fifteen">
<TextView
android:id="@+id/areas_of_interest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/thirty"
android:layout_marginTop="@dimen/twenty"
android:text="Areas of Interest"
android:textColor="@color/grey_1"
android:textSize="@dimen/twenty_text"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/sub_heading_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/thirty"
android:layout_marginTop="@dimen/ten"
android:text="Add areas of your interest to attract patients(Only 4)"
android:textColor="@color/text_color"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/areas_of_interest" />
<TextView
android:id="@+id/add_your_interest_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/thirty"
android:layout_marginTop="@dimen/thirty"
android:text="Add New Interest"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/seventeen_text_size"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/sub_heading_textview" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/specialities_recyclerview"
android:layout_width="@dimen/zero"
android:layout_height="250dp"
android:layout_marginStart="@dimen/thirty"
android:layout_marginTop="@dimen/five"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/add_your_interest_text_view" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
<MyButton
android:id="@+id/done_button"
android:layout_width="110dp"
android:layout_height="@dimen/confirm_button_height"
android:layout_marginStart="@dimen/thirty"
android:layout_marginBottom="@dimen/thirty"
android:background="@drawable/draw_body_green_button_circular_corners"
android:drawableRight="@drawable/vector_icon_arrow_right_white_2"
android:gravity="left|center_vertical"
android:paddingStart="@dimen/fifteen"
android:paddingEnd="@dimen/fifteen"
android:text="@string/next"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />
<include
android:id="@+id/central_retry_button_container"
layout="@layout/layout_central_retry_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<include
android:id="@+id/central_progress_bar_container"
layout="@layout/layout_progress_bar"
android:layout_width="@dimen/fifty"
android:layout_height="@dimen/fifty"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
package com.ts.marham_connect.fragments.extendedprofile;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class AreaOfInterestFragment extends BaseFragment implements View.OnClickListener, ServerConnectListener, AdapterViewItemClickedListener {
private ConstraintLayout recyclerViewContainer;
private TextView addYourInterestTextView;
private RecyclerView specialitiesRecyclerView;
private View centralProgreeBar;
private View centralRetryButton;
private MyButton doneButton;
private AlertWindow alertWindow;
private SpecialitiesAdapter specialitiesAdapter;
private List<Speciality> specialityList = new ArrayList<>();
private FragmentInteractionListener fragmentInteractionListener;
private Context context;
public AreaOfInterestFragment() {
}
public static AreaOfInterestFragment newInstance() {
return new AreaOfInterestFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_area_of_interest, container, false);
initViews(view);
setListeners();
getSpecialities();
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.retry_button_container:
getSpecialities();
break;
case R.id.add_your_interest_text_view:
if (isSelectedListSizeWithInMaxSize(4)) {
dialog.show();
} else {
alertWindow = new AlertWindow(context, getString(R.string.msg_when_user_tries_to_select_more_than_allowed));
alertWindow.show();
}
break;
case R.id.done_button:
if (isSelectedListSizeWithInValidRange(4)) {
submitSpecialitiesList();
} else {
alertWindow = new AlertWindow(context, getString(R.string.msg_when_user_does_not_any_area_of_interest));
alertWindow.show();
}
break;
}
}
@Override
public void onAdatviewItemClicked(int position) {
if (specialityList.get(position).getChecked().equals("1")) {
specialityList.get(position).setChecked("0");
specialitiesAdapter.notifyItemChanged(position);
} else {
if (isSelectedListSizeWithInMaxSize(4)) {
specialityList.get(position).setChecked("1");
specialitiesAdapter.notifyItemChanged(position);
} else {
alertWindow = new AlertWindow(context, getString(R.string.msg_when_user_tries_to_select_more_than_allowed));
alertWindow.show();
}
}
}
@Override
public void onAdatviewItemClicked(int position, int requestID) {
}
private void initViews(View view) {
recyclerViewContainer = view.findViewById(R.id.recyclerview_container);
addYourInterestTextView = view.findViewById(R.id.add_your_interest_text_view);
specialitiesRecyclerView = view.findViewById(R.id.specialities_recyclerview);
centralProgreeBar = view.findViewById(R.id.central_progress_bar_container);
centralRetryButton = view.findViewById(R.id.central_retry_button_container);
doneButton = view.findViewById(R.id.done_button);
setUpSpecialitiesRecyclerView();
setUpAlertDialog();
}
private void setListeners() {
centralRetryButton.setOnClickListener(this);
addYourInterestTextView.setOnClickListener(this);
doneButton.setOnClickListener(this);
}
public void setFragmentInteractionListener(FragmentInteractionListener fragmentInteractionListener) {
this.fragmentInteractionListener = fragmentInteractionListener;
}
private void setUpSpecialitiesRecyclerView() {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
specialitiesRecyclerView.setLayoutManager(linearLayoutManager);
specialitiesAdapter = new SpecialitiesAdapter(specialityList, this);
specialitiesRecyclerView.setAdapter(specialitiesAdapter);
}
private void upDateProfileCompletionObject() {
ProfileCompletion profileCompletion = (ProfileCompletion) SharedPreferenceHelper.getInstance().getSharedPreferenceObject(context, AppConstants.PROFILE_COMPLETION_OBJECT, ProfileCompletion.class);
profileCompletion.setAreasOfInterest(context.getString(R.string.one));
SharedPreferenceHelper.getInstance().setSharedPreferenceObject(context, AppConstants.PROFILE_COMPLETION_OBJECT, profileCompletion);
}
private void setViewsBeforeGettingSpecialities() {
recyclerViewContainer.setVisibility(View.GONE);
centralProgreeBar.setVisibility(View.VISIBLE);
centralRetryButton.setVisibility(View.GONE);
doneButton.setVisibility(View.GONE);
}
private void setViewsAfterGettingSpecialities() {
recyclerViewContainer.setVisibility(View.VISIBLE);
centralProgreeBar.setVisibility(View.GONE);
centralRetryButton.setVisibility(View.GONE);
doneButton.setVisibility(View.VISIBLE);
}
private void setViewsIncaseOfInternetFailureOrUnExpectedResponseFromServer() {
recyclerViewContainer.setVisibility(View.GONE);
centralProgreeBar.setVisibility(View.GONE);
centralRetryButton.setVisibility(View.VISIBLE);
doneButton.setVisibility(View.GONE);
}
private void setViewsBeforeSubmittingSpecialities() {
recyclerViewContainer.setVisibility(View.GONE);
doneButton.setVisibility(View.GONE);
centralProgreeBar.setVisibility(View.VISIBLE);
}
private void setViewsAfterSubmittingSpecialities() {
recyclerViewContainer.setVisibility(View.VISIBLE);
doneButton.setVisibility(View.VISIBLE);
centralProgreeBar.setVisibility(View.GONE);
}
private void setViewsIncaseOfInternetFailureOrUnExpectedResponseFromServerWhileSubmittingSpecialities() {
recyclerViewContainer.setVisibility(View.VISIBLE);
doneButton.setVisibility(View.VISIBLE);
centralProgreeBar.setVisibility(View.GONE);
}
private boolean isSelectedListSizeWithInMaxSize(int minListSize) {
int selectedItemCount = 0;
for (int i = 0; i < specialityList.size(); i++) {
if (specialityList.get(i).getChecked().equals("1")) {
selectedItemCount = selectedItemCount + 1;
}
}
if (selectedItemCount <= minListSize) {
return true;
} else {
return false;
}
}
private boolean isSelectedListSizeWithInValidRange(int minListSize) {
int selectedItemCount = 0;
for (int i = 0; i < specialityList.size(); i++) {
if (specialityList.get(i).getChecked().equals("1")) {
selectedItemCount = selectedItemCount + 1;
}
}
if (selectedItemCount > 0 && selectedItemCount <= minListSize) {
return true;
} else {
return false;
}
}
private void getSpecialities() {
setViewsBeforeGettingSpecialities();
HashMap<String, String> info = new HashMap<>();
info.put(getString(R.string.doctor_id_key), SharedPreferenceHelper.getInstance().getSharedPreferenceString(context, AppConstants.DOC_ID_KEY, null));
info.put(getString(R.string.speciality_id_key), SharedPreferenceHelper.getInstance().getSharedPreferenceString(context, AppConstants.SPECIALITY_ID_KEY, null));
info.put(getString(R.string.sub_speciality_id_key), SharedPreferenceHelper.getInstance().getSharedPreferenceString(context, AppConstants.SUB_SPECIALITY_ID_KEY, "0"));
Call<GetSpecialitiesServerResponse> call = APIClient.getInstance().getApiService().getSpecialities(info);
retroFit2Callback = new RetroFit2Callback<>(this, context, AppConstants.REQUEST_CODE_GET_SPECIALITIES);
call.enqueue(retroFit2Callback);
}
private void submitSpecialitiesList() {
setViewsBeforeSubmittingSpecialities();
SpecialitiesListContainer specialitiesListContainer = new SpecialitiesListContainer();
specialitiesListContainer.setdId(SharedPreferenceHelper.getInstance().getSharedPreferenceString(context, AppConstants.DOC_ID_KEY, null));
specialitiesListContainer.setAreas(specialityList);
Call<ServerResponse> call = APIClient.getInstance().getApiService().submitSpecialitiesList(specialitiesListContainer);
retroFit2Callback = new RetroFit2Callback<>(this, context, AppConstants.REQUEST_CODE_SUBMIT_SPECIALITIES);
call.enqueue(retroFit2Callback);
}
@Override
public void onSuccess(ServerResponse response) {
switch (response.getRequestCode()) {
case AppConstants.REQUEST_CODE_GET_SPECIALITIES:
setViewsAfterGettingSpecialities();
if (response.getReturn_status().equals(AppConstants.SUCCESS)) {
GetSpecialitiesServerResponse getSpecialitiesServerResponse = (GetSpecialitiesServerResponse) response;
specialityList.addAll(getSpecialitiesServerResponse.getData());
specialitiesAdapter.notifyDataSetChanged();
specialitiesRecyclerView.scrollToPosition(0);
} else {
Toast.makeText(context, response.getMessage(), Toast.LENGTH_LONG).show();
}
break;
case AppConstants.REQUEST_CODE_SUBMIT_SPECIALITIES:
setViewsAfterSubmittingSpecialities();
if (response.getReturn_status().equals(AppConstants.SUCCESS)) {
upDateProfileCompletionObject();
fragmentInteractionListener.onFramentRequest(AppConstants.FragmentInteractionRequest.CHANGE_PAGE);
} else {
Toast.makeText(context, response.getMessage(), Toast.LENGTH_LONG).show();
}
break;
}
}
@Override
public void onFailure(ServerResponse response) {
Toast.makeText(context, response.getMessage(), Toast.LENGTH_LONG).show();
switch (response.getRequestCode()) {
case AppConstants.REQUEST_CODE_GET_SPECIALITIES:
setViewsIncaseOfInternetFailureOrUnExpectedResponseFromServer();
break;
case AppConstants.REQUEST_CODE_SUBMIT_SPECIALITIES:
setViewsIncaseOfInternetFailureOrUnExpectedResponseFromServerWhileSubmittingSpecialities();
break;
}
}
@Override
public void onSessionExpiry(ServerResponse response) {
Toast.makeText(context, response.getMessage(), Toast.LENGTH_LONG).show();
switch (response.getRequestCode()) {
case AppConstants.REQUEST_CODE_GET_SPECIALITIES:
setViewsIncaseOfInternetFailureOrUnExpectedResponseFromServer();
break;
case AppConstants.REQUEST_CODE_SUBMIT_SPECIALITIES:
setViewsIncaseOfInternetFailureOrUnExpectedResponseFromServerWhileSubmittingSpecialities();
break;
}
}
// ADD SPECIALITY POP UP
private Dialog dialog;
private EditText specialityEditText;
private MyButton addButton;
private MyButton cancelButton;
private void setUpAlertDialog() {
dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.alert_window_add_speciality);
dialog.findViewById(R.id.add_speciality_edittext);
specialityEditText = dialog.findViewById(R.id.add_speciality_edittext);
addButton = dialog.findViewById(R.id.add_button);
cancelButton = dialog.findViewById(R.id.cancel_button);
addButton.setOnClickListener(onClickListener);
cancelButton.setOnClickListener(onClickListener);
}
private View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.add_button:
if (validateUserEnteredData()) {
dialog.dismiss();
}
break;
case R.id.cancel_button:
dialog.dismiss();
break;
}
}
};
private boolean validateUserEnteredData() {
boolean allFieldsValid = true;
if (!Utils.getInstance().isEmpty(specialityEditText)) {
if (specialityList != null) {
for (int i = 0; i < specialityList.size(); i++) {
if (specialityList.get(i).getTitle().equals(specialityEditText.getText().toString())) {
allFieldsValid = false;
}
}
}
if (allFieldsValid) {
addNewSpeciality();
}
} else {
specialityEditText.setError(getString(R.string.invalid_value));
allFieldsValid = false;
}
return allFieldsValid;
}
private void addNewSpeciality() {
Speciality speciality = new Speciality();
speciality.title = specialityEditText.getText().toString().trim();
speciality.setChecked(getString(R.string.one));
speciality.setId("0");
specialityList.add(0, speciality);
specialitiesAdapter.notifyDataSetChanged();
specialityEditText.setText(null);
}
}
解决方案
推荐阅读
- javascript - 获取服务器节点失败 [CANNOT_MEET_AREA_DEMAND] https://webrtc2-ap-web-2.agoraio.cn/api/v1
- ios - iOS 14 UITextField on touch crash *** Assertion failure in -[UIKBCacheToken stringForConstruction:], UIKBCacheToken.m:204
- c# - 找不到 Linq to SQL 类
- javascript - 将 useState setter 函数作为回调传递
- ms-access-2016 - 将集合从 VBA 传递到表单
- android - 如何将 EditText 添加到 RadioButton?
- node.js - 使用 pino api 将来自不同模块的日志添加到文件中
- javascript - Webpack 意外行为 TypeError:无法读取未定义的属性“调用”
- java - 使用共享首选项在按钮单击时保存数据
- mongodb - 在会话 Meteor 中存储变量