gpt4 book ai didi

android - 接下来关注RecyclerView里面的自定义view

转载 作者:行者123 更新时间:2023-11-29 02:23:39 25 4
gpt4 key购买 nike

我有代表表单的 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>

当表单仅由字符串组成时,它们没有问题。但他们也是一些自定义 View 。例如:

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

当像上面这样的自定义类型字段前面有字符串类型时,当我们在聚焦字符串类型字段时点击键盘的下一步 按钮时应用程序崩溃。

崩溃堆栈跟踪:

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)

最后是 DynamicRadioGroup 的 java 类:

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 方法但没有成功(也许我做错了)?

我还尝试使用以下代码 fragment 强制将下一个焦点设置到下一个 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 分析了我的布局,我在 DynamicRadioGroupAccessibility Properties 下发现了这条消息:

android focus search returned a view that wasn't able to take focus!

不知道该怎么办,但它可能会有所帮助。


长话短说

即使我在这里没有得到完整的答案,我也想通过获得不同问题的答案来更好地理解它是如何工作的:

  • 如何使自定义 View /自定义 View 组可聚焦并对获得/失去焦点使用react。
  • 在谈论 nextFocus 时,如何强制 RecyclerView 中的 View 指向下一个项目。
  • 上面的堆栈跟踪究竟意味着什么?
  • Stetho 检查器中的消息到底是什么意思?

感谢阅读!

最佳答案

当 ime 选项 EditorInfo.IME_ACTION_NEXT 的值和找到的下一个对象不可聚焦或其父对象不可聚焦时,会发生此错误。

IME_ACTION_NEXT 文档中所述here

Bits of IME_MASK_ACTION: the action key performs a "next" operation, taking the user to the next field that will accept text.

因此它调用查找下一个应将焦点转移到的项目,但此下一个 View 不存在或不可聚焦,或者父级实际上无法处理 findFocus() 调用并返回 null。

因此,在您的情况下,如果您的自定义类是从 RadioGroup 扩展的,而 扩展了 LinearLayout,那么您需要将自定义 View 设置为可点击focusabletrue 以便它可以处理焦点请求。

关于android - 接下来关注RecyclerView里面的自定义view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53522796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com