碎片(Fragment)是一种可以嵌入到活动当中的UI片段,它能让程序更加合理和充分的利用大屏幕的空间。
碎片的简单用法
首先写一个最简单的XML布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/fragment_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="fragment的测试按钮" />
</LinearLayout>
接下来,新建一个Fragment类,继承自supportV4 的Fragment
package com.example.administrator.myfragment.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import com.example.administrator.myfragment.R;
/**
* Created by Administrator on 2015/9/6.
*/
public class MyFirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.activity_fragment1,null);
Button mButton= (Button) view.findViewById(R.id.fragment_button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "您点击了这个测试按钮", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
然后在activity_main中使用标签在布局中添加碎片
<fragment
class="com.example.administrator.myfragment.fragment.MyFirstFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"></fragment>
动态添加碎片
在这里,设置三个按钮,通过按钮的点击事件来加入不同的碎片。在activity_main中进行三个按钮的布局,并加入一个FrameLayout布局,用来放置将来加载的碎片
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
<LinearLayout
android:id="@+id/fragment_linearlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button3"/>
</LinearLayout>
然后,新建两个继承自Fragment的类,总共有三个类
public class MyFirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.activity_fragment1,null);
Button mButton= (Button) view.findViewById(R.id.fragment_button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "您点击了这个测试按钮", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
public class MySecondFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.activity_fragment2,null);
}
public class MyThirdFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.activity_fragment3,null);
}
在MainActivity中修改代码实现三个按钮的点击事件,通过按钮的点击事件来加载碎片
package com.example.administrator.myfragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import com.example.administrator.myfragment.fragment.MyFirstFragment;
import com.example.administrator.myfragment.fragment.MySecondFragment;
import com.example.administrator.myfragment.fragment.MyThirdFragment;
public class MainActivity extends FragmentActivity implements View.OnClickListener{
private LinearLayout mLinearLayout;
private Button mButton1;
private Button mButton2;
private Button mButton3;
private MyFirstFragment mMyFirstFragment;
private MySecondFragment mMySecondFragment;
private MyThirdFragment mMyThirdFragment;
private FragmentManager mFragmentManager;
private FragmentTransaction mFragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLinearLayout= (LinearLayout) findViewById(R.id.fragment_linearlayout);
mButton1= (Button) findViewById(R.id.button1);
mButton2= (Button) findViewById(R.id.button2);
mButton3= (Button) findViewById(R.id.button3);
mButton1.setOnClickListener(this);
mButton2.setOnClickListener(this);
mButton3.setOnClickListener(this);
mFragmentManager=getSupportFragmentManager();
mMyFirstFragment=new MyFirstFragment();
mMySecondFragment=new MySecondFragment();
mMyThirdFragment =new MyThirdFragment();
mFragmentTransaction=mFragmentManager.beginTransaction();
mFragmentTransaction.add(R.id.fragment_container, mMyFirstFragment);
mFragmentTransaction.add(R.id.fragment_container,mMySecondFragment);
mFragmentTransaction.add(R.id.fragment_container,mMyThirdFragment);
mFragmentTransaction.hide(mMySecondFragment);
mFragmentTransaction.hide(mMyThirdFragment);
mFragmentTransaction.commit();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
mFragmentTransaction=mFragmentManager.beginTransaction();
mFragmentTransaction.hide(mMySecondFragment);
mFragmentTransaction.hide(mMyThirdFragment);
mFragmentTransaction.show(mMyFirstFragment);
//mFragmentTransaction.replace(R.id.fragment_container,new MyFirstFragment());
mFragmentTransaction.commit();
break;
case R.id.button2:
mFragmentTransaction=mFragmentManager.beginTransaction();
String text=mMyThirdFragment.getText();
mMySecondFragment.setText(text);
mFragmentTransaction.hide(mMyFirstFragment);
mFragmentTransaction.hide(mMyThirdFragment);
mFragmentTransaction.show(mMySecondFragment);
//mFragmentTransaction.replace(R.id.fragment_container,new MySecondFragment());
mFragmentTransaction.commit();
break;
case R.id.button3:
mFragmentTransaction=mFragmentManager.beginTransaction();
mFragmentTransaction.hide(mMySecondFragment);
mFragmentTransaction.hide(mMyFirstFragment);
mFragmentTransaction.show(mMyThirdFragment);
//mFragmentTransaction.replace(R.id.fragment_container,new MyThirdFragment());
mFragmentTransaction.commit();
break;
default:
break;
}
}
}
这里用的方法比较繁琐,是因为为了是实现碎片之间的通信而修改之后的代码,每个监听时间里可以通过使用mFragmentTransaction的replace方法来实现其功能。