一,*Fragment的简介:*
1、Fragment是android3.0引入的心的API,它代表Activity的子模板,所以可以把fragment理解为Activity片段。
2、Fragment必须被“嵌入”Avtivity中使用,因此Fragment也拥有自己的生命周期,不过Fragment的生命周期受Activity所控制,也就是说Activity停止的时候,Activity中所有的Fragment都会被停止。其他状态也是一样。
二,Fragment的静态加载
首先要创建一个Fragment,然后在创建一个Activity使其让Fragment能够在Activity上寄存起来,然后在Activity的布局文件中添加fragment的节点
<fragment
android:id="@+id/fragment_show"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.dfcn.sexmouth.Fragment.OneFragment">//fragment的显示界面</fragment>
注意在fragment节点内一定得要添加id,否则程序在编译的时候会报错!
三,Fragment的动态加载
实现流程:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Display dis = getWindowManager().getDefaultDisplay();
if(dis.getWidth() > dis.getHeight())
{
Fragment1 f1 = new Fragment1();
getFragmentManager().beginTransaction().replace(R.id.LinearLayout1, f1).commit();
}
else
{
Fragment2 f2 = new Fragment2();
getFragmentManager().beginTransaction().replace(R.id.LinearLayout1, f2).commit();
}
}
}
四,ViewPager+Fragment实现页卡滑动
1.首先我们的创建一个适配器FragmentAdapter
package com.example.dfcn.sexmouth.Adpter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
/**
* Created by dfcn on 2018/6/5.
*/
public class FragmentAdpter extends FragmentPagerAdapter {
private List<Fragment> fragmentList;//创建存储Fragment的List列表
public FragmentAdpter(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();
}
}
2.创建需要滑动的Fragment
其xml布局文件如下
<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"
tools:context="com.example.dfcn.sexmouth.Fragment.OneFragment">
<!-- : Update blank fragment layout -->
<TextView
android:id="@+id/one_fragment"
android:gravity="center"
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment1" />
</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="#f0f"
tools:context="com.example.dfcn.sexmouth.Fragment.TwoFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/two_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment2" />
</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"
tools:context="com.example.dfcn.sexmouth.Fragment.ThreeFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:gravity="center"
android:textSize="50dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="朋友圈" />
</FrameLayout>
3.创建Activity
<?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.dfcn.sexmouth.MainActivity">
<LinearLayout
android:background="#21616166"
android:layout_weight="9"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_contack"
android:text="联系人"
android:textSize="20sp"
android:gravity="center"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<TextView
android:id="@+id/tv_chat"
android:text="聊天"
android:textSize="20sp"
android:gravity="center"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<TextView
android:id="@+id/tv_friend"
android:text="朋友圈"
android:textSize="20sp"
android:gravity="center"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="6dp">
<View
android:id="@+id/v_contack"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="6dp"/>
<View
android:id="@+id/v_chat"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="6dp"/>
<View
android:id="@+id/v_friend"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="6dp"/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
//创建ViewPager
<android.support.v4.view.ViewPager
android:id="@+id/main_vp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
</LinearLayout>
package com.example.dfcn.sexmouth;
import android.annotation.TargetApi;
import android.graphics.Color;
import android.os.Build;
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.TextView;
import com.example.dfcn.sexmouth.Adpter.FragmentAdpter;
import com.example.dfcn.sexmouth.Fragment.OneFragment;
import com.example.dfcn.sexmouth.Fragment.ThreeFragment;
import com.example.dfcn.sexmouth.Fragment.TwoFragment;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private View v_contack,v_chat,v_friend;
private TextView tv_contack,tv_chat,tv_friend;
private ViewPager viewPager;
private List<Fragment> fragmentList=new ArrayList<>();//实例化List
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
viewPager=findViewById(R.id.main_vp);
v_contack=findViewById(R.id.v_contack);
v_chat=findViewById(R.id.v_chat);
v_friend=findViewById(R.id.v_friend);
tv_chat=findViewById(R.id.tv_chat);
tv_contack=findViewById(R.id.tv_contack);
tv_friend=findViewById(R.id.tv_friend);
tv_contack.setOnClickListener(this);
tv_friend.setOnClickListener(this);
tv_chat.setOnClickListener(this);
//创建Fragment对象
OneFragment oneFragment=new OneFragment();
TwoFragment twoFragment=new TwoFragment();
ThreeFragment threeFragment=new ThreeFragment();
//添加Fragment到List中
fragmentList.add(oneFragment);
fragmentList.add(twoFragment);
fragmentList.add(threeFragment);
//创建适配器
FragmentAdpter fragmentAdpter=new FragmentAdpter(getSupportFragmentManager(),fragmentList);
//绑定适配器
viewPager.setAdapter(fragmentAdpter);
//滑动字体改变颜色
v_contack.setBackgroundColor(Color.BLUE);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onPageSelected(int position) {
v_contack.setBackgroundColor(Color.WHITE);
v_chat.setBackgroundColor(Color.WHITE);
v_friend.setBackgroundColor(Color.WHITE);
switch (position){
case 0:
v_contack.setBackgroundColor(Color.BLUE);
break;
case 1:
v_chat.setBackgroundColor(Color.BLUE);
break;
case 2:
v_friend.setBackgroundColor(Color.BLUE);
break;
default:
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
//点击改变页卡
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.tv_contack:
viewPager.setCurrentItem(0);
break;
case R.id.tv_chat:
viewPager.setCurrentItem(1);
break;
case R.id.tv_friend:
viewPager.setCurrentItem(2);
break;
default:
break;
}
}
}
五,Fragment的生命周期
六,Fragment与Acitvity两者之间的通信
1.activity传值到Fragment中
在Activity中创建Bundle数据包,调用Fragment实例的setArguments(bundle) 从而将Bundle数据包传给Fragment,然后Fragment中调用getArguments获得 Bundle对象,然后进行解析就可以了
Bundle bundle=new Bundle();
bundle.putString("name","和马");
oneFragment.setArguments(bundle);
Fragment接受
Bundle bundle=getArguments();
String name=bundle.getString("name");
btn.setText(name);
2.Fragment传值到Activity中
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_one, container, false);
btn=view.findViewById(R.id.fragment_one_btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MainActivity mainActivity= (MainActivity) getActivity();
mainActivity.setText("好友");
}
});
return view;
}
Activity的方法:
public void setText(String s){
tv_contack.setText(s);
}