水文系列序言:
对于一个初入Android开发的菜鸟来说,从大神那里模仿一些东东,未尝不是一项必学菜鸟技能。
好比刚上学那会儿,很多东西都要不停的重复重复再重复。受此影响,博主打算将平时里看到的东东简单记录下来,打包成文,谓之“水文”,取“无营养、无含量”之意。当然了,这对很多人来说都是在浪费时间。针对我而言,权当作是学习的一个入门路子吧。如果在这条路上可以觅到同道中人,当是荣幸之至。若能得到高人指点一二,我当结草衔环,感激涕零之。
PS:本系列博客涉及到的XML文件大都来自原作者,博主只是根据需要作适当的修改。
正文开始:
想必大家看标题就明白了,这次我要默写的是官网提供的关于FragmentPagerAdapter的示例,详细的内容,大家可参见官方文档。
话不多说,打开你的Eclipse环境(当然,你也可以使用任何一款你喜欢的IDE,如AndroidStudio等),一步步开始你的Android工程创建之旅吧。如果在创建的工程中,你遇到任何问题,可以参考这里来排查问题。我相信你是OK的,比较一步步Next下去,总会有Finish的时候,除非你的环境搭建除了一点儿小问题:)
1、创建一个名为FragmentPagerAdapterDemo的Android 工程(记得,不是Java工程哦),其他保持默认配置:
2、一步步Next下来,在你的工程中会多出两个工程目录,一个是ADT自动添加进来的appcompat_v7,另一个则是以你在第一步中输入的工程名为目录名的工程,如FragmentPagerAdapterDemo工程。
3、修改MainActivity.java文件中的第52行代码,让其继承自ListFragment:
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends ListFragment {
//此处内容后面会补充
}
4、打开fragment_main.xml文件,按照官方提供的xml,直接复制过来:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:drawable/gallery_thumb">
<TextView android:id="@+id/text"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/hello_world"/>
<!-- The frame layout is here since we will be showing either
the empty view or the list view. -->
<!-- 下面的FrameLayout用于放置要显示的内容列表,或者当列表为空时,
放置要显示的提示文本控件 -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<!-- Here is the list. Since we are using a ListActivity, we
have to call it "@android:id/list" so ListActivity will
find it -->
<!-- 因为在demo中我们使用了ListFragment,因此根据需要,必须
声明一个id为@android:id/list的ListView控件 -->
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false"/>
<!-- Here is the view to show if the list is emtpy -->
<!-- 当数据为空时,显示如下控件,这里的文本提示信息是“No items” -->
<TextView android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="No items."/>
</FrameLayout>
</LinearLayout>
5、在PlaceholderFragment中添加我们需要的代码:
private TextView mPrompt;//为区别不同的Fragment而设置的提示信息
private int mNum;
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int num) {
PlaceholderFragment pf = new PlaceholderFragment();
//为Fragment传递显示的信息,这里简单的使用一个数字
Bundle bundle = new Bundle();
bundle.putInt("num", num);
pf.setArguments(bundle);
return pf;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments()==null ? -1 : getArguments().getInt("num");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
mPrompt = (TextView) rootView.findViewById(R.id.text);
mPrompt.setText("这里是第" + mNum + "个页(Fragment)");
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, week));
}
/**
* 重写ListFragment的方法,当用户点击列表行时显示信息
*/
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(getActivity(), "今天是" + week[position], Toast.LENGTH_SHORT).show();
}
6、在activity_main.xml中添加导航的按钮和ViewPager:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1">
</android.support.v4.view.ViewPager>
<LinearLayout android:orientation="horizontal"
android:gravity="center" android:measureWithLargestChild="true"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="0">
<Button android:id="@+id/goto_first"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="首页">
</Button>
<Button android:id="@+id/goto_last"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="尾页">
</Button>
</LinearLayout>
</LinearLayout>
7、根据布局文件,我们可用很容易的在MainActivity中实现我们的业务逻辑:
7.1 首先我们先创建一个内部类,即我们今天的主角FragmentPagerAdapter的子类。我们只需要重写它的两个重要方法即可:
class FragmentAdapter extends FragmentPagerAdapter {
public FragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
return PlaceholderFragment.newInstance(arg0);
}
@Override
public int getCount() {
return NUM_ITEMS;
}
}
7.2 在MainActivity中声明需要的成员,并初始化之:
private final int NUM_ITEMS = 10; //预先指定显示10页内容
private static String[] week = {"Mon", "Tus", "Wed", "Thu", "Fri", "San", "Sun"};
private FragmentAdapter mAdapter;
private ViewPager mPager;
private Button go2First;//导航到首页
private Button go2Last;//导航到尾页
成员的初始化:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPager = (ViewPager) findViewById(R.id.pager);
go2First = (Button) findViewById(R.id.goto_first);
go2Last = (Button) findViewById(R.id.goto_last);
mAdapter = new FragmentAdapter(getSupportFragmentManager());
initActions();
}
private void initActions() {
mPager.setAdapter(mAdapter);
go2First.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mPager.setCurrentItem(0);
}
});
go2Last.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mPager.setCurrentItem(NUM_ITEMS - 1);
}
});
}
OK,我们要做的就这些,上效果图吧:
源码下载地址:http://download.youkuaiyun.com/detail/u013665850/7410029