Android 带有ViewPager和setCurrentItem的FragmentPagerAdapter-奇怪的行为或已知的错误?

Android 带有ViewPager和setCurrentItem的FragmentPagerAdapter-奇怪的行为或已知的错误?,android,android-layout,android-fragments,android-viewpager,Android,Android Layout,Android Fragments,Android Viewpager,我有一个非常简单的ViewPager和FragmentPagerAdapter设置奇怪的行为 首先这里是源代码。我在一个适配器中有3个片段。以及对应活动中所有按钮的一个回调函数 这是片段0 public class Fragment0 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceStat

我有一个非常简单的ViewPager和FragmentPagerAdapter设置奇怪的行为

首先这里是源代码。我在一个适配器中有3个片段。以及对应活动中所有按钮的一个回调函数

这是片段0

public class Fragment0 extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment0, container, false);
    }
}
<LinearLayout xmlns:android=        "http://schemas.android.com/apk/res/android"
              android:orientation=  "vertical"
              android:layout_width= "match_parent"
              android:layout_height="match_parent">

    <Button     android:id=                 "@+id/pager_button_prev0"
                android:layout_width=       "wrap_content"
                android:layout_height=      "wrap_content"
                android:text=               "1"
                android:tag=                "page0"
                android:onClick=            "pagerTestClick"/>

    <Button     android:id=                 "@+id/pager_button_next0"
                android:layout_width=       "wrap_content"
                android:layout_height=      "wrap_content"
                android:text=               "2"
                android:tag=                "page0"
                android:onClick=            "pagerTestClick"/>
</LinearLayout>
以及碎片0的xml布局

public class Fragment0 extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment0, container, false);
    }
}
<LinearLayout xmlns:android=        "http://schemas.android.com/apk/res/android"
              android:orientation=  "vertical"
              android:layout_width= "match_parent"
              android:layout_height="match_parent">

    <Button     android:id=                 "@+id/pager_button_prev0"
                android:layout_width=       "wrap_content"
                android:layout_height=      "wrap_content"
                android:text=               "1"
                android:tag=                "page0"
                android:onClick=            "pagerTestClick"/>

    <Button     android:id=                 "@+id/pager_button_next0"
                android:layout_width=       "wrap_content"
                android:layout_height=      "wrap_content"
                android:text=               "2"
                android:tag=                "page0"
                android:onClick=            "pagerTestClick"/>
</LinearLayout>
与相应的布局

<LinearLayout xmlns:android=            "http://schemas.android.com/apk/res/android"
              android:orientation=      "vertical"
              android:layout_width=     "match_parent"
              android:layout_height=    "match_parent">

    <Button     android:id=                 "@+id/pager_button_prev1"
                android:layout_width=       "wrap_content"
                android:layout_height=      "wrap_content"
                android:text=               "2"
                android:tag=                "page1"
                android:onClick=            "pagerTestClick"/>

    <Button     android:id=                 "@+id/pager_button_next1"
                android:layout_width=       "wrap_content"
                android:layout_height=      "wrap_content"
                android:text=               "0"
                android:tag=                "page1"
                android:onClick=            "pagerTestClick"/>
</LinearLayout>
这里还有简单的布局:

<LinearLayout xmlns:android=        "http://schemas.android.com/apk/res/android"
              android:orientation=  "horizontal"
              android:layout_width= "match_parent"
              android:layout_height="match_parent">

    <Button     android:id=                 "@+id/pager_button_prev2"
                android:layout_width=       "wrap_content"
                android:layout_height=      "wrap_content"
                android:text=               "0"
                android:tag=                "page2"
                android:onClick=            "pagerTestClick"/>

    <Button     android:id=                 "@+id/pager_button_next2"
                android:layout_width=       "wrap_content"
                android:layout_height=      "wrap_content"
                android:text=               "1"
                android:tag=                "page2"
                android:onClick=            "pagerTestClick"/>
</LinearLayout>
所以现在问题来了

当我点击3页时。首先,一切似乎都很好。但在某一点上,有些事情会变得一团糟。即使我在page1上,单击按钮0进入page0,page2上的按钮似乎调用了函数pagerTestClick,因为标记字符串是page2

我做错什么了吗?或者这是一个已知的错误

附加问题

如果我用刷子扫过页面,所有页面上的东西都可以正常工作,例如,如果我在每个页面上添加一些额外的按钮,但是当我使用setCurrentItem并手动选择页面时,似乎可单击区域不是页面的全部大小。这意味着,如果我在页面中添加一些额外的ui按钮,其中的一些按钮是可点击的。 如果我切换到page0,然后切换到pageX,那么一切都正常

**整个问题是否与setCurrentItem有关?在调用setCurrentItem之后,是否需要调用任何特殊的内容

更奇怪的行为


我发现了一种更奇怪的行为。当我通过setCurrentItem点击页面时,在某个点上,我似乎可以点击当前页面并点击之前显示的页面上的按钮。这意味着,当我在第2页上,用setCurrentItem单击第1页时,我似乎仍然可以单击第2页的元素。在这种情况下,我在每个页面上添加了一些分布广泛的虚拟按钮。最好的方法是在每个片段中都有一个回调接口,让您的活动实现它们。如果你这样做是为了学习,那么,我也想学习。那么。。。我试试看。。这里有一个很好的谷歌例子,但我想这对我来说是一个过度的杀伤力。我将尝试它,并给出一些反馈,以及回调接口的相同行为。我更新了我的问题,因为我发现了一些更奇怪的行为,但我想它们都与setCurrentItem函数有关。好吧,我创建了一个bug。
public class Fragment_Pager_Adapter extends android.support.v4.app.FragmentPagerAdapter
{
List<Fragment> fragments = new ArrayList<Fragment>();

/**
 * default constructor
 */
public Fragment_Pager_Adapter(FragmentManager fm)
{
    super(fm);
    Fragment0 fragment0 =  new Fragment0();
    this.fragments.add(fragment0);
    Fragment1 fragment1 =  new Fragment1();
    this.fragments.add(fragment1);
    Fragment2 fragment2 =  new Fragment2();
    this.fragments.add(fragment2);
}

@Override
public Fragment getItem(int position)
{
    if (position >= this.getCount())
    {
        throw new AssertionError();
    }
    return this.fragments.get(position);
}

@Override
public int getCount()
{
    return this.fragments.size();
}
public void pagerTestClick(View view)
{
    int page = Integer.parseInt(((Button) view).getText().toString());
    String tag = view.getTag().toString();
    this.viewPager.setCurrentItem(page);
}