Fragment相关内容

1.什么是Fragment?

Fragment碎片,即如同贴纸一样贴在Activity上面,用于向用户显示贴纸上的内容。可以在一个Activity中用多个Fragment组合来构建多窗格的UI,以及在多个Activity中重复使用某个Fragment。需要依赖宿主Activity,有自己的生命周期。

2.为什么要用Fragment?

Fragment可以较好的完成局部布局的刷新,且可以完成相关屏幕适配兼容。代码复用。特别适用于模块化的开发,因为一个Fragment可以被多个Activity嵌套,有个共同的业务模块就可以复用了,是模块化UI的良好组件

3.Fragment的生命周期

此图网上寻找转载

4.Fragment的静态加载

首先创建好一个Fragment与Activity,完成Fragment与Activity的布局文件Xml,在MainActivity的布局文件中用将将要插入的Fragment位置放好。设置name来绑定Fragment。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.wang3.myapplication.MainActivity">


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:id="@+id/nvbt"
            android:layout_width="80dp"
            android:layout_height="40dp"
            android:text="女装" />

        <Button
            android:id="@+id/nanbt"
            android:layout_width="80dp"
            android:layout_height="40dp"
            android:text="男装" />

    </LinearLayout>
    <fragment
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:name="com.example.wang3.myapplication.BlankFragment"
       />
</LinearLayout>

5.Fragment的动态加载

Fragment的动态加载实现点击按钮完成Fragment的动态加载切换,所以先建立几个Fragment,完成它们的布局文件,在MainActivity的布局文件中设置好所需控件,并绑定好Id,设置监听。在xml中用帧布局来确定Fragment的位置。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.wang3.myapplication.MainActivity">


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:id="@+id/nvbt"
            android:layout_width="80dp"
            android:layout_height="40dp"
            android:text="女装" />

        <Button
            android:id="@+id/nanbt"
            android:layout_width="80dp"
            android:layout_height="40dp"
            android:text="男装" />

    </LinearLayout>

    <FrameLayout
        android:id="@+id/framelayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>
</LinearLayout>

在将Fragment利用 FragmentManager和FragmentTransaction创建对象,transaction.replace来替换Fragment。最后提交事务。

package com.example.wang3.myapplication;


import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button nbt;//定义按钮对象
    private Button nnbt;//对象二
    private NewFragment fragment1;//定义碎片对象
    private BlankFragment fragment2;//碎片二

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BlindID();//绑定ID
    }

    private void BlindID() {
        nbt = findViewById(R.id.nvbt);
        nnbt = findViewById(R.id.nanbt);
        nbt.setOnClickListener(this);
        nnbt.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        FragmentManager manager=getFragmentManager();//创建fragmentManager对象
        FragmentTransaction transaction=manager.beginTransaction();//创建FragmentTransaction对象
        switch (view.getId()) {
            case R.id.nvbt:
                if (fragment1==null)//判断碎片1是否为空
                {
                    fragment1=new NewFragment();//创建实体碎片对象
                }
                transaction.replace(R.id.framelayout,fragment1);//使用一个fragment替换当前真布局
                break;
            case R.id.nanbt:
                if (fragment2==null)//同理
            {
                fragment2=new BlankFragment();
            }
                transaction.replace(R.id.framelayout,fragment2);//同理
                break;
        }
     transaction.commit();//提交事务
    }
}

6.Fragment与Viewpager点击滑动切换

首先创建好多个Fragment,完成想要的布局文件。并创建出MainActivity完成相关的xml布局中的控件。
用Viewpager来确定Fragment的位置。设置好相关ID。

<android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/linerlayout"
        android:layout_below="@id/text1">

    </android.support.v4.view.ViewPager>

重点是将Fragment放入Viewpager中,所以我们需要建立好Fragment与Viewpager之间的适配器。来将两者连接起来。

创建一个Adaper继承FragmentPagerAdapter并实现父类方法,然后定义一个List数组来传递Fragment
返回数组的size和position。

public class FragmentAdapter extends FragmentPagerAdapter {
    private List<Fragment> fragmentList;

    public FragmentAdapter(FragmentManager fm, List<Fragment> fragmentList) {
        super(fm);
        this.fragmentList = fragmentList;
    }

    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }

    @Override
    public int getCount() {
        return fragmentList.size();
    }
}

所要我们需要创建一个Fragment数组来存放我们的Fragment。

  private ArrayList<Fragment> fragmentArrayList = new ArrayList<>();

然后创建好Fragment的实体对象并且加入数组内。

        contextFragment = new ContextFragment();
        friendFragment = new FriendFragment();
        meFragment = new MeFragment();
        fragmentArrayList.add(contextFragment);
        fragmentArrayList.add(friendFragment);
        fragmentArrayList.add(meFragment);

绑定适配器

 FragmentAdapter fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), fragmentArrayList);
        viewPager.setAdapter(fragmentAdapter);

为了按钮点击实现滑动效果,我们先定义好所需按钮,绑定ID和设置监听。

    private Button button;
    private Button button2;
    private Button button3;
 private void BlindID() {
        button = findViewById(R.id.bt1);
        button2 = findViewById(R.id.bt2);
        button3 = findViewById(R.id.bt3);
        viewPager = findViewById(R.id.viewpager);
        textView1=findViewById(R.id.text1);
        button.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);

    }

最后设置监听内容

  @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt1:
                viewPager.setCurrentItem(0);
                break;
            case R.id.bt2:
                viewPager.setCurrentItem(1);
                break;
            case R.id.bt3:
                viewPager.setCurrentItem(2);
                break;
        }

    }

及完成滑动点击效果。

全码

package com.example.wang3.myapplication;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.wang3.myapplication.adapter.FragmentAdapter;
import com.example.wang3.myapplication.fragment.ContextFragment;
import com.example.wang3.myapplication.fragment.FriendFragment;
import com.example.wang3.myapplication.fragment.MeFragment;

import java.util.ArrayList;

public class WEActivity extends AppCompatActivity implements View.OnClickListener {
    private Button button;
    private Button button2;
    private Button button3;
    private TextView textView1;
    private ViewPager viewPager;
    private ArrayList<Fragment> fragmentArrayList = new ArrayList<>();
    private ContextFragment contextFragment;
    private FriendFragment friendFragment;
    private MeFragment meFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_we);
        BlindID();
        contextFragment = new ContextFragment();
        friendFragment = new FriendFragment();
        meFragment = new MeFragment();
        fragmentArrayList.add(contextFragment);
        fragmentArrayList.add(friendFragment);
        fragmentArrayList.add(meFragment);
        FragmentAdapter fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), fragmentArrayList);
        viewPager.setAdapter(fragmentAdapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        button.setTextColor(getResources().getColor(R.color.colorAccent));
                        button2.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
                        button3.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
                        break;
                    case 1:
                        button.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
                        button3.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
                        button2.setTextColor(getResources().getColor(R.color.colorAccent));
                        break;
                    case 2:
                        button.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
                        button2.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
                        button3.setTextColor(getResources().getColor(R.color.colorAccent));
                        break;
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void BlindID() {
        button = findViewById(R.id.bt1);
        button2 = findViewById(R.id.bt2);
        button3 = findViewById(R.id.bt3);
        viewPager = findViewById(R.id.viewpager);
        textView1=findViewById(R.id.text1);
        button.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);

    }

    public void name(String name){
        textView1.setText(name);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt1:
                viewPager.setCurrentItem(0);
                break;
            case R.id.bt2:
                viewPager.setCurrentItem(1);
                break;
            case R.id.bt3:
                viewPager.setCurrentItem(2);
                break;
        }

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值