如何在键盘布局中使用ViewStub

时间:2019-05-07 06:48:00

标签: android performance keyboard viewstub

我想在keyboard.xml中使用ViewStub

我的布局带有键盘按键,请参见屏幕截图

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 :(得分:0)

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

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

本地TextView比customView快,所以我用本地TextView替换了所有键