初学者对于Fragment的应用

作为一名初学者 要想运用好Fragment 首先我们要搞清楚什么事Fragment。

什么是Fragment

Fragment中文翻译过来叫“片段”,但我觉得中文叫法有点生硬,还是保持叫Fragment比较好。它是Google为了屏幕适应而在android3.0后引入的一个API。

既然知道了Fragment是什么,那我们就要清楚什么时候去应用Fragment也就是用Fragment能够解决我们什么样的问题

用Fragment能够解决什么问题

1.也就是我刚才在解释什么是Fragment中说到的,为了解决屏幕适应问题,不需要为许多屏幕分辨率不同的设备而编写多套的布局方案。
2.使用Fragment可以在一个Activity中实现不同的界面的灵活切换。

在今天我学习了Fragment的静态加载方法和动态的加载方法,下面我会说明这些方法的运用并且附上我的一些代码。

Fragment静态加载方法

下面是今天老师教我们Fragment静态加载的4个步骤
1.新建类继承Fragment;
2.重写onCreateView方法;
3.使用LayoutInflater对象中的inflate方法绑定布局和控件;
4.在Activity相对应的布局文件中通过标签Fragment引用

下面是我今天对于Fragment静态加载的代码附上

下面是布局文件的代码

 <TextView
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="这是一个TextView"
        android:gravity="center"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <fragment
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
           android:name="ssqcom.administrator.myapplication.BlankFragment">

        </fragment>
        <fragment
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:name="ssqcom.administrator.myapplication.BlankFragment2">

        </fragment>
    </LinearLayout>

Fragment动态加载方法

下面是今天老师教我们Fragment动态加载的4个步骤
1.新建类继承Fragment;
2.重写onCreateView方法;
3.使用LayoutInflater对象中的inflate方法绑定布局和控件;
4.使用FragmentManager和FragmentTransaction对象进行动态加载

布局里面的代码


<LinearLayout
    android:orientation="vertical"
    android:layout_width="100dp"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/man_btn"
        android:text="男装"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
    <Button
        android:id="@+id/woman_btn"
        android:text="女装"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
</LinearLayout>
    <FrameLayout
        android:id="@+id/shop"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>



在Activity里的代码

  public void onClick(View view) {//点击事件
        FragmentManager manager = getFragmentManager();//创建FragmentManager对象
        FragmentTransaction transaction = manager.beginTransaction();//创建FragmentTransaction对象
        switch (view.getId()) {
            case R.id.man_btn://跳转man按钮
                //显示男装的fragment
                if (manfragment == null) {
                    manfragment = new manFragment();
                }
                transaction.replace(R.id.shop, manfragment);
//                FragmentManager manager=getFragmentManager();//创建FragmentManager对象
//                FragmentTransaction transaction=manager.beginTransaction();//创建FragmentTransaction对象
//                transaction.replace(R.id.shop,manfragment);
//                transaction.commit();//最后一步提交
                break;
            case R.id.woman_btn://跳转woman按钮
                if (womanfragment == null) {
                    womanfragment = new womanFragment();
                }
                transaction.replace(R.id.shop, womanfragment);
                break;
            default:
                break;
        }
        transaction.commit();//最后一步提交
    }

ViewPager+Fragement实现页卡切换

在使用ViewPager的时候我们需要使用适配器进行绑定

布局代码附上

 <android.support.v4.view.ViewPager
        android:id="@+id/vx_vp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        >

        <Button
            android:id="@+id/chat_btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="聊天"
            />
        <Button
            android:id="@+id/friend_btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="朋友圈"
            />
        <Button
            android:id="@+id/find_btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="发现"
            />


    </LinearLayout>

适配器代码


public class MyPagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> mfragmentList;

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

    }

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

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

Activity中的代码

  private Button chatbtn;
    private Button friendbtn;
    private Button findbtn;
    private ViewPager vxvp;

    private ChatFragment chatFragment;
    private FindFragment findFragment;
    private FriendFragment friendFragment;

    private List<Fragment> fragmentList=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vx);

        bindid();
        chatFragment=new ChatFragment();
        findFragment=new FindFragment();
        friendFragment=new FriendFragment();

        fragmentList.add(chatFragment);
        fragmentList.add(friendFragment);
        fragmentList.add(findFragment);


        //创建适配器
        MyPagerAdapter adapter=new MyPagerAdapter(getSupportFragmentManager(),fragmentList);
        //绑定适配器
        vxvp.setAdapter(adapter);
    }

    private void bindid() {
        chatbtn=findViewById(R.id.chat_btn);
        friendbtn=findViewById(R.id.friend_btn);
        findbtn=findViewById(R.id.find_btn);


        vxvp=findViewById(R.id.vx_vp);

        chatbtn.setOnClickListener(this);
        friendbtn.setOnClickListener(this);
        findbtn.setOnClickListener(this);






    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.chat_btn:
                vxvp.setCurrentItem(0);
                break;
            case R.id.friend_btn:
                vxvp.setCurrentItem(1);
                break;
            case R.id.find_btn:
                vxvp.setCurrentItem(2);
                break;
                default:
                    break;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值