Fragment,官方解释它为碎片,其实我倒喜欢将它看出是Activity中的控件View。只是使用起来与控件有些区别(因为它要自定义Fragment)。
首先来看看,固定使用Fragment,也就是
静态的Fragment
定义activity_fragment_main.xml
<?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"
tools:context="me.xxx.FragmentMainActivity">
<fragment
class="me.xxx.TitleFragment"
android:id="@+id/fragment_title_f"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<fragment
android:id="@+id/fragment_content_f"
class="me.xxx.ContentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/fragment_title_f"/>
</RelativeLayout>
这里有个问题需要注意一下
Caused by: Java.lang.IllegalArgumentException: Binary XML file line #7: Must specify unique android:id, android:tag, or have a parent with an id for me.xxx.ContentFragment
可能会出现以上的错误,原因是,没有个fragment定义id导致的,加上id问题就解决。
写Activity:
public class FragmentMainActivity extends FragmentActivity {
FragmentManager fragment;
TextView aaaaTv,bbbbTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_fragment_main);}}
在activity_fragment_main中我自定义了两个Fragment,接下来,简单的实现下他们两个就行。
TitleFragment
public class TitleFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_title,container, false);
return view;
}
}
ContentFragment
public class ContentFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_content,container,false);
}
}
在Fragment的布局里随便写点东西就可以了。接下来运行出现就能发现,这里的静态Fragment和View控件其实差不多。
实现动态的加载Fragment
也不难,只是需要多使用两个类,两个方法。
首先是fragment_interact.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" >
<include
android:id="@+id/layout_header"
layout="@layout/activity_header" />
<LinearLayout
android:id="@+id/my_question_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/layout_header"
android:paddingTop="@dimen/frame_and_ll"
android:orientation="vertical">
<TextView
android:id="@+id/my_question_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/my_question"
android:layout_marginLeft="@dimen/frame_and_ll"
android:textSize="18dp"/>
<ImageView
android:id="@+id/my_question_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/lineae"
android:layout_marginLeft="@dimen/frame_and_ll"
android:visibility="invisible"/>
</LinearLayout>
<LinearLayout
android:id="@+id/teacher_question_ll"
android:layout_width="wrap_content"
android:layout_toRightOf="@id/my_question_ll"
android:layout_below="@id/layout_header"
android:paddingTop="@dimen/frame_and_ll"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/teacher_question_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/teacher_question"
android:layout_marginLeft="10dp"
android:textSize="18dp"/>
<ImageView
android:id="@+id/teacher_question_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/lineae"
android:layout_marginLeft="10dp"
android:visibility="invisible"/>
</LinearLayout>
<FrameLayout
android:id="@+id/id_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/my_question_ll" />
</RelativeLayout>
由于项目需要,我是在Fragment上再创建Fragment。所以下面请看我的Fragment。
public class InteractFragment extends BaseFragment implements View.OnClickListener{
private View view;
private TextView myQuestionTv, teacherQuestioniTv;
private ImageView myQuestionIv, teacherQuestionIv;
private FragmentManager fragment;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_interact, container, false);
headerTitle = (TextView) view.findViewById(R.id.title_center);
setHeaderTitle(R.string.interactive);
myQuestionTv = (TextView) view.findViewById(R.id.my_question_tv);
teacherQuestioniTv = (TextView) view.findViewById(R.id.teacher_question_tv);
myQuestionTv.setOnClickListener(this);
teacherQuestioniTv.setOnClickListener(this);
myQuestionIv = (ImageView) view.findViewById(R.id.my_question_iv);
teacherQuestionIv = (ImageView) view.findViewById(R.id.teacher_question_iv);
firstFragment();
// Fragment.getSupportFragmentManager();
this.getFragmentManager();
return view;
}
public void firstFragment() {
modifyMyQuestion();
fragment = getFragmentManager();
FragmentTransaction transaction = fragment.beginTransaction();
transaction.add(R.id.id_content, new MyFragment());
transaction.commit();
}
@Override
public void onClick(View view) {
FragmentTransaction transaction = fragment.beginTransaction();
switch (view.getId()) {
case R.id.my_question_tv:
modifyMyQuestion();
transaction.replace(R.id.id_content, new MyFragment());
break;
case R.id.teacher_question_tv:
modifyTeacherQuestion();
transaction.replace(R.id.id_content, new TeacherFragment());
break;
default:
break;
}
transaction.commit();
}
public void modifyMyQuestion() {
teacherQuestionIv.setVisibility(View.INVISIBLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
teacherQuestioniTv.setTextColor(getActivity().getResources().getColor(R.color.black, getActivity().getTheme()));
}else {
teacherQuestioniTv.setTextColor(getActivity().getResources().getColor(R.color.black));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
myQuestionTv.setTextColor(getActivity().getResources().getColor(R.color.theme_green, getActivity().getTheme()));
}else {
myQuestionTv.setTextColor(getActivity().getResources().getColor(R.color.theme_green));
}
myQuestionIv.setVisibility(View.VISIBLE);
}
public void modifyTeacherQuestion() {
myQuestionIv.setVisibility(View.INVISIBLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
myQuestionTv.setTextColor(getActivity().getResources().getColor(R.color.black, getActivity().getTheme()));
}else {
myQuestionTv.setTextColor(getActivity().getResources().getColor(R.color.black));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
teacherQuestioniTv.setTextColor(getActivity().getResources().getColor(R.color.theme_green, getActivity().getTheme()));
}else {
teacherQuestioniTv.setTextColor(getActivity().getResources().getColor(R.color.theme_green));
}
teacherQuestionIv.setVisibility(View.VISIBLE);
}
}
这里用到的MyFragment和TeacherFragment跟上面的类似,可以自己简单写写。
最后我获得的效果如下图所示