Как показать и скрыть вид после нажатия кнопки, используя привязку данных - PullRequest
0 голосов
/ 18 марта 2019

У меня есть 2 поля редактирования, одна кнопка отправки и одно текстовое представление. Сначала пользователь заполнил поля редактирования, а затем нажал кнопку отправки.Я должен проверить, все ли поля являются действительными, если они действительны, то я хочу показать показ текста с помощью привязки данных.У меня есть проблема привязки данных в XML.я думаю, что чего-то не хватает Вот мой текстовый просмотр

<data>

    variable
        name="helper"
        type="com.abc.online.views.helper.OtpNoteBinderHelper" />
</data>
<TextView
                    android:id="@+id/idtverrorMsg"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/_3sdp"
                    android:fontFamily="@font/montserrat_regular"
                    android:gravity="center_horizontal"
                    android:letterSpacing="-0.01"
                    android:text="@string/ops_looks_like_you_apos_ve_entered_a_wrong_code"
                    android:textColor="#ff3b30"
                    android:textSize="@dimen/text_size_9"
                    app:presentation_note="false"
                    android:visibility="@{helper.is_presentation_note_visible ? View.VISIBLE : View.GONE}" />

Вот мой класс помощника

public class OtpNoteBinderHelper extends BaseObservable {
    private Boolean presentation_note;
    private Boolean timerElementsVisible;

    public OtpNoteBinderHelper(){
        this.presentation_note = true;
        this.timerElementsVisible = false;
    }
    public void setPresentationNoteViewVisible(boolean presentationElementsVisible) {
        this.presentation_note = presentationElementsVisible;

    }

    @Bindable
    public Boolean getPresentation_note() {
        return presentation_note;
    }

}

Ответы [ 2 ]

0 голосов
/ 19 марта 2019

Вы должны сделать это в MainActivity, потому что скрывать или показывать textView - это updateUI, поэтому вы не делаете этого в классе OtpNoteBinderHelper

0 голосов
/ 19 марта 2019

Это, вероятно, не лучший ответ, который вы найдете, но он выполняет свою работу ..

Поскольку мы не используем двухстороннюю привязку данных, я прослушал onClick кнопки отправки вMainActivity, где я изменяю переменную NoteBinderhelper

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

    mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);

    final NoteBinderhelper noteBinderhelper = new NoteBinderhelper();

    mainBinding.setHelper(noteBinderhelper);

    mainBinding.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String textOne = mainBinding.editText1.getText().toString();
            String textTwo = mainBinding.editText2.getText().toString();

            if(textOne.isEmpty() || textTwo.isEmpty()){
                noteBinderhelper.setPresentationNoteViewVisible(false);

            } else {
                noteBinderhelper.setPresentationNoteViewVisible(true);
            }
            mainBinding.setHelper(noteBinderhelper);
        }
    });

}

Сделано небольшое изменение в классе NoteBinderHelper

public class NoteBinderhelper extends BaseObservable {
private Boolean presentation_note;
private Boolean timerElementsVisible;

public NoteBinderhelper(){
    this.presentation_note = true;
    this.timerElementsVisible = false;
}
public void setPresentationNoteViewVisible(boolean presentationElementsVisible) {
    this.presentation_note = presentationElementsVisible;
    notifyPropertyChanged(BR.presentation_note);


 }

 @Bindable
 public Boolean getPresentation_note() {
    return presentation_note;
  } 
 }

И я изменил видимость textView на

 android:visibility="@{helper.presentation_note ? View.VISIBLE : View.GONE}"
...