Android Fragment的使用

在android 的开发中,经常会用到Fragment来进行页面的切换。其中经常用到的有ViewPager+Fragment。

先说一下单独使用Fragment来进行页面的切换,并且,切换的页面中包含有GridView。

上代码吧:main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <LinearLayout
        android:id="@+id/line_icon"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/img_find_stage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="17dp"
            android:layout_marginTop="10dp" />

        <ImageView
            android:id="@+id/img_find_course"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="17dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp" />

        <ImageView
            android:id="@+id/img_find_unit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="17dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp" />
    </LinearLayout>

    <com.view.MyScrollView
        android:id="@+id/my_scroll_frag"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/line_icon"
        android:layout_marginTop="10dp"
        android:background="#4c4c4c">

        <FrameLayout
            android:id="@+id/fragment_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="50dp">

        </FrameLayout>
    </com.view.MyScrollView>
</RelativeLayout>
我的布局是这样的,只做记录,不能照搬,大家根据需求来弄,我的FrameLayout是在ScrollView的下面。大家如果不需要滚动可以不将FrameLayout嵌套在ScrollView下。

布局完成,那么我们就要具体到我们的Fragment中了。

我们是利用FrameLayout来加载Fragment。Fragment部分的xml布局course_fragment.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/text_findout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="20dp"
            android:text="搜索结果"
            android:textSize="15sp" />

        <com.view.MyGridView
            android:id="@+id/grid_find_course"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/text_findout"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="25dp"
            android:verticalSpacing="12dp">

        </com.view.MyGridView>

    </RelativeLayout>

</RelativeLayout>
依旧是那句话,根据自己的需求,来写Fragment的布局文件。我所需求的是Fragment的布局文件下包含一个gridview。大家可以换成TextView等控件。活学活用。

对应的Fragment有对应的class,如下:

public class CourseFragment extends Fragment {
    private View rootview;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootview = inflater.inflate(R.layout.course_fragment, null);
        gridView_find_course = (GridView) rootview.findViewById(R.id.grid_find_course);
        return rootview;
    }
}
这部分代码就是加载fragment的布局。如果要获取gridview的资源,如上可以获取到。至于给gridview的adapter就不说了。这部分在这里不是关键。

我们重点说Fragment的界面切换的实现。

CourseFragment.class和course_fragment.xml说完,这部分的准备工作算是完整了。

就要到我们的主activity中进行适配了。

MainActivity.class:

private FragmentManager fm;
private FragmentTransaction ft;
先声明Fragment管理器和Transaction。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    fm = getFragmentManager();
    ft = fm.beginTransaction();
    ft.replace(R.id.fragment_content, new CourseFragment());
    ft.commit();
}
onCreate()中定义的是默认状态下的Fragment显示的界面。我们定义默认显示CourseFragment()。

然后就是我们的ImageView的点击监听。

@Override
public void onClick(View v) {
    fm = getFragmentManager();
    ft = fm.beginTransaction();
    switch (v.getId()) {
        case R.id.img_find_stage:    
            ft.replace(R.id.fragment_content, new StageFragment());
            break;
        case R.id.img_find_course: 
            ft.replace(R.id.fragment_content, new CourseFragment());
            break;
        case R.id.img_find_unit:
            ft.replace(R.id.fragment_content, new UnitFragment());
            break;
        default:
            break;
    }
    // 不要忘记提交
    ft.commit();
}
这部分就是点击相应的imageview然后replace掉原来的fragment。CourseFragment()只是我们的例子,剩下的StageFragment和UnitFrament基本类似,大家可以自行编写。

这样就实现了我们的Fragment的点击按钮切换。但是不能侧滑。

如果要实现滑动tab来继续fragment的切换就需要用到我们上面提到过的viewpager+fragment了。

至于viewpager+fragment的,大家看了我的这部分描述以后,再进行研究会发现其实原理相同。

大家可以参考:http://blog.163.com/shexinyang@126/blog/static/13673931220157552844552/


今天就不总结这个了,以后有机会再对viewpager+fragment进行总结吧。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值