andorid Fragment点击重复加载数据的简单解决方法(闲鱼导航布局)

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();

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值