設置主佈局文件,為根佈局設置一個id,作為內容區 給ListView的條目設置點擊事件,setOnItemClickListener()方法,參數:上下文 當前的Fragment實現OnItemClickListener介面 實現onItemClick()方法 switch判斷一下當前菜單的索引位置 ...
設置主佈局文件,為根佈局設置一個id,作為內容區
給ListView的條目設置點擊事件,setOnItemClickListener()方法,參數:上下文
當前的Fragment實現OnItemClickListener介面
實現onItemClick()方法
switch判斷一下當前菜單的索引位置,根據索引來實例化不同的Fragment對象
調用getActivity()方法,得到宿主Activity對象,這個activity暴露一個方法,替換fragment
使用關鍵字instances of判斷getActivity()得到的是MainActivity類的實例
調用宿主Activity對象的switchMenuFragment()方法,切換內容部分的fragment,參數:Fragment對象
宿主activity暴露方法switchMenuFragment(),切換內容區fragment,參數:Fragment對象
getSupportFragmentManager().beginTransaction().replace(R.id.ll_main_content, fragment).commit();
調用SlidingMenu對象的toggle()方法,關閉左側菜單
設置預設,當app第一次打開時顯示導航第一個的內容
MainActivity
package com.qingguow.news; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.view.Window; import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu; import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity; import com.qingguow.news.fragment.IndexFragment; import com.qingguow.news.fragment.MenuFragment; public class MainActivity extends SlidingFragmentActivity { private SlidingMenu sm; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); //設置滑動菜單 setBehindContentView(R.layout.menu); setContentView(R.layout.activity_main); sm=getSlidingMenu(); sm.setMode(SlidingMenu.LEFT); sm.setBehindWidth(200); sm.setShadowDrawable(R.drawable.shape); sm.setShadowWidth(5); sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); FragmentTransaction ft=getSupportFragmentManager().beginTransaction(); //替換導航fragemnt MenuFragment menuFragment=new MenuFragment(); ft.replace(R.id.ll_left_menu, menuFragment); //替換內容fragment IndexFragment index=new IndexFragment(); ft.replace(R.id.ll_main_content, index).commit(); } /** * 暴露方法切換內容區 * @param fragment */ public void switchMenuFragment(Fragment fragment){ getSupportFragmentManager().beginTransaction().replace(R.id.ll_main_content, fragment).commit(); sm.toggle(); } }
MenuFragment.java
package com.qingguow.news.fragment; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListAdapter; import android.widget.ListView; import com.qingguow.news.MainActivity; import com.qingguow.news.R; public class MenuFragment extends Fragment implements OnItemClickListener { private View view; //載入佈局文件 @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.list_view, null); return view; } //設置listview數據 @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); ListView listview = (ListView) view.findViewById(R.id.lv_menu); List<String> data=new ArrayList<String>(); data.add("新聞"); data.add("發現"); data.add("關註"); ListAdapter adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, data); listview.setAdapter(adapter); listview.setOnItemClickListener(this); } /** * 導航點擊 */ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Fragment fragment=null; switch (position) { case 0: fragment=new IndexFragment(); break; default: break; } if(fragment!=null){ Activity activity=getActivity(); if(activity instanceof MainActivity){ ((MainActivity) activity).switchMenuFragment(fragment); } } } }