Android dataBinding-取决于布尔值的TextView不可见 [英] Android dataBinding - TextView not visible depending on a boolean value

查看:103
本文介绍了Android dataBinding-取决于布尔值的TextView不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据boolean的值设置为true的时间使TextView可见,并且根据相同的booleanfalse值使LinearLayout的可见性>使用dataBinding变量.问题在于,尽管设置了LinearLayout的可见性,但没有设置TextView的可见性,但是当我记录boolean的值时,它的状态根据控制流而改变.

I'm trying to make a TextView visible depending on when a boolean's value is set to true and a LinearLayout's visibility depending on the false value of the same boolean variable using dataBinding. The problem is that only LinearLayout's visibility is set and not of the TextView's although when I log the boolean's value it's state is changing as per the control flow.

下面是我的代码,自从昨晚以来我一直对此表示坚决,对此不胜感激,如果我是dataBinding的新手,那么如果这是一个菜鸟问题,则对不起.我在onCreateViewonActivityCreated中都设置了两次该值,只是为了测试流和日志

Below is my code any help is much appreciated as I'm stuck with this since last night and sorry if this is a noob question as I'm new to dataBinding. I'm setting the value twice in both onCreateView and onActivityCreated just to test the flow and log

片段布局

    <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
  <data>
    <variable
        name="isLoading"
        type="boolean"
        />
    <variable
        name="profileViewModel"
        type="com.example.siddhi.mvvm_login.viewmodel.ProfileViewModel"
        />
  </data>
  <FrameLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      >
    <TextView
        android:id="@+id/logging_in"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical|center_horizontal"
        android:text="@string/logging_in"
        android:textAlignment="center"
        app:visibleGone="@{isLoading}"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginEnd="@dimen/item_horizontal_margin"
        android:layout_marginStart="@dimen/item_horizontal_margin"
        android:gravity="center_vertical|center_horizontal"
        android:orientation="vertical"
        android:padding="5dp"
        android:paddingTop="@dimen/activity_vertical_margin"
        app:visibleGone="@{!isLoading}"
        >

      <ImageView
          android:id="@+id/imageView"
          android:layout_width="@dimen/logo_width"
          android:layout_height="@dimen/logo_height"
          android:src="@drawable/gfee_logo"
          />

      <TextView
          android:id="@+id/emp_pk"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:contentDescription="Emp_pk"
          android:paddingBottom="5dp"
          android:text="@{profileViewModel.emp_pk}"
          android:textAlignment="center"
          android:textSize="20sp"
          android:textStyle="bold"
          />

      <TextView
          android:id="@+id/emp_lic"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:contentDescription="Emp_lic"
          android:paddingBottom="5dp"
          android:text="@{profileViewModel.emp_lic}"
          android:textAlignment="center"
          android:textSize="20sp"
          />

    </LinearLayout>

  </FrameLayout>
</layout>

CustomBindingAdapter

public class CustomBindingAdapter {
  @BindingAdapter("visibleGone") public static void showHide(View view, boolean show) {
    view.setVisibility(show ? View.VISIBLE : View.GONE);
    Log.e("show", "" + show);
  }
}

Java片段

public class ProfileFragment extends Fragment {
  private ProfilefragmentBinding binding;

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

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    binding = DataBindingUtil.inflate(inflater, R.layout.profilefragment, container, false);
    Log.e("FirstTimeIsLoading","" + binding.getIsLoading());
    binding.setIsLoading(true);
    Log.e("SecondTimeIsLoading","" + binding.getIsLoading());
    return binding.getRoot();
  }

  @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    final ProfileViewModel viewModel = ViewModelProviders.of(this).get(ProfileViewModel.class);
    binding.setProfileViewModel(viewModel);
    Log.e("ThirdTimeIsLoading","" + binding.getIsLoading());
    binding.setIsLoading(true);
    Log.e("FourthTimeIsLoading","" + binding.getIsLoading());
    observeViewModel(viewModel);
  }

  private void observeViewModel(final ProfileViewModel viewModel) {
    viewModel.getObservableProfile().observe(this, new Observer<List<UserInfo>>() {
      @Override public void onChanged(@Nullable List<UserInfo> userInfos) {
        if (userInfos != null) {
          binding.setIsLoading(false);
          Log.e("isloadingFalse","" + binding.getIsLoading());
          viewModel.setEmp_pk(userInfos.get(0).getEmpPk());
          viewModel.setEmp_lic(userInfos.get(0).getEmpLicenceType());
        } else {
          Log.e("userInfos is null", "");
        }
      }
    });
  }
}

我的布尔值日志

E/FirstTimeIsLoading: false
E/SecondTimeIsLoading: true
I/art: Background sticky concurrent mark sweep GC freed 15486(3MB) AllocSpace objects, 0(0B) LOS objects, 28% free, 8MB/12MB, paused 11.385ms total 63.940ms
E/ThirdTimeIsLoading: true
E/FourthTimeIsLoading: true

                       [ 03-02 01:18:22.091  5874: 5874 D/         ]
                       HostConnection::get() New Host Connection established 0xd98cd400, tid 5874


                       [ 03-02 01:18:22.110  5874: 5874 W/         ]
                       Process pipe failed
E/show: true
E/show: false
E/isloadingFalse: false
E/setEmp_pk: 166
E/show: false
E/show: true

logcat 之后,将Log.e("show", "" + show + " " + view.getClass().getName());添加到CustomBindingAdapter

03/02 14:08:07: Launching app
$ adb install-multiple -r -t D:\Siddhi\Local Projects\MVVM_Login\app\build\intermediates\split-apk\debug\slices\slice_4.apk D:\Siddhi\Local Projects\MVVM_Login\app\build\intermediates\split-apk\debug\slices\slice_5.apk D:\Siddhi\Local Projects\MVVM_Login\app\build\intermediates\split-apk\debug\slices\slice_0.apk D:\Siddhi\Local Projects\MVVM_Login\app\build\intermediates\split-apk\debug\slices\slice_6.apk D:\Siddhi\Local Projects\MVVM_Login\app\build\intermediates\split-apk\debug\slices\slice_9.apk D:\Siddhi\Local Projects\MVVM_Login\app\build\intermediates\split-apk\debug\slices\slice_8.apk D:\Siddhi\Local Projects\MVVM_Login\app\build\intermediates\split-apk\debug\slices\slice_7.apk D:\Siddhi\Local Projects\MVVM_Login\app\build\intermediates\split-apk\debug\slices\slice_1.apk D:\Siddhi\Local Projects\MVVM_Login\app\build\intermediates\split-apk\debug\dep\dependencies.apk D:\Siddhi\Local Projects\MVVM_Login\app\build\intermediates\split-apk\debug\slices\slice_3.apk D:\Siddhi\Local Projects\MVVM_Login\app\build\intermediates\split-apk\debug\slices\slice_2.apk D:\Siddhi\Local Projects\MVVM_Login\app\build\outputs\apk\debug\app-debug.apk 
Split APKs installed
$ adb shell am start -n "com.example.siddhi.mvvm_login/com.example.siddhi.mvvm_login.view.ui.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Connected to process 3170 on device genymotion-google_pixel_xl___7_1_0___api_25___1440x2560-192.168.87.101:5555
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
W/System: ClassLoader referenced unknown path: /data/app/com.example.siddhi.mvvm_login-1/lib/x86
I/InstantRun: starting instant run server: is main process
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
E/FirstTimeIsLoading: false
E/SecondTimeIsLoading: true
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
E/ThirdTimeIsLoading: true
E/FourthTimeIsLoading: true

                       [ 03-02 03:45:18.242  3170: 3170 D/         ]
                       HostConnection::get() New Host Connection established 0xd6e2d140, tid 3170


                       [ 03-02 03:45:18.250  3170: 3170 W/         ]
                       Process pipe failed
E/show: true android.support.v7.widget.AppCompatTextView
E/show: false android.widget.LinearLayout
D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
E/libEGL: load_driver(/system/lib/egl/libGLES_emulation.so): dlopen failed: library "/system/lib/egl/libGLES_emulation.so" not found
D/libEGL: loaded /system/lib/egl/libEGL_emulation.so
D/libEGL: loaded /system/lib/egl/libGLESv1_CM_emulation.so
D/libEGL: loaded /system/lib/egl/libGLESv2_emulation.so

          [ 03-02 03:45:18.361  3170: 3194 D/         ]
          HostConnection::get() New Host Connection established 0xd6e2d5c0, tid 3194
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
E/EGL_emulation: tid 3194: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xd6bea500, error=EGL_BAD_MATCH
W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
E/response_data: com.example.siddhi.mvvm_login.service.model.LoginResponse@649f367
E/isloadingFalse: false
E/setEmp_pk: 166
E/show: false android.support.v7.widget.AppCompatTextView
E/show: true android.widget.LinearLayout

调试器屏幕截图

LoginRepo代码,我在将线程延迟设置为200而不是2000时犯了一个错误

    public LiveData<List<UserInfo>> getUserLiveData(String id, String password, String device_id, String token) {
    final MutableLiveData<List<UserInfo>> user = new MutableLiveData<>();
    final MutableLiveData<String> emp_pk = new MutableLiveData<>();

    gfeeLoginService.getProfileDetails(id, password,"","").enqueue(new Callback<LoginResponse>() {
      @Override public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
        simulateDelay();
        Log.e("response_data","" + response.body());
        user.setValue(response.body().getUserInfo());
        emp_pk.setValue(response.body().getUserInfo().get(0).getEmpPk());
      }

      @Override public void onFailure(Call<LoginResponse> call, Throwable t) {
        user.setValue(null);
        Log.e("Data", "is NULL! " + t);
      }
    });
    return user;
  }

  private void simulateDelay() {
    try {
      Thread.sleep(2000); //mistake of adding 200 rather than 2000
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

推荐答案

您可以在模型类中设置可观察的布尔值,并根据需要设置true和false.

You can make observable boolean in model class and set true and false as your requirement.

例如

<TextView
        android:id="@+id/logging_in"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical|center_horizontal"
        android:text="@string/logging_in"
        android:visibility="@{profileViewModel.isLoading ? View.GONE : View.VISIBLE}"/>

这篇关于Android dataBinding-取决于布尔值的TextView不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆