重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章将为大家详细讲解有关怎么在Android中使用fragment实现一个左侧导航,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
创新互联建站是一家专业提供信宜企业网站建设,专注与成都网站设计、成都网站制作、外贸网站建设、H5技术、小程序制作等业务。10年已为信宜众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。
MainActivity.java
public class MainActivity extends FragmentActivity { // private Handler handler; private ViewPager fragmentViewPager; private ListView leftListView; private Listfragmentlist; private Fragment1 fragment1; private Fragment2 fragment2; private ArrayList leftlist = new ArrayList ();// 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://www.easyicon.net/"; // 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 buttonListView = new ArrayList (); 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 (); 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 arrayList = null; LayoutInflater inflater; View view; ButtonLayoutHolder buttonLayoutHolder; LinearLayout buttonLayout = null; TextView buttonText = null; private int selectedPosition = -1;// 选中的位置 public ListAdapter(ArrayList 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 copy public class FragmentAdapter extends FragmentPagerAdapter{ private List listmVp; private Context context; public FragmentAdapter(FragmentManager fm, List 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); } }
关于怎么在Android中使用fragment实现一个左侧导航就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。