1

(特に)ActionViewでを使用すると、奇妙な問題が発生します。この問題は、SDK 3.0 より前のデバイスでのみ発生します (基本的に、私がテストしているのは Gingerbread デバイスのみです)。ActionbarActionbarSherlock

私のアプリには があり、の実行ViewPager中にメニュー項目の 1 つをアニメーション化していますAsyncTask。スワイプする前に をタッチすると、ActionViewすべて問題ありません。いずれかのビューにスワイプすると、最初のビューに戻ります。これは を持つ唯一のビューであり、アイコンActionViewをタッチするとActionView、次のようにそれ自体が 2 倍になっています。

ここに画像の説明を入力

を実装する前に、以前にこの同じ問題がありました。ViewPagerでアニメーションを停止して開始し、 を呼び出すことで修正onCreateOptionsMenuしました。それを呼び出すことで、メニューが更新され、重複が修正されることを期待していましたが、そうではありません。invalidateMenuOptionsActivityCompat.invalidateOptionsMenu(this);onResumeActivity

更新を使用getSherlock().dispatchInvalidateOptionsMenu();すると、少なくともメニューが更新されるように見えますが、それによりActionView気が狂ってしまいます。

ViewPager:

public class Main extends SherlockFragmentActivity
{
    private static List<Integer> mIds;
    private static SparseArray<Fragment> mPageReferenceMap = new SparseArray<Fragment>();

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

        setContentView(R.layout.main);

        mViewPager = (ViewPager)findViewById(R.id.viewpager);
        mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mMyFragmentPagerAdapter);

        mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                GetListingFragment().StopAnimation(); //needed to add this because the ActionView was showing on the other views when swiping
            }

            @Override
            public void onPageScrolled(int position, float offset, int offsetPixel) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mIds = new ArrayList<Integer>();

        mIds.add(0);
        mIds.add(1);
        mIds.add(2);
    }

    @Override
    public void onResume()
    { 
        super.onResume();
        ActivityCompat.invalidateOptionsMenu(this);
    }

    private ListingFragment GetKeywordsFragment()
    {       
        ListingFragment lf = (ListingFragment)getSupportFragmentManager().findFragmentById(R.id.fragmentListing);

        if (lf == null)
        {
            final MyFragmentPagerAdapter fpa = (MyFragmentPagerAdapter)mViewPager.getAdapter();
            lf = (ListingFragment)fpa.getFragment(0);
        }

        return lf;
    }

    private static class MyFragmentPagerAdapter extends FragmentStatePagerAdapter  {  

        public MyFragmentPagerAdapter(FragmentManager fm) {  
             super(fm);  
        }  

        @Override  
        public Fragment getItem(int index) {
            if (index == 0)
            {
                final ListingFragment lf = ListingFragment.newInstance();
                mPageReferenceMap.put(index, lf);
                return lf;
            }
            else
            {
                final DetailFragment df = DetailFragment.newInstance(mIds.get(index));
                mPageReferenceMap.put(index, df);
                return df;
            }
        }  

        public Fragment getFragment(int key) {
            return mPageReferenceMap.get(key);
        }

        @Override
        public int getCount() {  
             return 3;
        }
   }  
}

断片:

public class ListFragment extends SherlockListFragment
{
    private int mId;
    private MenuItem refreshItem;
    private AsyncTask<Void, String, Void> gi;

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

    public static ListingFragment newInstance(int id) {

        ListingFragment lf = new ListingFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        lf.setArguments(bundle);

        return lf;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (getArguments() != null)
            mId = getArguments().getInt("id");

        return inflater.inflate(R.layout.listing, container, false);
    }

    private class GetItems extends AsyncTask<Void, String, Void> {

        @Override
        protected void onPreExecute() 
        {
          StartAnimation();
        }

        @Override
        protected Void doInBackground(Void... unused)
        {
            //background process to get items
        }

        protected void onPostExecute(final Void unused)
        {
            StopAnimation();
        }
    }

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

        StopAnimation();

        refreshItem = menu.findItem(R.id.refresh);

         if (fi != null && fi.getStatus() == AsyncTask.Status.RUNNING)
            StartAnimation();

        super.onCreateOptionsMenu(menu, inflater);
    }

    private void StartAnimation() {

        if (refreshItem != null && refreshItem.getActionView() == null)
        {
           final LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           final ImageView ivRefresh = (ImageView)inflater.inflate(R.layout.refresh_view, null);

           final Animation rotation = AnimationUtils.loadAnimation(activity, R.anim.refresh);
           ivRefresh.startAnimation(rotation);

           refreshItem.setActionView(ivRefresh);
        }
    }

    public void StopAnimation()
    {
        if (refreshItem != null && refreshItem.getActionView() != null) 
        {
            refreshItem.getActionView().clearAnimation();           
            refreshItem.setActionView(null);
        }
    }

    @Override
    public boolean onOptionsItemSelected(final MenuItem item) 
    {
        if (item.getItemId() == R.id.getitems) {
            gi = new GetItems(getActivity(), null);

            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }
}
4

1 に答える 1

0

これは ABS の既知のバグのようです。

https://github.com/JakeWharton/ActionBarSherlock/issues/331

于 2013-03-03T15:43:14.493 に答える