Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

c# - ViewPager Focus Issue

I have a ViewPager that holds 4 fragments/panels, each half-width of a landscaped screen.

 _______________ _______________ _______________ _______________
|1              |2              |3              |4              |
|               |               |               |               |
|               |               |  ___________  |  ___________  |
|               |               | | EditText1 | | | EditText2 | |
|               |               | |___________| | |___________| |
|_______________|_______________|_______________|_______________|

Panels 3 and 4 each have a single EditText.

At program start, the user is show screens 2 & 3 (they can swipe back to see screen 1, and screen 4 does not yet exist until a list item on 3 is selected).

So, the view hosting the panels (and associated ViewPager) makes a call to

viewPager.SetCurrentItem(PANEL3,true);   // const int PANEL3 = 2;  - zero offset

This causes 2 & 3 to be displayed, and gives 3 the focus (which gives EditText1 focus).

When the user selects something on 3 that causes 4 to slide in (so that now 3 & 4 are showing), another call is made in the host view

viewPager.SetCurrentItem(PANEL4,true);    // const int PANEL4 = 3;  - zero offset

so that now panel 4 gets the focus (which gives EditText2 the focus).


Here is the issue:

With panels 3 & 4 showing, where EditText2 has focus, if I then click into EditText1, which causes the softkeyboard to popup, focus will revert back to EditText2 (the cursor is in EditText2 and any typing gets entered into it).

I need to know what is causing focus to revert back to EditText2.

I'm speculating that the call to viewPager.SetContentView(PANEL4) is causing the panel focus state to be retained in the ViewPager somewhere, and when the softkeyboard pops up and causes some "refresh" events to fire, the ViewPager is restoring that focus state (which is PANEL4), though I'm not certain. I made a lot of printouts within the host view and each of the panels, and the softkeyboard popup doesn't cause any of the standard lifetime events to be fired, however I do know that the viewPager's OnLayout does fire.


Any help or insight is welcome, thank you.


Cheesebaron:

This is supportive information to help illustrate an inflation issue that happens if one of the views in my solution isn't wrapped in a ViewGroup (LinearLayout) within its associated layout file.

If PagedFragmentRecordNoteBoxInput is not wrapped in a LinearLayout, we have

<?xml version="1.0" encoding="utf-8"?>
<FieldInspection.Droid.Views.Custom.PagedFragmentRecordNoteBox.PagedFragmentRecordNoteBoxInput
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fi_record_note_box_input"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFFFF"
    android:gravity="top"
    android:textSize="20sp"
    android:textColor="#FF000000"
    android:layout_margin="20dip"/>

and the inflation code in PagedFragmentRecordNoteBox changes to

protected override RecordNoteBoxInput InstantiateInput()
{
    View v = _inflater.Inflate(Resource.Layout.PagedFragmentRecordNoteBoxInput, null);

    _pagedFragmentFieldInput = (PagedFragmentRecordNoteBoxInput) v;

    _pagedFragmentFieldInput.OuterClass = this;

    return (RecordNoteBoxInput)_pagedFragmentFieldInput;
}

Which then produces the exception:

07-02 16:21:32.494: I/MonoDroid(18642): UNHANDLED EXCEPTION: Android.Views.InflateException: Exception of type 'Android.Views.InflateException' was thrown.
07-02 16:21:32.494: I/MonoDroid(18642): at Android.Runtime.JNIEnv.CallObjectMethod (intptr,intptr,Android.Runtime.JValue[]) <0x00080>
07-02 16:21:32.494: I/MonoDroid(18642): at Android.Views.LayoutInflater.Inflate (int,Android.Views.ViewGroup) <0x0018f>
07-02 16:21:32.494: I/MonoDroid(18642): at FieldInspection.Droid.Views.Custom.PagedFragmentRecordNoteBox.InstantiateInput () <0x0002b>
07-02 16:21:32.494: I/MonoDroid(18642): at FieldInspection.Droid.Views.Custom.RecordNoteBox.Init (Android.Content.Context) <0x00063>
07-02 16:21:32.494: I/MonoDroid(18642): at FieldInspection.Droid.Views.Custom.RecordNoteBox..ctor (Android.Content.Context,Android.Util.IAttributeSet) <0x0002f>
07-02 16:21:32.494: I/MonoDroid(18642): at FieldInspection.Droid.Views.Custom.PagedFragmentRecordNoteBox..ctor (Android.Content.Context,Android.Util.IAttributeSet) <0x00023>
07-02 16:21:32.494: I/MonoDroid(18642): at (wrapper dynamic-method) object.705e129e-2c0d-42db-87f7-db1842b85d7c (intptr,object[]) <0x0005f>
07-02 16:21:32.494: I/MonoDroid(18642): at Java.Interop.TypeManager.n_Activate (intptr,intptr,intptr,intptr,intptr,intptr) <0x000f7>
07-02 16:21:32.494: I/MonoDroid(18642): at (wrapper native-to-managed) Java.Interop.TypeManager.n_Activate (intptr,intptr,intptr,intptr,intptr,intptr) <0x0006f>
07-02 16:21:32.494: I/MonoDroid(18642): at (wrapper delegate-invoke) <Module>.invoke_intptr__this___intptr_intptr_intptr_JValue[] (intptr,intptr,intptr,Android.Runtime.JValue[]) <0x000ab>
07-02 16:21:32.494: I/MonoDroid(18642): at Android.Runtime.JNIEnv.CallObjectMethod (intptr,intptr,Android.Runtime.JValue[]) <0x00053>
07-02 16:21:32.494: I/MonoDroid(18642): at Android.Views.LayoutInflater.Inflate (int,Android.Views.ViewGroup,bool) <0x001f3>
07-02 16:21:32.494: I/MonoDroid(18642): at FieldInspection.Droid.Views.ParcelRecordDetailInspectionView.Render () <0x0002f>
07-02 16:21:32.494: I/MonoDroid(18642): at FieldInspection.Droid.Views.ParcelRecordDetailInspectionView.OnCreateView (Android.Views.LayoutInflater,Android.Views.ViewGroup,Android.OS.Bundle) <0x000af>
07-02 16:21:32.494: I/MonoDroid(18642): at Android.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_ (intptr,intptr,intptr,intptr,intptr) <0x0009f>
07-02 16:21:32.494: I/MonoDroid(18642): at (wrapper dynamic-method) object.5d994140-166d-44ce-ac16-71f1e1698262 (intptr,intptr,intptr,intptr,intptr) <0x0005b>
07-02 16:21:32.494: I/MonoDroid(18642):   --- End of managed exception stack trace ---
07-02 16:21:32.494: I/MonoDroid(18642): android.view.InflateException: Binary XML file line #5: Error inflating class FieldInspection.Droid.Views.Custom.PagedFragmentRecordNoteBox.PagedFragmentRecordNoteBoxInput
07-02 16:21:32.494: I/MonoDroid(18642):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
07-02 16:21:32.494: I/MonoDroid(18642):     at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
07-02 16:21:32.494: I/MonoDroid(18642):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-02 16:21:32.494: I/MonoDroid(18642):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-02 16:21:32.494: I/MonoDroid(18642):     at mono.android.TypeManager.n_activate(Native Method)
07-02 16:21:32.494: I/MonoDroid(18642):     at mono.android.TypeManager.Activate(TypeManager.java:7)
07-02 16:21:32.494: I/MonoDroid(18642):     at fieldinspection.droid.views.custom.PagedFragmentRecordNoteBox.<init>(PagedFragmentRecordNoteBox.java:29)
07-02 16:21:32.494: I/MonoDroid(18642):     at java.lang.reflect.Constructor.constructNative(Native Method)
07-02 16:21:32.494: I/MonoDroid(18642):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
07-02 16:21:32.494: I/MonoDroid(18642):     at android.view.LayoutInflater.createView(LayoutInflater.java:586)
07-02 16:21:32.494: I/MonoDroid(18642):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
07-02 16:21:32.494: I/MonoDroid(18642):     at android.view.LayoutInflater.parseInclude(LayoutInflater.java:800)
07-02 16:21:32.494: I/MonoDroid(18642):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:729)
07-02 16:21:32.494: I/MonoDroid(18642):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
07-02 16:21:32.494: I/MonoDroid(18642):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
07-02 16:21:32.494: I/MonoDroid(18642):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
07-02 16:21:32.494: I/MonoDroid(18642):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-02 16:21:32.494: I/MonoDroid(18642):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-02 16:21:32.494: I/MonoDroid(18642):     at fieldinspection.droid.views.ParcelRecordDetailInspectionView.n_onCreateView(Native Method)
07-02 16:21:32.494: I/MonoDroid(18642):     at fieldinspection.droid.views.ParcelRecordDetailInspectionView.onCreateView(ParcelRecordDetailInspectionView.java:52)
07-02 16:21:32.494: I/MonoDroid(18642):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:828)
07-02 16:21:32.494: I/MonoDroid(18642):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
07-02 16:21:32.494: I/MonoDroid(18642):     at android.app.BackStackRecord.run(BackStackRecord.java:622)
07-02 16:21:32.494: I/MonoDroid(18642):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
07-02 16:21:32.494: I/MonoDroid(18642):     at android.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:437)
07-02 16:21:32.494: I/MonoDroid(18642):     at android.support.v13.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:167)
07-02 16:21:32.494: I/MonoDroid(18642):     at android.supp
07-02 16:21:32.504: E/mono(18642): Unhandled Exception:
07-02 16:21:32.504: E/mono(18642): Android.Views.InflateException: Exception of type 'Android.Views.InflateException' was thrown.
07-02 16:21:32.504: E/mono(18642): at Android.Runtime.JNIEnv.CallObjectMethod (intptr,intptr,Android.Runtime.JValue[]) <0x00080>
07-02 16:21:32.504: E/mono(18642): at Android.Views.LayoutInflater.Inflate (int,Android.Views.ViewGroup) <0x0018f>
07-02 16:21:32.504: E/mono(18642): at FieldInspection.Droid.Views.Custom.PagedFragmentRecordNoteBox.InstantiateInput () <0x0002b>
07-02 16:21:32.504: E/mono(18642): at FieldInspection.Droid.Views.Custom.RecordNoteBox.Init (Android.Content.Context) <0x00063>
07-02 16:21:32.504: E/mono(18642): at FieldInspection.Droid.Views.Custom.RecordNoteBox..ctor (Android.Content.Context,Android.Util.IAttributeSet) <0x0002f>
07-02 16:21:32.504: E/mono(18642): at FieldInspection.Droid.Views.Custom.PagedFragmentRecordNoteBox..ctor (Android.Content.Context,Android.Util.IAttributeSet) <0x00023>
07-02 16:21:32.504: E/mono(18642): at (wrapper dynamic-method) object.705e129e-2c0d-42db-87f7-db1842b85d7c (intptr,object[]) <0x0005f>
07-02 16:21:32.504: E/mono(18642): at Java.Interop.TypeManager.n_Activate (intptr,intptr,intptr,intptr,intptr,intptr) <0x000f7>
07-02 16:21:32.504: E/mono(18642): at (wrapper native-to-managed) Java.Interop.TypeManager.n_Activate (intptr,intptr,intptr,intptr,
07-02 16:21:32.504: I/mono(18642): [ERROR] FATAL UNHANDLED EXCEPTION: Android.Views.InflateException: Exception of type 'Android.Views.InflateException' was thrown.
07-02 16:21:32.504: I/mono(18642): at Android.Runtime.JNIEnv.CallObjectMethod (intptr,intptr,Android.Runtime.JValue[]) <0x00080>
07-02 16:21:32.504: I/mono(18642): at Android.Views.LayoutInflater.Inflate (int,Android.Views.ViewGroup) <0x0018f>
07-02 16:21:32.504: I/mono(18642): at FieldInspection.Droid.Views.Custom.PagedFragmentRecordNoteBox.InstantiateInput ()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

My answer lies within this stack trace, which is from the OnFocusChange() override of the EditText subclass EditText2 mentioned above (good times tracing this):

Note: This is just for archival purposes only, to potentially help any future readers with this issue. I will not be accepting this as my answer and am still open for help (Thanks again).


dalvik.system.VMStack.getThreadStackTrace(Native Method) java.lang.Thread.getStackTrace(Thread.java:591) appname.droid.views.EditText2.n_onFocusChanged(Native Method) appname.droid.views.EditText2.onFocusChanged(EditText2.java:53) android.view.View.handleFocusGainInternal(View.java:3680) android.view.View.requestFocus(View.java:5373) android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2154) android.view.ViewGroup.requestFocus(ViewGroup.java:2110) android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2154) android.view.ViewGroup.requestFocus(ViewGroup.java:2110) android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2154) android.view.ViewGroup.requestFocus(ViewGroup.java:2110) android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2154) android.view.ViewGroup.requestFocus(ViewGroup.java:2113) android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2154) android.view.ViewGroup.requestFocus(ViewGroup.java:2110) android.view.View.requestFocus(View.java:5323) android.support.v4.view.ViewPager.populate(ViewPager.java:1051) android.support.v4.view.ViewPager.populate(ViewPager.java:881) android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1366) android.view.View.measure(View.java:12728) android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369) android.widget.LinearLayout.measureVertical(LinearLayout.java:660) android.widget.LinearLayout.onMeasure(LinearLayout.java:553) android.view.View.measure(View.java:12728) android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) android.widget.FrameLayout.onMeasure(FrameLayout.java:293) android.view.View.measure(View.java:12728) android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369) android.widget.LinearLayout.measureVertical(LinearLayout.java:660) android.widget.LinearLayout.onMeasure(LinearLayout.java:553) android.view.View.measure(View.java:12728) android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) android.widget.FrameLayout.onMeasure(FrameLayout.java:293) com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092) android.view.View.measure(View.java:12728) android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064) android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442) android.os.Handler.dispatchMessage(Handler.java:99) android.os.Looper.loop(Looper.java:137) android.app.ActivityThread.main(ActivityThread.java:4424) java.lang.reflect.Method.invokeNative(Native Method) java.lang.reflect.Method.invoke(Method.java:511) com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) dalvik.system.NativeStart.main(Native Method)

public class ViewPager extends ViewGroup 
{
    ...

    void populate 
    {
        ...

        if (hasFocus()) {
            View currentFocused = findFocus();
            ItemInfo ii = currentFocused != null ? infoForAnyChild(currentFocused) : null;
            if (ii == null || ii.position != mCurItem) {
                for (int i=0; i<getChildCount(); i++) {
                View child = getChildAt(i);
                ii = infoForChild(child);
                if (ii != null && ii.position == mCurItem) {
                    if (child.requestFocus(FOCUS_FORWARD)) {
                        break;
                    }
                }
            }
        }

        ...
    }

    ...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...