标签:
使用场景,打算设计一个“底部菜单栏+其余可滑动的页面”的简单的功能。
package com.lanyuweng.mibaby; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.view.ViewPager; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.CalendarView; import android.widget.TextView; import com.lanyuweng.mibaby.fragment.FragmentAdapter; public class MainActivity extends FragmentActivity implements OnClickListener{ private static final String TAG = "MainActivity-----"; public static final int TAB_CALENDAR = 0; public static final int TAB_REMINDER = 1; public static final int TAB_NOTE = 2; public static final int TAB_MORE = 3; private CalendarView calendarView; private ViewPager viewPager; private TextView tvCalendar,tvReminder,tvNote,tvMore; //定义适配器 private FragmentAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); initView(); } private void initView() { Log.i(TAG,"initView----"); viewPager = (ViewPager)findViewById(R.id.viewpager); tvCalendar = (TextView) findViewById(R.id.tvCalendar); tvReminder = (TextView) findViewById(R.id.tvReminder); tvNote = (TextView) findViewById(R.id.tvNote); tvMore = (TextView) findViewById(R.id.tvMore); tvCalendar.setOnClickListener(this); tvReminder.setOnClickListener(this); tvNote.setOnClickListener(this); tvMore.setOnClickListener(this); adapter = new FragmentAdapter(getSupportFragmentManager()); viewPager.setAdapter(adapter); } @Override public void onClick(View arg0) { switch (arg0.getId()) { case R.id.tvCalendar: viewPager.setCurrentItem(TAB_CALENDAR); findViewById(R.id.tvConfig); break; case R.id.tvMore: viewPager.setCurrentItem(TAB_MORE); break; case R.id.tvNote: viewPager.setCurrentItem(TAB_NOTE); break; case R.id.tvReminder: viewPager.setCurrentItem(TAB_REMINDER); break; default: break; } } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } }
通过两种方式来切换页面,左右滑动以及点击底部菜单栏。第二种方式是使用onClick()方法,第二种方式则是以继承FragmentPagerAdapter来控制Fragment的显示。
package com.lanyuweng.mibaby.fragment; import com.lanyuweng.mibaby.MainActivity; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; public class FragmentAdapter extends FragmentPagerAdapter { public final static int TAB_COUNT = 4; public FragmentAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int arg0) { switch (arg0) { case MainActivity.TAB_CALENDAR: CalendarFragment calendarFragment = new CalendarFragment(); return calendarFragment; case MainActivity.TAB_REMINDER: ReminderFragment reminderFragment = new ReminderFragment(); return reminderFragment; case MainActivity.TAB_NOTE: NoteFragment noteFragment = new NoteFragment(); return noteFragment; case MainActivity.TAB_MORE: MoreFragment moreFragment = new MoreFragment(); return moreFragment; } return null; } @Override public int getCount() { // TODO Auto-generated method stub return TAB_COUNT; } }
同时,XML为:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_weight="9" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="55sp" android:layout_weight="1" android:background="@color/burlywood" android:orientation="horizontal" > <TextView android:id="@+id/tvCalendar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1" android:drawableTop="@drawable/calendar" android:gravity="center" android:text="@string/calendar" android:textSize="12sp" tools:ignore="NestedWeights" > </TextView> <TextView android:id="@+id/tvReminder" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1" android:drawableTop="@drawable/reminder" android:gravity="center" android:text="@string/reminder" android:textSize="12sp" > </TextView> <TextView android:id="@+id/tvNote" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1" android:drawableTop="@drawable/note" android:gravity="center" android:text="@string/note" android:textSize="12sp" > </TextView> <TextView android:id="@+id/tvMore" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1" android:drawableTop="@drawable/more" android:gravity="center" android:text="@string/more" android:textSize="12sp" > </TextView> </LinearLayout> </LinearLayout>
记录一下fragmentactivity+viewpager+adapter的使用,以备后用
Android FragmentActivity+viewpager的使用
标签:
原文地址:http://www.cnblogs.com/lan4/p/4274195.html