как использовать ViewStub в раскладке клавиатуры - PullRequest
0 голосов
/ 07 мая 2019

Я хочу использовать ViewStub в keyboard.xml

У меня есть раскладка, в которой есть клавиша на клавиатуре, см. Скриншот

keyboard screenshot

когда я собираюсь открыть клавиатуру, требуется некоторое время, чтобы открыть из-за загрузки такого количества просмотров.Для этого я использовал ViewStub, но это не сократит время, чтобы кто-то открыл клавиатуру

, помогите разобраться с этим, как я могу сократить время загрузки этих таких групп представлений

КОД

                <com.lingapps.appwriter.keyboard.internal.KeyView
                android:id="@+id/key_q"
                style="@style/keyboard_key_view_mobile"
                android:onClick="@{clickListener::onClick}"
                android:onLongClick="@{longClickListener::onLongClick}"
                custom:code="@integer/q"
                custom:layout_constraintLeft_toLeftOf="parent"
                custom:layout_constraintRight_toLeftOf="@id/key_w"
                custom:layout_constraintTop_toTopOf="parent"
                custom:title="@{isShifted?Character.toUpperCase(@integer/q):Character.toLowerCase(@integer/q)}" />

для каждого ключа, который я использовал выше, вид

и здесь я раздуваю макет

public class KeyboardView extends LinearLayout implements OnTouchListener, View.OnClickListener, View.OnLongClickListener {

private KeyboardQwertyBinding mBindingQwerty;
private KeyboardSymbolsBinding mBindingSymbol;
private KeyboardSymbolsShiftBinding mBindingSymbolShift;

private ObservableBoolean languageOtherKeyVisibility = new ObservableBoolean(false);
private ObservableBoolean isShiftedObservableBoolean = new ObservableBoolean(false);

private ObservableInt languageKeyObservable1 = new ObservableInt(0);
private ObservableInt languageKeyObservable2 = new ObservableInt(0);
private ObservableInt languageKeyObservable3 = new ObservableInt(0);


private Handler handlerDeleteButton = new Handler();
private int codeDeleteButton;
public boolean keyboardChanged = false;

private boolean previousShift;
private CandidateView mCandidateView;
private RelativeLayout rlCandidateView;

public enum KeyboardType {
    QWERTY, SYMBOLS, SYMBOLS_SHIFT
}

public enum KeyboardLocale {
    ENGLISH, DANISH, SWEDISH, ENGLISH_US, AUSTRALIAN, DUTCH, GERMAN, SPANISH, FRENCH, BOKMAL, NYNORSK
}

private KeyboardType mKeyboardType = KeyboardType.QWERTY;
private KeyboardLocale mKeyboardLocale = KeyboardLocale.DANISH;

private Keyboard keyboard;
private Context context;

public Keyboard getKeyboard() {
    return keyboard;
}

public CandidateView setKeyboard(Keyboard keyboard, boolean languageChange, EditorInfo attribute) {

    this.keyboard = keyboard;
    this.mKeyboardType = keyboard.getType();
    if (keyboard.getLocale() != null) {
        this.mKeyboardLocale = keyboard.getLocale();
    }
    keyboard.setShiftStateListener(shiftListener);
    makeKeyboard(languageChange, attribute);
    previousShift = false;

    return mCandidateView;
}

public KeyboardView(Context context) {
    super(context);
    this.context = context;
    appPreferences = new AppPreferences(context);
    setOrientation(LinearLayout.VERTICAL);
    setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}

public KeyboardView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
    setOrientation(LinearLayout.VERTICAL);
    setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}

/**
 * make keyboard for
 *
 * @param languageChange reinitialize keyboard if language change
 * @param attribute      editor info
 */
protected void makeKeyboard(boolean languageChange, EditorInfo attribute) {
    if (keyboard == null || mKeyboardType == null)
        return;

    ConstraintLayout linearLayout;

    if (mKeyboardType.equals(KeyboardType.SYMBOLS)) {
        if (mBindingSymbol == null)
            prepareKeyboardLayoutSymbol(attribute);
        linearLayout = (ConstraintLayout) mBindingSymbol.getRoot();

    } else if (mKeyboardType.equals(KeyboardType.SYMBOLS_SHIFT)) {
        if (mBindingSymbolShift == null)
            prepareKeyboardLayoutShiftSymbol(attribute);
        linearLayout = (ConstraintLayout) mBindingSymbolShift.getRoot();

    } else {
        if (languageChange && mBindingQwerty == null)
            prepareKeyboardLayoutQwerty(attribute);

        linearLayout = (ConstraintLayout) mBindingQwerty.getRoot();
    }

    if (rlCandidateView == null) {
        rlCandidateView = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.candidate_view, new RelativeLayout(context));
    }

    if (mCandidateView == null) {

        if (appWriterSoftKeyboard == null) {
            appWriterSoftKeyboard = (AppWriterSoftKeyboard) context;
        }

        mCandidateView = new CandidateView(context, rlCandidateView);
        mCandidateView.setSoftDanishKeyboard(appWriterSoftKeyboard);
    }

    removeAllViewsInLayout();
    setOrientation(VERTICAL);
    addView(rlCandidateView);
    addView(linearLayout);
    invalidate();
    requestLayout();
}

/**
 * prepare layout for qwerty keyboard
 *
 * @param attribute editor info
 */
@SuppressLint("ClickableViewAccessibility")
private void prepareKeyboardLayoutQwerty(EditorInfo attribute) {
    mBindingQwerty = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.keyboard_qwerty, null, false);
    mBindingQwerty.setLanguageOtherKeyVisibility(languageOtherKeyVisibility);
    mBindingQwerty.setIsShifted(isShiftedObservableBoolean);

    mBindingQwerty.setLanguageKey1(languageKeyObservable1);
    mBindingQwerty.setLanguageKey2(languageKeyObservable2);
    mBindingQwerty.setLanguageKey3(languageKeyObservable3);
    getKeyboardLayout();

    mBindingQwerty.setClickListener(KeyboardView.this);
    mBindingQwerty.setLongClickListener(KeyboardView.this);
    mBindingQwerty.keyBackspace.setOnTouchListener(KeyboardView.this);
    setImeOption(attribute, mBindingQwerty.keyEnter);
}

public boolean isShifted() {
    return getKeyboard().isShifted();
}

public void setShifted(boolean b) {
    if (getKeyboard() != null)
        getKeyboard().setShifted(b);
}

private ShiftStateListener shiftListener = new ShiftStateListener() {
    @Override
    public void onShiftChange(boolean isShifted) {
        if (getKeyboard() == null)
            return;
        if (previousShift != isShifted) {
            isShiftedObservableBoolean.set(isShifted);
            previousShift = isShifted;
        }
    }
};

}

1 Ответ

0 голосов
/ 14 мая 2019

Эта проблема решена, и теперь нет необходимости использовать ViewStub

Я заменил TextView вместо com.lingapps.appwriter.keyboard.internal.KeyView, теперь он работает быстро и не требует времени для загрузки

Native TextView быстрее, чем customView, поэтому я заменил весь ключ на native TextView

...