并排的两个ViewPager片段不会聚焦EditText

时间:2016-06-21 06:17:25

标签: android android-fragments android-edittext android-viewpager

我已经实现了一个ViewPager,每个页面上都有不同的片段。在纵向模式下,ViewPager显示单个片段。在横向模式下,ViewPager并排显示两个片段。我通过在ViewPagerAdapter中使用getPageWidth()覆盖来实现这一点。像这样:

@Override
public float getPageWidth(int position) {
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    if ((metrics.widthPixels / metrics.density) > 900) {
        return (0.5f);
    }
    return super.getPageWidth(position);
}

在这两个片段中,我都有EditText字段。

当处于横向模式并且两个片段并排放置时,我可以单击并将焦点放在左侧片段的EditText上。当我尝试单击右侧片段上的EditText时,我无法在日志中收到以下警告:

  

W / IInputConnectionWrapper:getTextBeforeCursor on inactive InputConnection

MainActivity.java

public class MainActivity extends AppCompatActivity /*implements PageLoader*/ {
    MyPagerAdapter adapter;
    DirectionalViewPager pager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the ViewPager and set it's PagerAdapter so that it can display items
        pager = (DirectionalViewPager) findViewById(R.id.vpPager);
        adapter = new MyPagerAdapter(getSupportFragmentManager());
        pager.setOffscreenPageLimit(8);
        pager.setAdapter(adapter);
    }

    public class MyPagerAdapter extends SmartFragmentStatePagerAdapter {
        private static final int NUM_ITEMS = 4;

        public MyPagerAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        // Returns total number of pages
        @Override
        public int getCount() {
            return NUM_ITEMS;
        }

        // Returns the fragment to display for that page
        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0: // Fragment # 0 - This will show Frag1
                    return Frag1.newInstance(position, Frag1.class.getSimpleName());
                case 1: // Fragment # 0 - This will show Frag1 different title
                    return Frag1.newInstance(position, Frag1.class.getSimpleName());
                case 2: // Fragment # 1 - This will show Frag2
                    return Frag2.newInstance(position, Frag2.class.getSimpleName());
                default:
                    return Frag3.newInstance(position, Frag3.class.getSimpleName());
            }
        }

        // Returns the page title for the top indicator
        @Override
        public CharSequence getPageTitle(int position) {
            return "Page " + position;

        }

        @Override
        public float getPageWidth(int position) {
            DisplayMetrics metrics = getResources().getDisplayMetrics();
            if ((metrics.widthPixels / metrics.density) > 900) {
                return (0.5f);
            }
            return super.getPageWidth(position);
        }
    }
}

Frag1.java Frag2.java Frag3.java

public class Frag1 extends Fragment {
    // newInstance constructor for creating fragment with arguments
    public static Frag1 newInstance(int page, String title) {
        Frag1 fragmentFirst = new Frag1();
        Bundle args = new Bundle();
        args.putInt("someInt", page);
        args.putString("someTitle", title);
        fragmentFirst.setArguments(args);
        return fragmentFirst;
    }
    // Inflate the view for the fragment based on layout XML
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag1, container, false);
        return view;
    }
}

frag1.xml frag2.xml frag3.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#cc2">

    <TextView
        android:id="@+id/txt_frag1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="txt_frag1"
        />
    <Button
        android:id="@+id/btn_frag1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="btn_frag1"
        android:textSize="26dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/et_frag1"
        android:ems="10"
        android:text="ET_FRAG1"/>

</LinearLayout>

为什么会发生这种情况?如何绕过它?

1 个答案:

答案 0 :(得分:2)

经过无数个小时搜索互联网后,我发现这个SO answer解释了这个问题本来就是一个ViewPager问题。因此,真正修复它的唯一方法是修改ViewPager源本身。

void populate(int newCurrentItem) {

    // ... CODES OMITTED

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

public boolean infoIsOnScreen(ItemInfo info) {
    float curItemOffset = mCurItem * info.widthFactor;  // find the offset value for curr item
    float offsetUpperBound = curItemOffset + 1;         // +1 to get the upper bound offset value
    if ((info.offset >= curItemOffset) && (info.offset < offsetUpperBound))
        return true; // curr item offset value is within bound, hence it is on the screen
    return false;
}

如果你觉得这很有用,请向上投票或其他SO答案,因为由于这个问题不受欢迎而很难将其挖掘出来。虽然我不明白为什么会考虑ViewPager本身很受欢迎,所以人们以前肯定会使用EditText(也许只有不少人在同一个屏幕上的多个页面中使用它们)