โฟกัสถัดไปที่มุมมองที่กำหนดเองภายใน RecyclerView

ฉันมี RecyclerView ที่แสดงถึงแบบฟอร์ม RecyclerView เหล่านี้ประกอบด้วย View หลายประเภท แต่ละรายการมี base_layout และ View เฉพาะเจาะจง ขึ้นอยู่กับประเภทข้อมูลที่เป็นตัวแทน (เช่น: สตริงจะแสดงด้วย EditText)

นี่คือไฟล์โครงร่างที่แสดงถึงประเภทข้อมูล string:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <include android:id="@+id/base_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/view_form_field_base"
        app:label="@{viewModel.label}"
        app:errorMessage="@{viewModel.formElement.errorMessage}"
        app:canGenerate="@{viewModel.formElement.canBeGenerated &amp;&amp; viewModel.editable}" />

    <com.my.app.views.edittext.ActionEditText
        android:id="@+id/field_content"
        android:layout_width="0dp"
        android:layout_height="wrap_content"

        android:layout_marginEnd="8dp"
        android:padding="@{viewModel.editable ? 10 : 0}"

        android:background="@{viewModel.editable ? @drawable/my_edit_text_background : @drawable/empty_drawable}"

        android:clickable="@{viewModel.editable}"
        android:cursorVisible="@{viewModel.editable}"
        android:focusableInTouchMode="@{viewModel.editable}"
        android:focusable="@{viewModel.editable}"
        android:inputType="textMultiLine|textNoSuggestions"

        android:ellipsize="end"
        android:text="@={viewModel.formElement.value}"
        app:layout_constraintBottom_toTopOf="@+id/error_message"
        app:layout_constraintEnd_toStartOf="@+id/generate_button"
        app:layout_constraintStart_toStartOf="@+id/label"
        app:layout_constraintTop_toBottomOf="@+id/label" />

</android.support.constraint.ConstraintLayout>

เมื่อแบบฟอร์มถูกสร้างขึ้นจากสตริง ไม่มีปัญหาใดๆ แต่พวกเขาก็ยังเป็นมุมมองที่กำหนดเองด้วย ตัวอย่างเช่น:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <include android:id="@+id/base_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/view_form_field_base"
        app:label="@{viewModel.label}"
        app:errorMessage="@{viewModel.formElement.errorMessage}"/>

    <com.my.app.views.dynamic_list.DynamicRadioGroup
        android:id="@+id/field_content"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="@+id/label"
        app:layout_constraintEnd_toStartOf="@+id/medical_record_level"
        app:layout_constraintBottom_toTopOf="@+id/error_message"
        app:layout_constraintTop_toBottomOf="@+id/label"

        app:setEditable="@{viewModel.editable}"
        android:layout_marginEnd="8dp" />

</android.support.constraint.ConstraintLayout>

เมื่อช่องประเภทแบบกำหนดเองตามด้านบนนำหน้าด้วยประเภทสตริง แอปพลิเคชันจะขัดข้องเมื่อเราแตะปุ่ม ถัดไป ของแป้นพิมพ์ขณะที่โฟกัสไปที่ช่องประเภทสตริง

Stacktrace ความผิดพลาด:

java.lang.IllegalStateException: focus search returned a view that wasn't able to take focus!
        at android.widget.TextView.onKeyUp(TextView.java:7520)
        at android.view.KeyEvent.dispatch(KeyEvent.java:3361)
        at android.view.View.dispatchKeyEvent(View.java:10615)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1697)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1697)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1697)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1697)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1697)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1697)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1697)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1697)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1697)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1697)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1697)
        at com.android.internal.policy.DecorView.superDispatchKeyEvent(DecorView.java:590)
        at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1885)
        at android.support.v4.view.KeyEventDispatcher.activitySuperDispatchKeyEventPre28(KeyEventDispatcher.java:130)
        at android.support.v4.view.KeyEventDispatcher.dispatchKeyEvent(KeyEventDispatcher.java:87)
        at android.support.v4.app.SupportActivity.dispatchKeyEvent(ComponentActivity.java:126)
        at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:535)
        at com.pascalwelsch.compositeandroid.activity.CompositeActivity.super_dispatchKeyEvent(CompositeActivity.java:2264)
        at com.pascalwelsch.compositeandroid.activity.ActivityDelegate.dispatchKeyEvent(ActivityDelegate.java:756)
        at com.pascalwelsch.compositeandroid.activity.CompositeActivity.dispatchKeyEvent(CompositeActivity.java:278)
        at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
        at android.support.v7.app.AppCompatDelegateImpl$AppCompatWindowCallback.dispatchKeyEvent(AppCompatDelegateImpl.java:2533)
        at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
        at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:467)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:5041)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5003)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4532)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4585)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4551)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4684)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4559)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4741)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4532)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4585)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4551)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4559)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4532)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7092)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7024)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6985)
        at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:4181)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6776)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)

สุดท้ายนี่คือคลาส Java ของ DynamicRadioGroup:

public class DynamicRadioGroup extends ViewSwitcher {
    private CharSequence[] values;
    private int defaultValuePosition;
    private int orientation;
    private String selectedValue;
    private RadioGroup radioGroup;
    private TextView checkedOption;
    private OnValueChangeListener listener;

    public DynamicRadioGroup(Context context) {
        super(context);
        init();
    }

    /**
     * Initialize the view.
     */
    private void init() {
        //The ViewSwitcher will wrap its currently displayed layout.
        this.setMeasureAllChildren(false);

        //Tried this to fix the issue.
        this.setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS);

        radioGroup = new RadioGroup(getContext());
        checkedOption = new TextView(getContext(), null, dynamicRadioGroupStyle);
        checkedOption.setGravity(Gravity.CENTER_VERTICAL);
        radioGroup.setOrientation(orientation);

        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            RadioButton radioButton = group.findViewById(checkedId);
            if (radioButton != null) {
                selectedValue = radioButton.getText().toString();
                checkedOption.setText(selectedValue);
                if (listener != null)
                    listener.onValuesChanged(this, selectedValue, checkedId);
            }
        });

        addView(radioGroup, 0);
        addView(checkedOption, 1);
    }

    //region GETTER & SETTER
    public void setValues(CharSequence[] values) {
        this.values = values;
        if(CollectionUtils.isNullOrEmpty(values)
            return;
        radioGroup.removeAllViews();
        for (int i = 0; i < this.values.length; i++) {
            RadioButton radioButton = new RadioButton(getContext());
            radioButton.setText(this.values[i]);
            radioButton.setId(i);
            if (radioGroup.getOrientation() == LinearLayout.HORIZONTAL)
                radioButton.setLayoutParams(new RadioGroup.LayoutParams(0, RadioGroup.LayoutParams.WRAP_CONTENT, 1.0f));
            radioGroup.addView(radioButton);
        }

    }

    public void setDefaultValuePosition(int defaultValuePosition) {
        this.defaultValuePosition = defaultValuePosition;
        RadioButton radioButton = (RadioButton) radioGroup.getChildAt(this.defaultValuePosition);
        if (radioButton != null) {
            radioGroup.clearCheck();
            radioButton.setChecked(true);
        }
    }

    public void setEditable(boolean editable) {
        setDisplayedChild(editable ? 0 : 1);
        setFocusable(editable);
    }

    public String getSelectedValue() {
        return selectedValue;
    }

    public void setSelectedValue(String selectedValue) {
        this.selectedValue = selectedValue;
    }

    public int getOrientation() {
        return orientation;
    }

    public void setOrientation(int orientation) {
        this.orientation = orientation;
        if (radioGroup != null)
            radioGroup.setOrientation(orientation);
    }

    public void setOnValueChangeListener(OnValueChangeListener listener) {
        this.listener = listener;
    }

    @Override
    protected boolean onRequestFocusInDescendants(final int dir, final Rect rect) {
        final View view = radioGroup.findViewById(radioGroup.getCheckedRadioButtonId());
        return view.requestFocus();
    }
    //endregion

}

อย่างที่เราเห็นฉันพยายามเล่นกับวิธี setDescendantFocusability แต่ไม่ประสบความสำเร็จ (บางทีฉันอาจทำผิด)

ฉันยังพยายามบังคับให้ตั้งค่าโฟกัสถัดไปไปที่รายการ RecyclerView ถัดไปโดยใช้ข้อมูลโค้ดนี้:

@Override
public void onBindViewHolder(BaseFormViewHolder viewHolder, int index) {
    // (omitted stuffs)
    RecyclerView.ViewHolder previousViewHolder = getRecyclerView().findViewHolderForAdapterPosition(index -1);
    if(previousViewHolder != null)
        previousViewHolder.itemView.setNextFocusDownId(viewHolder.itemView.getId());

}

ข้อมูลอีกชิ้นหนึ่งคือ ฉันใช้ Stetho เพื่อวิเคราะห์เลย์เอาต์ของฉัน และพบว่าอยู่ใต้ Accessibility Properties ของ DynamicRadioGroup ข้อความนี้:

การค้นหาโฟกัสของ Android ส่งคืนมุมมองที่ไม่สามารถโฟกัสได้!

ไม่แน่ใจว่าต้องทำอย่างไร แต่อาจช่วยได้


TL;DR

แม้ว่าฉันจะไม่ได้รับคำตอบทั้งหมดที่นี่ แต่ฉันอยากจะเข้าใจให้ดีขึ้นว่าสิ่งนี้ทำงานอย่างไรโดยการหาคำตอบสำหรับคำถามต่างๆ:

  • วิธีสร้างมุมมองแบบกำหนดเอง/ViewGroup แบบกำหนดเอง focusable และตอบสนองต่อการเพิ่ม/ลดโฟกัส
  • วิธีบังคับให้ View ภายใน RecyclerView ชี้ไปยังรายการถัดไปเมื่อพูดถึง nextFocus
  • Stacktrace ด้านบนหมายถึงอะไรจริงๆ
  • ข้อความภายในตัวตรวจสอบ Stetho หมายความว่าอย่างไรจริงๆ

ขอบคุณสำหรับการอ่าน!


person MHogge    schedule 28.11.2018    source แหล่งที่มา
comment
คำตอบนี้มีประโยชน์ stackoverflow.com/a/49433332/8547916   -  person snersesyan    schedule 06.12.2018


คำตอบ (2)


ฉันไม่เห็นสองฟังก์ชันต่อไปนี้ในตัวสร้างสำหรับมุมมองที่กำหนดเองของคุณหรือใน xml และเพื่อให้มุมมองสามารถโฟกัสได้คุณต้องการสิ่งเหล่านี้ setFocusable(true); setClickable(true);

person Navi    schedule 04.12.2018

ข้อผิดพลาดนี้เกิดขึ้นเมื่อค่าของตัวเลือก ime EditorInfo.IME_ACTION_NEXT และอ็อบเจ็กต์ถัดไปที่พบไม่สามารถโฟกัสได้หรือพาเรนต์ไม่สามารถโฟกัสได้

ตามที่กล่าวไว้ในเอกสารประกอบของ IME_ACTION_NEXT ที่นี่

บิตของ IME_MASK_ACTION: คีย์การดำเนินการดำเนินการ "ถัดไป" โดยนำผู้ใช้ไปยังฟิลด์ถัดไปที่จะยอมรับข้อความ

ดังนั้นจึงเรียกเพื่อค้นหารายการถัดไปที่ควรถ่ายโอนโฟกัสไป แต่มุมมองถัดไปนี้ไม่มีอยู่หรือไม่สามารถโฟกัสได้ หรือพาเรนต์ไม่สามารถจัดการกับการเรียก findFocus() ได้จริงและส่งคืนค่าว่าง

ดังนั้น ในกรณีของคุณ หากคลาสที่คุณกำหนดเองขยายจาก RadioGroup ซึ่ง ขยาย LinearLayout ดังนั้น คุณจะต้องทำให้มุมมองที่กำหนดเองของคุณเป็น clickable และ focusable เป็น จริง< /strong> เพื่อให้สามารถจัดการคำขอโฟกัสได้

person karan    schedule 06.12.2018