Android使用fragment实现左侧导航

时间:2021-05-20

使用简单的fragment实现左侧导航,供大家参考,具体内容如下

先上效果图:

MainActivity.java

public class MainActivity extends FragmentActivity { // private Handler handler; private ViewPager fragmentViewPager; private ListView leftListView; private List<Fragment> fragmentlist; private Fragment1 fragment1; private Fragment2 fragment2; private ArrayList<String> leftlist = new ArrayList<String>();// left list private ListAdapter listAdapter = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fragmentViewPager = (ViewPager) findViewById(R.id.fragmentViewPager); leftListView = (ListView) findViewById(R.id.leftListView); initDatas();// left初始化。 initDatasViewPager(); // aTextViewCallBack back = (aTextViewCallBack) // findViewById(R.id.aTextViewCallBack); // back.setCalBack(new calBack() { // @Override // public void onclick() { // // TODO Auto-generated method stub // Toast.makeText(MainActivity.this, "这是回调的点击事件哦~", // Toast.LENGTH_LONG).show(); // } // }); // // new Thread(new Runnable() { // // @Override // public void run() { // // TODO Auto-generated method stub // String str = "http:///"; // String st = getHtmlByURL.getHtmlByURL(str); // Message msg = handler.obtainMessage(0,st); // handler.sendMessage(msg); // } // }).start(); // handler = new Handler() { // public void handleMessage(Message msg) { // super.handleMessage(msg); // String x = msg.obj.toString(); // Log.i("GEtHtml", x); // }; // }; } public void initDatas() { for (int i = 0; i < 5; i++) { leftlist.add("第" + i + "个"); Log.i("65", leftlist.get(i).toString()); } ArrayList<ButtonView> buttonListView = new ArrayList<ButtonView>(); ButtonView a = new ButtonView(R.string.pay_name_weixin); buttonListView.add(a); ButtonView b = new ButtonView(R.string.pay_name_alipay); buttonListView.add(b); listAdapter = new ListAdapter(buttonListView); leftListView.setAdapter(listAdapter); leftListView.setDividerHeight(0); // listview点击事件 leftListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Log.e("tag", Integer.toString(position)); // TODO Auto-generated method stub listAdapter.setSelectedPosition(position); listAdapter.notifyDataSetInvalidated(); if (position % 2 == 1) { fragmentViewPager.setCurrentItem(1); }else{ fragmentViewPager.setCurrentItem(0); } } }); } public void initDatasViewPager() { fragmentlist = new ArrayList<Fragment>(); fragment1 = new Fragment1();// fragment2 = new Fragment2();// fragmentlist.add(fragment1); fragmentlist.add(fragment2); fragmentViewPager.setAdapter(new FragmentAdapter( getSupportFragmentManager(), fragmentlist, this)); fragmentViewPager.setOnPageChangeListener(new MyOnPageChangeListener()); } // set OnPageChangeListener in inner class class MyOnPageChangeListener implements OnPageChangeListener { @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } /** * 当点击不同id的ViewPage的时候才触发 * */ @Override public void onPageSelected(int arg0) { Log.e("tag1", Integer.toString(arg0)); switch (arg0) { case 0: break; case 1: break; case 2: break; } } } public class ListAdapter extends BaseAdapter { ArrayList<ButtonView> arrayList = null; LayoutInflater inflater; View view; ButtonLayoutHolder buttonLayoutHolder; LinearLayout buttonLayout = null; TextView buttonText = null; private int selectedPosition = -1;// 选中的位置 public ListAdapter(ArrayList<ButtonView> buttonListView) { // TODO Auto-generated constructor stub arrayList = buttonListView; } @Override public int getCount() { // TODO Auto-generated method stub return arrayList.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return arrayList.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } public void setSelectedPosition(int position) { selectedPosition = position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.button_layout, null, false); buttonLayoutHolder = (ButtonLayoutHolder) view.getTag(); if (buttonLayoutHolder == null) { buttonLayoutHolder = new ButtonLayoutHolder(); buttonLayoutHolder.buttonLayout = (LinearLayout) view .findViewById(R.id.LinearLayoutButton); buttonLayoutHolder.textView = (TextView) view .findViewById(R.id.TextViewButton); view.setTag(buttonLayoutHolder); } buttonLayout = buttonLayoutHolder.buttonLayout; buttonText = buttonLayoutHolder.textView; if (selectedPosition == position) { buttonText.setSelected(true); buttonText.setPressed(true); buttonLayout.setBackgroundColor(Color.parseColor("#e4e8e9")); buttonText.setTextColor(Color.BLUE); } else { buttonText.setSelected(false); buttonText.setPressed(false); buttonLayout.setBackgroundColor(Color.parseColor("#2f4471")); buttonText.setTextColor(Color.WHITE); } buttonText.setHeight(40); buttonText.setText(arrayList.get(position).textViewId); return view; } }; } class ButtonView { int textViewId; ButtonView(int tId) { textViewId = tId; } } class ButtonLayoutHolder { LinearLayout buttonLayout; TextView textView; } FragmentAdapter.java[java] view plain copypublic class FragmentAdapter extends FragmentPagerAdapter{ private List<Fragment> listmVp; private Context context; public FragmentAdapter(FragmentManager fm, List<Fragment> listmVp,Context context) { super(fm); this.listmVp = listmVp; this.context = context; } @Override public int getCount() { // TODO Auto-generated method stub return listmVp.size(); } @Override public Fragment getItem(int arg0) { // TODO Auto-generated method stub return listmVp.get(arg0); } }

Fragment1.java

public class Fragment1 extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.fragment1, container, false); } }

Fragment2.java

public class Fragment2 extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.fragment2, container, false); } }

源码地址:使用fragment实现左侧导航

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章