packagecom.qingguow.news.fragment;importjava.util.ArrayList;importjava.util.List;importandroid.app.Activity;importandroid.os.Bundle;importandroid.support.annotation.Nullable;importandroid.support.v4.app.Fragment;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.AdapterView;importandroid.widget.AdapterView.OnItemClickListener;importandroid.widget.ArrayAdapter;importandroid.widget.ListAdapter;importandroid.widget.ListView;importcom.qingguow.news.MainActivity;importcom.qingguow.news.R;public class MenuFragment extends Fragment implementsOnItemClickListener {privateView view;//加载布局文件
@OverridepublicView onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view= inflater.inflate(R.layout.list_view, null);returnview;
}//设置listview数据
@Overridepublic voidonActivityCreated(@Nullable Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);
ListView listview=(ListView) view.findViewById(R.id.lv_menu);
List data=new ArrayList();
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);
}/*** 导航点击*/@Overridepublic void onItemClick(AdapterView> parent, View view, intposition,longid) {
Fragment fragment=null;switch(position) {case 0:
fragment=newIndexFragment();break;default:break;
}if(fragment!=null){
Activity activity=getActivity();if(activity instanceofMainActivity){
((MainActivity) activity).switchMenuFragment(fragment);
}
}
}
}