Android之BaseFragment的封装(第一篇)
创建子Fragment继承BaseFragment
package com. example. shoppingmall. home. fragment;
import android. graphics. Color;
import android. os. Bundle;
import android. util. Log;
import android. view. LayoutInflater;
import android. view. View;
import android. view. ViewGroup;
import android. widget. TextView;
import androidx. annotation. NonNull;
import androidx. annotation. Nullable;
import androidx. fragment. app. Fragment;
import com. example. shoppingmall. R;
import com. example. shoppingmall. base. BaseFragment;
public class HomeFragment extends BaseFragment {
private TextView textView;
private static String TAG= HomeFragment. class . getSimpleName ( ) ;
@Override
protected View initView ( ) {
Log. e ( "TAG" , "主页面被初始化了" ) ;
textView= new TextView ( mContext) ;
textView. setTextSize ( 25 ) ;
textView. setTextColor ( Color. RED) ;
return textView;
}
@Override
public void initData ( ) {
super . initData ( ) ;
Log. e ( "TAG" , "主页面数据被初始化了" ) ;
textView. setText ( "This is HomeFragment" ) ;
}
}
剩余的子Fragment都是如此
接着在主页面添加各个Fragment并实现切换
package com. example. shoppingmall. app;
import android. content. Context;
import android. os. Bundle;
import android. widget. FrameLayout;
import android. widget. RadioButton;
import android. widget. RadioGroup;
import androidx. fragment. app. Fragment;
import androidx. fragment. app. FragmentActivity;
import androidx. fragment. app. FragmentManager;
import androidx. fragment. app. FragmentTransaction;
import com. example. shoppingmall. R;
import com. example. shoppingmall. base. BaseFragment;
import com. example. shoppingmall. community. fragment. CommunityFragment;
import com. example. shoppingmall. home. fragment. HomeFragment;
import com. example. shoppingmall. shoppingcart. fragment. ShoppingCartFragment;
import com. example. shoppingmall. type. fragment. TypeFragment;
import com. example. shoppingmall. user. fragment. UserFragment;
import java. util. ArrayList;
import butterknife. BindView;
import butterknife. ButterKnife;
public class MainActivity extends FragmentActivity {
@BindView ( R. id. frameLayout)
FrameLayout frameLayout;
@BindView ( R. id. rb_home)
RadioButton rbHome;
@BindView ( R. id. rb_type)
RadioButton rbType;
@BindView ( R. id. rb_community)
RadioButton rbCommunity;
@BindView ( R. id. rb_cart)
RadioButton rbCart;
@BindView ( R. id. rb_user)
RadioButton rbUser;
@BindView ( R. id. rg_main)
RadioGroup rgMain;
public ArrayList< BaseFragment> fragments;
private int position;
private Fragment tempFragment;
private BaseFragment mContext;
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
setContentView ( R. layout. activity_main) ;
ButterKnife. bind ( this ) ;
rgMain= ( RadioGroup) findViewById ( R. id. rg_main) ;
fragments= new ArrayList < > ( ) ;
initFragment ( ) ;
initListener ( ) ;
}
private void initListener ( ) {
rgMain. check ( R. id. rb_home) ;
rgMain. setOnCheckedChangeListener ( new RadioGroup. OnCheckedChangeListener ( ) {
@Override
public void onCheckedChanged ( RadioGroup group, int checkedId) {
switch ( checkedId) {
case R. id. rb_home: / / 主页
position= 0 ;
break ;
case R. id. rb_type: / / 分类
position= 1 ;
break ;
case R. id. rb_community: / / 发现
position= 2 ;
break ;
case R. id. rb_cart: / / 购物车
position= 3 ;
break ;
case R. id. rb_user: / / 用户中心
position= 4 ;
break ;
default :
break ;
}
BaseFragment baseFragment= getFragment ( position) ;
switchFragment ( baseFragment) ;
}
} ) ;
}
private void initFragment ( ) {
fragments. add ( new HomeFragment ( ) ) ;
fragments. add ( new TypeFragment ( ) ) ;
fragments. add ( new CommunityFragment ( ) ) ;
fragments. add ( new ShoppingCartFragment ( ) ) ;
fragments. add ( new UserFragment ( ) ) ;
}
private BaseFragment getFragment ( int position) {
if ( fragments!= null&& fragments. size ( ) > 0 ) {
BaseFragment baseFragment= fragments. get ( position) ;
return baseFragment;
}
return null;
}
private void switchFragment ( BaseFragment baseFragment) {
FragmentManager manager= getSupportFragmentManager ( ) ;
FragmentTransaction transaction= manager. beginTransaction ( ) ;
transaction. replace ( R. id. frameLayout, baseFragment) ;
transaction. commit ( ) ;
}
}
关于transaction.replace的源码分析:
@NonNull
public FragmentTransaction replace ( @IdRes int containerViewId, @NonNull Fragment fragment) {
return replace ( containerViewId, fragment, null) ;
}
在项目中切换Fragment用replace()方法来替换Fragment,但是存在一个很大的问题,每次切换的时候Fragment都会重新实例化,重新加载一次数据,这样做是非常消耗app性能和用户的流量的。
官方文档解释说:replace()方法只是在上一个Fragemnt不再需要时采用的简便方法。
So I will taking another method to replace it !