ViewPager 和 ActionBar (Sherlock) 的奇怪错误/行为 [英] Strange bug / behaviours with ViewPager and ActionBar (Sherlock)

查看:15
本文介绍了ViewPager 和 ActionBar (Sherlock) 的奇怪错误/行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下错误将发生在 2.3 设备上,我的设置在 4.x 设备上运行良好.

The following bug will happen on an 2.3 device, my setup works fine on 4.x devices.

我有一个 ViewPager,里面有一些 Fragment(它们都是同一个类).每个 Fragment 都会膨胀它自己的菜单,因为菜单项可能因 Fragment 而异.

I have a ViewPager with some Fragments in it (they're all of the same class). Every Fragment inflates it's own Menu, because the Menu Items may vary from Fragment to Fragment.

出于测试目的,我在 ActionBar 中设置了一个菜单项(ActionBar 显示在图片的底部,因为它是一个拆分的 ActionBar).当 Item 被点击时,Fragment 中的 TextView 应设置为点击".这在开始时有效,但在轻弹一下后,就会发生这种情况:

For test purposes, I have set up a Menu Item in the ActionBar (the ActionBar is shown on the bottom in the pic because it's a split ActionBar). When the Item is tapped, a TextView in the Fragment should be set to "clicked". This works in the beginning, but after flicking around a bit, this happens:

点击菜单项时,什么也没有发生.相反,只要我滑动到下一个 Fragment,下一个 Fragment 就会将其 TextView 设置为点击".似乎 ActionBar 及其菜单与下一个 Fragment 相关联.

When the Menu Item is tapped, nothing happens. Instead, as soon as I swipe to the next Fragment, the next Fragment sets its TextView to "clicked". It seems like the ActionBar and it's Menu are associated with the next Fragment.

这是一张照片

还有一些代码:

我的活动:

public class MyActivity extends SherlockFragmentActivity implements
    MyFragment.InvalidateListener {

ViewPager viewPager;
SectionsPagerAdapter pagerAdapter;


public void invalidate() {
    ActivityCompat.invalidateOptionsMenu(act);
}

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

    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);



    pagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setAdapter(pagerAdapter);
    viewPager.setCurrentItem(initialIndex);


}


public class SectionsPagerAdapter extends FragmentPagerAdapter {
    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        Fragment fragment = new MyFragment();
        fragment.setHasOptionsMenu(true);


        return fragment;
    }

    // ...
}

我的片段:

public class MyFragment extends SherlockFragment {

HashSet<ImageView> runningImageTasks = new HashSet<ImageView>();


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_expose, null);

}




@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_grundstueckexpose, menu);

    // ...

}

@Override
public boolean onOptionsItemSelected(MenuItem mitem) {

    switch (mitem.getItemId()) {

    case android.R.id.home:
        getActivity().finish();
        return true;

    case R.id.myitem:

        textView.setText("clicked"); 

        return true;

    default:
        return super.onOptionsItemSelected(mitem);
    }
}





}

有没有其他人遇到过这样的事情,或者对这里可能出现的问题有什么想法?

Has anyone else experienced something like this or has an idea on what could be the problem here?

推荐答案

问题是 MotionEvent 没有被内部类 ActionMenuItemView 正确处理(实际上,有此类中没有任何特定行为).

The problem is that the MotionEvent does not handled correctly by internal class ActionMenuItemView (actually, there is no any specific behavior in this class).

所以,我没有解决最初的问题,但我找到了解决方法.我只是覆盖 ActionMenuItemView.dispatchTouchEvent() 并使用 GestureDetector 手动处理单击和长按.

So, I do not resolve initial problem, but I find workaround solution. I just override ActionMenuItemView.dispatchTouchEvent() and handle click and long-click manually using GestureDetector.

您可以在 github 上查看此解决方案.

You can check this solution on github.

这篇关于ViewPager 和 ActionBar (Sherlock) 的奇怪错误/行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆