Android——使用Fragment实现窗口切换

1.Fragment是什么?

碎片(Fragment)是一种可以嵌入在活动当中的UI片段,它能让程序更加合理和充分地利用大屏幕的空间,因而在平板上应用的非常广泛。虽然碎片对你来说应该是个全新的概念,但我相信你学习起来应该毫不费力,因为它和活动实在是太像了,同样都能包含布局,同样都有自己的生命周期。你甚至可以将碎片理解成一个迷你型的活动,虽然这个迷你型的活动有可能和普通的活动是一样大的。

2.Fragment的生命周期

Fragment必须是依存与Activity而存在的,因此Activity的生命周期会直接影响到Fragment的生命周期。如下图,这是fragment与activity的生命周期关系:

161725_oW0Q_3508669.png

这里具体的方法不再赘述

3.使用Fragment进行窗口切换

一共有4个界面,由每个Activity对应一个Fragment,通过FragmentTransaction互相切换。

Fragment代码:

package nju.edu.futures_android.Fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.support.v7.widget.RecyclerView.ViewHolder;

import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import nju.edu.futures_android.R;
import nju.edu.futures_android.Util.DividerItemDecoration;
import nju.edu.futures_android.Util.HomeAdapter;
import nju.edu.futures_android.Util.pagerViewAdapter;
import nju.edu.futures_android.VO.URLVO;
import nju.edu.futures_android.logic.GetURL;
import nju.edu.futures_android.logic.OnItemClickListener;

/**
 * Created by nick on 16/8/13.
 */
public class Fragment_home extends Fragment implements OnItemClickListener {
    private RollPagerView pagerView;

    private RecyclerView mRecyclerView;
    private HomeAdapter mAdapter;

    private GetURL connector ;
    private Vector<URLVO> urls;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.tab_home, container, false);

        getNews();

        initView(view);

        return view;
    }

    private void getNews() {
        connector = new GetURL();
        urls=connector.getURL();
    }


    public void initView(View parent){
        //滚动条
        pagerView = (RollPagerView) parent.findViewById(R.id.roll_view_pager);
        pagerView.setPlayDelay(3000);   //设置播放时间间隔
        pagerView.setAnimationDurtion(1000);//设置动画时长
        pagerView.setAdapter(new pagerViewAdapter());//设置适配器
        pagerView.setHintView(new ColorPointHintView(parent.getContext(),Color.YELLOW, Color.WHITE));

        //新闻list
        mRecyclerView = (RecyclerView)parent.findViewById(R.id.id_list_news);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(parent.getContext()));
        mAdapter = new HomeAdapter(urls);
        mAdapter.setOnItemClickListener(this);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(parent.getContext(),
                DividerItemDecoration.VERTICAL_LIST));

    }


    @Override
    public void onItemClick(View view, int position) {
        Intent intent = new Intent();
        intent.putExtra("url",urls.get(position).getUrl());
        intent.setAction("nju.edu.futures.news");
        startActivity(intent);
    }
}

Activity代码:

4.这样做的优势

  • FragmentManager提供了对Activity运行时的Fragment的添加、删除、替换的操作。在Activity运行期间可以添加Fragment而不是在XML布局文件中进行定义。如果打算在Activity中改变Fragment的生命过程。如果要执行添加、删除、修改的操作,必须通过FragmentManager的对象获得一个FragmentTransaction对象,通过它的API来执行这些操作。

  • 将页面切换的代码集中在Fragment,使得Activity的类中不必管理界面切换,降低代码的耦合度,使程序更易维护。

  • 使用了Fragment以适配不同分辨率的硬件。使得应用能够适配不同的布局。

  • 对于类似Tab页面切换,如果使用Activity则需要一次性全部加载,而Fragment则不需要,降低了初始化所需要的加载时间。

转载于:https://my.oschina.net/u/3508669/blog/919515

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值