您的位置:首页 > 移动开发 > Android开发

Android Fragment的使用 三 Fragment与Activity交互

2017-04-16 13:45 507 查看
我这次是在Fragment类里定义公共接口让Activity复写,然后Fragment通过getActivity来调用此接口,这样一来就可以达成activity自己自定义管理Fragment,减少了Fragment与Activity的耦合,而且使得activity统一管理Fragment

使用例子如下:

public class MainActivity extends AppCompatActivity implements FragmentOne.FOneBtnClickListener,FragmentTwo.FTwoBtnClickListener{

private FragmentOne mFOne;
private FragmentTwo mFTwo;
private FragmentThree mFThree;

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

mFOne = new FragmentOne();
FragmentManager fm = getFragmentManager();
FragmentTransaction tx = fm.beginTransaction();
tx.add(R.id.id_content,mFOne,"ONE");
tx.commit();

}

@Override
public void onFOneBtnClick() {

if(mFTwo == null){
mFTwo = new FragmentTwo();
mFTwo.setfTwoBtnClickListener(this);
}
FragmentManager fm = getFragmentManager();
FragmentTransaction tx = fm.beginTransaction();
tx.replace(R.id.id_content,mFTwo,"TWO");
tx.addToBackStack(null);
tx.commit();
}

@Override
public void onFTwoBtnClick() {

if(mFThree == null){
mFThree = new FragmentThree();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction tx = fm.beginTransaction();
tx.hide(mFTwo);
tx.add(R.id.id_content,mFThree,"THREE");
tx.addToBackStack(null);
tx.commit();

}
}

public class FragmentOne extends Fragment implements View.OnClickListener {

private Button button;

public interface FOneBtnClickListener{
void onFOneBtnClick();
}

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,
Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_one,container,false);
button = (Button) view.findViewById(R.id.id_fragment_one_btn);
button.setOnClickListener(this);
return view;
}

@Override
public void onClick(View v){

if(getActivity() instanceof FOneBtnClickListener){
((FOneBtnClickListener)getActivity()).onFOneBtnClick();
}

}

}

public class FragmentTwo extends Fragment implements View.OnClickListener {

private Button button;

private FTwoBtnClickListener fTwoBtnClickListener;

public interface FTwoBtnClickListener{
void onFTwoBtnClick();
}

public void setfTwoBtnClickListener(FTwoBtnClickListener fTwoBtnClickListener){
this.fTwoBtnClickListener = fTwoBtnClickListener;
}

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,
Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_two,container,false);
button = (Button) view.findViewById(R.id.id_fragment_two_btn);
button.setOnClickListener(this);
return view;
}

@Override
public void onClick(View v){

if(fTwoBtnClickListener != null){
fTwoBtnClickListener.onFTwoBtnClick();
}

}

}

public class FragmentThree extends Fragment implements View.OnClickListener {

private Button button;

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,
Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_three,container,false);
button = (Button) view.findViewById(R.id.id_fragment_three_btn);
button.setOnClickListener(this);
return view;
}

@Override
public void onClick(View v){
Toast.makeText(getActivity(),"three",Toast.LENGTH_SHORT).show();

}

}

activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="android.com.myapplication.MainActivity">

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

</RelativeLayout>

fragment_one
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one"
android:id="@+id/id_fragment_one_btn"
android:layout_gravity="center"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

fragment_two

<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two"
android:id="@+id/id_fragment_two_btn"
android:layout_gravity="center"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

fragment_three

<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three"
android:id="@+id/id_fragment_three_btn"
android:layout_gravity="center"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>


下次讲运行时配置改变,Fragment的布局如何也能够跟着改变,为了解决屏幕大小问题和横竖屏问题。

再见
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: