Fragment作为现在常用的布局,其身影随处可见,如微信,微博,QQ等等,简单介绍一下点击重复加载数据的问题和处理方法。
1、Fragment的片段,只贴出一个,另外一个基本相同。
public class AFragment extends android.support.v4.app.Fragment {
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_a, container, false);
return view;
}
2、Fragment的布局文件,同上,也只贴出一个。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/et_sousuo"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="#fff"
android:gravity="center"
android:hint="搜索"
android:padding="5dp"
android:textSize="18sp" />
</LinearLayout>
3、activity_main中的布局:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--主内容显示区域 -->
<FrameLayout
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/mainTabBar"/>
<!--阴影分割线 -->
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/mainTabBar"
android:background="@mipmap/comui_bar_top_shadow"/>
<!--第一个LinearLayout 内有5个竖向LinearLayout-->
<LinearLayout
android:id="@+id/mainTabBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:paddingBottom="5dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/rb_goods"
android:layout_width="25dp"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginRight="20dp"
android:src="@drawable/comui_tab_home"
android:scaleType="centerInside"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/rb_mine"
android:layout_width="25dp"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginLeft="20dp"
android:src="@drawable/comui_tab_person_selected"
android:scaleType="centerInside"/>
</LinearLayout>
</LinearLayout>
<!--第二个LinearLayout 内有凸起按钮-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="81dp"
android:layout_alignParentBottom="true"
android:background="@android:color/transparent"
android:gravity="center|top"
android:orientation="horizontal">
<ImageView
android:id="@+id/rb_add"
android:layout_width="75dp"
android:layout_height="75dp"
android:onClick="onClickPublish"
android:src="@drawable/comui_tab_post_selected"/>
</LinearLayout>
</RelativeLayout>
4、下面就是主要逻辑的实现代码片段,首先看一下 activity_main布局
首页 = AFragment ,我的 = BFragment,有些程序员在点击切换的时候没有考虑到重复点击一直刷新数据的问题,其实解决很简单:
public void onClick(View v) { fragmentManager = getSupportFragmentManager(); fragmentTransaction = fragmentManager.beginTransaction(); switch (v.getId()){ case R.id.rb_a: if (AFragment ==null) {//判断要显示的frament是否等于空 AFragment = new AFragment(); fragmentTransaction.add(R.id.main_container, AFragment);//添加要显示的fragment } if (BFragment!=null){//判断另一个fragment是否不等于空 fragmentTransaction.hide(BFragment);//是的话就隐藏 } fragmentTransaction.show(AFragment);//显示点击的fragment break; case R.id.rb_b: if (BFragment==null) { BFragment = new BFragment(); fragmentTransaction.add(R.id.main_container, BFragment); } if (AFragment !=null){ fragmentTransaction.hide(AFragment); } fragmentTransaction.show(BFragment); break; } fragmentTransaction.commit(); }
/* 默认页面:AFragment */ AFragment = new AFragment(); fragmentTransaction.add(R.id.main_container,AFragment); fragmentTransaction.show(AFragment); fragmentTransaction.commit();