您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關怎么在Android中使用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://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<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 copy public 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); } }
關于怎么在Android中使用fragment實現一個左側導航就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。