As we know,Fragment可以被Activity托管,但是如果每次定义一个Fragment就要对Activity进行重新定义的话,代码的重复率非常的高。
于是可以使用一个抽象类来定义一个把所有重复的代码都包括进来的类。
开发环境
Android studio3.0
较2.0版本的区别,界面更加酷炫,功能各种酷炫、各种特效,其他的功能没有发现,因为刚刚开始用。
需要定义的类
SingleFragmentActivity.java
这个类是一个抽象类,同时也包括一个抽象方法,子类可以通过这个抽象方法传入自己要托管的Fragment。
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
public abstract class SingleFragmentActivity extends FragmentActivity {
protected abstract Fragment CreateFragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager=getSupportFragmentManager();
Fragment fragment=fragmentManager.findFragmentById(R.id.fragment_container);
if(fragment==null){
fragment=CreateFragment();
fragmentManager.beginTransaction().add(R.id.fragment_container,fragment).commit();
}
}
}
MainActivity.java
对应布局文件:activity_main.xml
此处实现了SingleFragmentActivity.java抽象类,并且实现了抽象方法CreateFragment,托管的Fragment为MyFragment。
import android.support.v4.app.Fragment;
/**
* Created by unclegy on 2017/11/6.
*/
public class MainActivity extends SingleFragmentActivity {
@Override
protected Fragment CreateFragment() {
return new MyFragment();
}
}
MyFragment.java
对应布局文件:fragment_my.xml
主要实现的方法是onCreateView方法。
import android.os.Bundle;
import android.support.annotation.Nullable;
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;
/**
* Created by unclegy on 2017/11/6.
*/
public class MyFragment extends Fragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_my,container,false);
Button button=view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(),"hello,world",Toast.LENGTH_LONG).show();
}
});
return view;
}
}
布局文件
activity_main.xml
重点是:FrameLayout这里就是Fragment的显示区域。如果定义多个FrameLayout就可以同时显示多个Fragment。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</android.support.constraint.ConstraintLayout>
fragment_my.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button"
android:layout_alignStart="@+id/button"
android:layout_marginBottom="75dp"
android:text="@string/myfragment" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/click"
/>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>