0

我想在keyboard.xml中使用ViewStub

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

键盘截图

当我要打开键盘时,由于加载了这么多视图,需要一些时间才能打开。为此我使用了 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;
        }
    }
};

}

4

1 回答 1

0

这个问题解决了,现在不用用了ViewStub

我已经更换TextView而不是com.lingapps.appwriter.keyboard.internal.KeyView现在它工作得很快并且不需要时间来加载

NativeTextView 比 customView 快,所以我把所有的 key 都换成了 nativeTextView

于 2019-05-14T06:56:44.410 回答