【问题标题】:Android dataBinding - TextView not visible depending on a boolean valueAndroid dataBinding - TextView 不可见,具体取决于布尔值
【发布时间】:2018-08-10 08:43:39
【问题描述】:

我正在尝试使 TextView 可见,具体取决于 boolean 的值何时设置为 trueLinearLayout 的可见性取决于同一 @987654328 的 false 值@变量使用dataBinding。问题是只设置了LinearLayout 的可见性,而不是TextView 的可见性,尽管当我记录boolean 的值时,它的状态正在根据控制流发生变化。

以下是我的代码,非常感谢任何帮助,因为我从昨晚开始就一直坚持这一点,如果这是一个菜鸟问题,我很抱歉,因为我是 dataBinding 的新手。我在onCreateViewonActivityCreated 中设置了两次值,只是为了测试流程和日志

片段布局

    <?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 afterLog.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

调试器截图

我错误地将线程延迟设置为 200 而不是 2000 的 LoginRepo 代码

    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();
    }
  }

【问题讨论】:

  • 它给了我以下结果:class 'android.support.v7.widget.AppCompatTextView' class 'android.widget.LinearLayout'
  • 是的,这是我的 logcat 输出
  • 因为我使用 Log.e 来记录而不是 Log.d,这是一个肮脏的习惯,我需要改掉 D 表示调试,E 表示错误
  • @pskink 你能建议任何解决方案吗?
  • 如果您在添加我建议的内容后不想发布您的 logcat 怎么办?只需尝试:Log.e("show", "" + show + " " + view.getClass().getName()); 并发布整个 logcat 输出

标签: java android android-layout android-fragments android-databinding


【解决方案1】:

您可以在模型类中设置 observable 布尔值,并根据您的要求设置 true 和 false。

例如

<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}"/>

【讨论】:

    猜你喜欢
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多