build.gradle(:app)
android {
...
buildFeatures {
viewBinding true
}
}
一、BaseActivity
1、BaseAbstractActivity
abstract public class BaseAbstractActivity<V extends ViewBinding> extends AppCompatActivity {
protected V mViewBinding = null;
private View mBaseView = null;
protected Context mContext;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
if (useViewBinding()) {
//初始化 viewBinding
initViewBinding();
}
if (getLayoutId() != 0) {
if (!useViewBinding() || mViewBinding == null) {
//不支持viewBinding时,获取根布局
LayoutInflater inflater = LayoutInflater.from(this);
mBaseView = inflater.inflate(getLayoutId(), null);
} else {
//支持viewBinding时,获取根布局
mBaseView = mViewBinding.getRoot();
}
setContentView(mBaseView);
}
init();
}
/**
* 是否使用 ViewBinding, 默认使用
*
* @return
*/
protected boolean useViewBinding() {
return true;
}
/**
* 初始化 viewBinding
*/
private void initViewBinding() {
try {
ParameterizedType type = null;
type = (ParameterizedType) getClass().getGenericSuperclass();
if (type == null) return;
Class<V> cls = (Class<V>) type.getActualTypeArguments()[0];
Method method = cls.getDeclaredMethod("inflate", LayoutInflater.class);
mViewBinding = (V) method.invoke(null, getLayoutInflater());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Toast 提示
*
* @param msg
*/
protected void showToast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
protected abstract int getLayoutId();
protected abstract void init();
}
MainActivity
public class MainActivity extends BaseAbstractActivity<ActivityMainBinding> {
@Override
protected int getLayoutId() {
return R.layout.activity_main;
}
@Override
protected void init() {
mViewBinding.btnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showToast("点击了按钮");
}
});
}
}
layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/btn_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/black"
android:padding="30dp"
android:text="Button"
android:textColor="@color/white"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
二、BaseFragment
BaseAbstractFragment
abstract public class BaseAbstractFragment<V extends ViewBinding> extends Fragment {
protected V mViewBinding = null;
protected Context mContext = null;
private View mBaseView = null;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getContext();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (useViewBinding()) {
//使用 viewBinding
initViewBinding();
}
if (getLayoutId() != 0) {
if (!useViewBinding() || mViewBinding == null) {
//不支持viewBinding
mBaseView = LayoutInflater.from(getContext()).inflate(getLayoutId(), null);
} else {
//支持viewBinding
mBaseView = mViewBinding.getRoot();
}
return mBaseView;
}
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
init();
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
@Override
public void onDestroy() {
super.onDestroy();
}
/**
* 是否使用 ViewBinding, 默认使用
*
* @return
*/
protected boolean useViewBinding() {
return true;
}
/**
* 初始化 viewBinding
*/
private void initViewBinding() {
try {
ParameterizedType type = null;
type = (ParameterizedType) getClass().getGenericSuperclass();
if (type == null) return;
Class<V> cls = (Class<V>) type.getActualTypeArguments()[0];
Method method = cls.getDeclaredMethod("inflate", LayoutInflater.class);
mViewBinding = (V) method.invoke(null, getLayoutInflater());
} catch (Exception e) {
e.printStackTrace();
}
}
protected abstract int getLayoutId();
protected abstract void init();
/**
* 展示 Toast 信息
*
* @param msg
*/
protected void showToast(String msg) {
Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
}
}
OneFragment
public class OneFragment extends BaseAbstractFragment<FragmentOneBinding> {
@Override
protected int getLayoutId() {
return R.layout.fragment_one;
}
@Override
protected void init() {
mViewBinding.tvOneDisplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showToast("点击了 One Fragment 中的 TextView");
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_red_light">
<TextView
android:id="@+id/tv_one_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:padding="20dp"
android:text="one fragment"
android:textColor="@android:color/holo_purple"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
TwoFragment
public class TwoFragment extends BaseAbstractFragment<FragmentTwoBinding> {
@Override
protected int getLayoutId() {
return R.layout.fragment_two;
}
@Override
protected void init() {
mViewBinding.tvTwoDisplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showToast("点击了 Two Fragment 中的 TextView");
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_dark">
<TextView
android:id="@+id/tv_two_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_dark"
android:padding="20dp"
android:text="two fragment"
android:textColor="@color/white"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
SecondActivity
public class SecondActivity extends BaseAbstractActivity<ActivitySecondBinding> {
private List<Fragment> mFragmentList = null;
private FragmentManager mFragmentManager = null;
private Fragment mCurrentFragment;
@Override
protected int getLayoutId() {
return R.layout.activity_second;
}
@Override
protected void init() {
mFragmentList = new ArrayList<>();
mFragmentList.add(new OneFragment());
mFragmentList.add(new TwoFragment());
mFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
mCurrentFragment = mFragmentList.get(0);
fragmentTransaction.add(R.id.frame_change, mCurrentFragment);
fragmentTransaction.show(mCurrentFragment);
fragmentTransaction.commitAllowingStateLoss();
mViewBinding.btnOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
changeFragment(0);
}
});
mViewBinding.btnTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
changeFragment(1);
}
});
}
/**
* 切换 fragment
*
* @param position
*/
private void changeFragment(int position) {
if (mCurrentFragment == mFragmentList.get(position)) {
return;
}
mCurrentFragment = mFragmentList.get(position);
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
//首先隐藏所有fragment
for (int i = 0; i < mFragmentList.size(); i++) {
fragmentTransaction.hide(mFragmentList.get(i));
}
//添加fragment
if (!mCurrentFragment.isAdded()) {
fragmentTransaction.add(R.id.frame_change, mCurrentFragment);
}
fragmentTransaction.show(mCurrentFragment);
fragmentTransaction.commitAllowingStateLoss();
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/frame_change"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="20dp"
android:background="@android:color/darker_gray"
android:padding="10dp">
<TextView
android:id="@+id/btn_one"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/black"
android:gravity="center"
android:padding="10dp"
android:text="one"
android:textColor="@color/white"
android:textSize="20dp" />
<TextView
android:id="@+id/btn_two"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/holo_red_dark"
android:gravity="center"
android:padding="10dp"
android:text="two"
android:textColor="@color/white"
android:textSize="20dp" />
</LinearLayout>
</FrameLayout>