2016-01-03 46 views
-1

我正在使用導航抽屜,最近我更改了build.gradle來編譯API 23的應用程序。 我注意到onAttach方法已被棄用。我在這個論壇中發現了幾個解決方案,Android Fragment onAttach() deprecated但他們都沒有工作,您有其他解決方案嗎?謝謝Android NavigationDrawer onAttach已棄用

public class NavigationDrawerFragment extends Fragment implements NavigationDrawerCallbacks { 
private NavigationDrawerCallbacks mCallbacks; 
    private RecyclerView mDrawerList; 
    private View mFragmentContainerView; 
    private DrawerLayout mDrawerLayout; 
    private ActionBarDrawerToggle mActionBarDrawerToggle; 
    private boolean mUserLearnedDrawer; 
    private boolean mFromSavedInstanceState; 
    private int mCurrentSelectedPosition; 
@Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_navigation_drawer, container, false); 
mDrawerList = (RecyclerView) view.findViewById(R.id.drawerList); 
     LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); 
     layoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
     mDrawerList.setLayoutManager(layoutManager); 
     mDrawerList.setHasFixedSize(true); 

     final List<NavigationItem> navigationItems = getMenu(); 
     NavigationDrawerAdapter adapter = new NavigationDrawerAdapter(navigationItems); 
     adapter.setNavigationDrawerCallbacks(this); 
     mDrawerList.setAdapter(adapter); 
     selectItem(mCurrentSelectedPosition); 


     return view; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mUserLearnedDrawer = Boolean.valueOf(readSharedSetting(getActivity(), PREF_USER_LEARNED_DRAWER, "false")); 
     if (savedInstanceState != null) { 
      mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION); 
      mFromSavedInstanceState = true; 
     } 
    } 


    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     try { 
      mCallbacks = (NavigationDrawerCallbacks) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException("Activity must implement NavigationDrawerCallbacks."); 
     } 
    } 

    public ActionBarDrawerToggle getActionBarDrawerToggle() { 
     return mActionBarDrawerToggle; 
    } 

    public void setActionBarDrawerToggle(ActionBarDrawerToggle actionBarDrawerToggle) { 
     mActionBarDrawerToggle = actionBarDrawerToggle; 
    } 

    public void setup(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar) { 
     mFragmentContainerView = getActivity().findViewById(fragmentId); 
     mDrawerLayout = drawerLayout; 
     mActionBarDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, toolbar, R.string.app_name, R.string.app_name) { 
      @Override 
      public void onDrawerClosed(View drawerView) { 
       super.onDrawerClosed(drawerView); 
       if (!isAdded()) return; 
       getActivity().invalidateOptionsMenu(); 
      } 

      @Override 
      public void onDrawerOpened(View drawerView) { 
       super.onDrawerOpened(drawerView); 
       if (!isAdded()) return; 
       if (!mUserLearnedDrawer) { 
        mUserLearnedDrawer = true; 
        saveSharedSetting(getActivity(), PREF_USER_LEARNED_DRAWER, "true"); 
       } 

       getActivity().invalidateOptionsMenu(); 
      } 
     }; 

     if (!mUserLearnedDrawer && !mFromSavedInstanceState) 
      mDrawerLayout.openDrawer(mFragmentContainerView); 

     mDrawerLayout.post(new Runnable() { 
      @Override 
      public void run() { 
       mActionBarDrawerToggle.syncState(); 
      } 
     }); 

     mDrawerLayout.setDrawerListener(mActionBarDrawerToggle); 
    } 

    public void openDrawer() { 
     mDrawerLayout.openDrawer(mFragmentContainerView); 
    } 

    public void closeDrawer() { 
     mDrawerLayout.closeDrawer(mFragmentContainerView); 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mCallbacks = null; 
    } 

    public List<NavigationItem> getMenu() { 
     List<NavigationItem> items = new ArrayList<NavigationItem>(); 
     items.add(new NavigationItem(getString(R.string.nav1), ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_home_black_24dp, null))); 
     items.add(new NavigationItem(getString(R.string.nav2), ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_content_paste_black_24dp, null))); 
     items.add(new NavigationItem(getString(R.string.nav3), ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_format_list_bulleted_black_24dp, null))); 
     items.add(new NavigationItem(getString(R.string.nav4), ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_receipt_black_24dp, null))); 
     items.add(new NavigationItem(getString(R.string.nav5), ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_trending_down_black_24dp, null))); 
     items.add(new NavigationItem(getString(R.string.nav6), ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_donut_large_black_24dp, null))); 
     items.add(new NavigationItem(getString(R.string.nav7), ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_equalizer_black_24dp, null))); 
     items.add(new NavigationItem(getString(R.string.nav8), ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_search_black_24dp, null))); 
     items.add(new NavigationItem(getString(R.string.nav9), ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_settings_black_24dp, null))); 

     return items; 
    } 

    void selectItem(int position) { 
     mCurrentSelectedPosition = position; 
     if (mDrawerLayout != null) { 
      mDrawerLayout.closeDrawer(mFragmentContainerView); 
     } 
     if (mCallbacks != null) { 
      mCallbacks.onNavigationDrawerItemSelected(position); 
     } 
     ((NavigationDrawerAdapter) mDrawerList.getAdapter()).selectPosition(position); 
    } 

    public boolean isDrawerOpen() { 
     return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     mActionBarDrawerToggle.onConfigurationChanged(newConfig); 
    } 

    @Override 
    public void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition); 
    } 

    @Override 
    public void onNavigationDrawerItemSelected(int position) { 
     selectItem(position); 
    } 

    public DrawerLayout getDrawerLayout() { 
     return mDrawerLayout; 
    } 

    public void setDrawerLayout(DrawerLayout drawerLayout) { 
     mDrawerLayout = drawerLayout; 
    } 

    public static void saveSharedSetting(Context ctx, String settingName, String settingValue) { 
     SharedPreferences sharedPref = ctx.getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE); 
     SharedPreferences.Editor editor = sharedPref.edit(); 
     editor.putString(settingName, settingValue); 
     editor.apply(); 
    } 

    public static String readSharedSetting(Context ctx, String settingName, String defaultValue) { 
     SharedPreferences sharedPref = ctx.getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE); 
     return sharedPref.getString(settingName, defaultValue); 
    } 
+0

由於API 23的新方法'onAttach(上下文)'中引入的。這是前「onAttach(Activity)」的概括。覆蓋其中一個,就完成了。 –

回答

1

不知道您找到的解決方案有什麼問題。但onAttach應該接受一個Context。一個活動是一個上下文,所以下面的工作。

@Override 
public void onAttach(Context context) { 
super.onAttach(context); 
try { 
    mCallbacks = (NavigationDrawerCallbacks) context; 
} catch (ClassCastException e) { 
    throw new ClassCastException("Activity must implement NavigationDrawerCallbacks."); 
} 

}

+0

所以,就像論壇的解決方案一樣,沒有片段受到攻擊。 – user2847219

+0

我不確定我瞭解你的問題。 onAttach(Activity)是一種折舊方法。該解決方案解決了折舊方法的問題。我的問題是,使用onAttach(Context)的錯誤或問題是什麼? –

+0

如果我使用onAttach(Context)片段不被Attaccked。 – user2847219