**## Fragment ##**
- 什么是Fragment
fragment意为碎片,多个fragment进行组合可以做成多窗的UI界面。他有着自己的生命周期。 - 用Fragment能解决什么问题
能解决多种产品之间分辨率不同的问题。 - Fragment静态加载方法
首先我们得建两个fragment布局文件,这两个布局我们只需要简单设定一下颜色和文字意思一下就好了。
<FrameLayout 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"
android:background="#90ff55"
tools:context="com.example.pc.fragment.fragment.Fragmentone">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment_one"
android:textSize="30sp"/>
</FrameLayout>
<FrameLayout 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"
android:background="#605555"
tools:context="com.example.pc.fragment.fragment.Fragmenttwo">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment_two"
android:textSize="30sp"/>
</FrameLayout>
然后建两个类分别继承上面两个布局文件,然后我们在主Activity的布局文件里添上简单的布局。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="vertical"
tools:context="com.example.pc.fragment.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="Hello World!"
android:textSize="30sp"
android:gravity="center"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<fragment
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/f_one"
android:name="com.example.pc.fragment.fragment.Fragmentone"
/>
<fragment
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/f_two"
android:name="com.example.pc.fragment.fragment.Fragmenttwo"
/>
</LinearLayout>
</LinearLayout>
最后显示如下效果
- Fragment动态加载方法
动态的加载方法大部分与静态一样,使用除了主的布局和activity不一样代码如下。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.example.pc.fragment.TianMaoActivity">
<LinearLayout
android:id="@+id/lb"
android:layout_width="100dp"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/nvzhuang_btn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="女装"/>
<Button
android:id="@+id/nanzhuang_btn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="男装"/>
</LinearLayout>
<FrameLayout
android:id="@+id/yemian"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@id/lb"
android:layout_marginLeft="5dp"
>
</FrameLayout>
</RelativeLayout>
package com.example.pc.fragment;
import android.app.Fragment;
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 com.example.pc.fragment.fragment.NanzhuangFragment;
import com.example.pc.fragment.fragment.NvzhuangFragment;
public class TianMaoActivity extends AppCompatActivity implements View.OnClickListener{
private Button nanBtn;
private Button nvBtn;
private NanzhuangFragment nanzhuangFragment;
private NvzhuangFragment nvzhuangFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tian_mao);
bindId();
}
private void bindId(){
nanBtn = findViewById(R.id.nanzhuang_btn);
nvBtn = findViewById(R.id.nvzhuang_btn);
nanBtn.setOnClickListener(this);
nvBtn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
switch (view.getId()){
case R.id.nanzhuang_btn:
if(nanzhuangFragment==null){
nanzhuangFragment = new NanzhuangFragment();
}
transaction.replace(R.id.yemian,nanzhuangFragment);
break;
case R.id.nvzhuang_btn:
if(nvzhuangFragment==null){
nvzhuangFragment = new NvzhuangFragment();
}
transaction.replace(R.id.yemian,nvzhuangFragment);
break;
default:
break;
}
transaction.commit();
}
}
5. Viewpager+Fragment实现页卡滑动切换
package com.example.pc.fragment.adapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
/**
* Created by pc on 2018/3/6.
*/
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();
}
}
package com.example.pc.fragment;
import android.support.v4.app.Fragment;
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 com.example.pc.fragment.adapter.MypagerAdapter;
import com.example.pc.fragment.fragment.ContactFragment;
import com.example.pc.fragment.fragment.FriendFragment;
import com.example.pc.fragment.fragment.NewsFragment;
import java.util.ArrayList;
import java.util.List;
public class WxActivity extends AppCompatActivity implements View.OnClickListener{
private Button contactBtn;
private Button friendBtn;
private Button newsBtn;
private ViewPager viewPager;
private ContactFragment contactFragment;
private FriendFragment friendFragment;
private NewsFragment newsFragment;
private List<Fragment> fragmentList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wx);
bindID();
contactFragment = new ContactFragment();
friendFragment = new FriendFragment();
newsFragment = new NewsFragment();
fragmentList.add(contactFragment);
fragmentList.add(friendFragment);
fragmentList.add(newsFragment);
MypagerAdapter adapter = new MypagerAdapter(getSupportFragmentManager(),fragmentList);
viewPager.setAdapter(adapter);
}
private void bindID() {
contactBtn = findViewById(R.id.contact_btn);
friendBtn = findViewById(R.id.friend_btn);
newsBtn = findViewById(R.id.news_btn);
viewPager = findViewById(R.id.wx_vp);
contactBtn.setOnClickListener(this);
friendBtn.setOnClickListener(this);
newsBtn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.contact_btn:
viewPager.setCurrentItem(0);
break;
case R.id.friend_btn:
viewPager.setCurrentItem(1);
break;
case R.id.news_btn:
viewPager.setCurrentItem(2);
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.example.pc.fragment.WxActivity">
<LinearLayout
android:id="@+id/wx_bottom"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/contact_btn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:text="联系人"/>
<Button
android:id="@+id/friend_btn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:text="朋友圈"/>
<Button
android:id="@+id/news_btn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:text="动态"/>
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/wx_vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/wx_bottom">
</android.support.v4.view.ViewPager>
</RelativeLayout>
本文详细介绍了Fragment的概念及其在Android应用开发中的作用。Fragment作为一种可重用的UI组件,能够帮助开发者更好地应对不同屏幕尺寸的问题。文中通过实例展示了Fragment的静态加载与动态加载方法,并介绍了如何结合ViewPager实现流畅的页面切换效果。
1980

被折叠的 条评论
为什么被折叠?



