Strange bug / behaviours with ViewPager and ActionBar (Sherlock)

守給你的承諾、 提交于 2020-03-22 07:50:23

问题


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

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.

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:

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.

Heres a pic

And heres some code:

My Activity:

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

    // ...
}

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


回答1:


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

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.

You can check this solution on github.




回答2:


I don't know this exact problem, but I had a problem when flipping the device. The app was crashing. Finally, I've found that was problem of the Pager classes, because I was implementing them like you've implemented your SectionsPagerAdapter class.

I put the public classes that were on the main class on separated classes and the layouts worked well in vertical and horizontal position.

I don't know if this could be the problem, but you could try to create the corresponding classes instead of leaving them on the main class. Anyway, you pass to the SectionsPagerAdapter the FragmentManager, so you will not have any extra problem putting the public classes in their respective files.

Good luck!



来源:https://stackoverflow.com/questions/16591690/strange-bug-behaviours-with-viewpager-and-actionbar-sherlock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!