如何在键盘布局中使用 ViewStub

how to use ViewStub in keyboard layout

我想在keyboard.xml

中使用ViewStub

我有一个带有键盘键的布局,请看截图

当我要打开键盘时,由于加载了这么多视图,需要一些时间才能打开。 为此,我使用了 ViewStub,但它不会减少打开键盘的时间

哪位大神帮忙解决一下,怎么才能减少这一堆view的加载时间呢

代码

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

}

此问题已解决,现在无需使用ViewStub

我已经替换了 TextView 而不是 com.lingapps.appwriter.keyboard.internal.KeyView 现在它运行速度很快并且不需要时间来加载

Native TextView 比 customView 快,所以我用 native TextView

替换了所有 key