1

我在Actionbar(特別是ActionbarSherlock)中使用ActionView有一個奇怪的問題。這個問題只發生在SDK 3.0以下的設備上(基本上薑餅設備就是我測試的)。Actionbar在Gingerbread中保留菜單項

我的應用程序有ViewPager,我正在運行一個AsyncTask而我的一個菜單項正在運行。如果我在滑動之前觸摸了ActionView,則一切正常,如果我滑動到任何視圖,則返回第一個視圖,這是唯一擁有ActionView的視圖,然後觸摸ActionView圖標自身翻倍,像這樣:

enter image description here

我以前有同樣的問題,我實現了一個ViewPager之前,我固定它通過停止和onCreateOptionsMenu啓動動畫,並呼籲invalidateMenuOptions,這就是爲什麼我在onResumeActivityCompat.invalidateOptionsMenu(this);Activity。我希望通過調用它,菜單會刷新自己並修復重複,但事實並非如此。

UPDATE使用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); 
     } 
    } 
} 

回答