-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- BaseFragment 支持 DataBinding,要使用请先在初始化时开启 `BaseFrameworkSettings.useDataBinding = true` 然后在 BaseActivity 上设置对应的 ViewBinding 泛型,例如 `MainFragment extends BaseBindingFragment<MainActivity, ActivityMainBinding>` 然后直接使用 `binding.` 即可。
- Loading branch information
Showing
6 changed files
with
81 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
baseframework/src/main/java/com/kongzue/baseframework/BaseBindingFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.kongzue.baseframework; | ||
|
||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
|
||
import androidx.viewbinding.ViewBinding; | ||
|
||
import java.lang.reflect.Method; | ||
import java.lang.reflect.ParameterizedType; | ||
|
||
public abstract class BaseBindingFragment<ME extends BaseActivity, VB extends ViewBinding> extends BaseFragment<ME> { | ||
|
||
public BaseBindingFragment(VB binding) { | ||
this.binding = binding; | ||
} | ||
|
||
public BaseBindingFragment() { | ||
} | ||
|
||
protected VB binding; | ||
|
||
@Override | ||
public void initViews() { | ||
|
||
} | ||
|
||
@Override | ||
public View resetContentView() { | ||
return userDataBindingCreateLayout(); | ||
} | ||
|
||
@Override | ||
public abstract void initDatas(); | ||
|
||
@Override | ||
public abstract void setEvents(); | ||
|
||
private View userDataBindingCreateLayout() { | ||
if (binding == null) { | ||
String bindingClassName = getViewBindClassName(); | ||
try { | ||
// 通过反射实例化Binding对象 | ||
Class<?> bindingClass = Class.forName(bindingClassName); | ||
Method inflateMethod = bindingClass.getMethod("inflate", LayoutInflater.class); | ||
binding = (VB) inflateMethod.invoke(null, getLayoutInflater()); | ||
return binding.getRoot(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} else { | ||
return binding.getRoot(); | ||
} | ||
} | ||
|
||
private String getViewBindClassName() { | ||
ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass(); | ||
String type = genericSuperclass.getActualTypeArguments()[1].toString(); | ||
if (type.contains(" ")) { | ||
String[] splitType = type.split(" "); | ||
type = splitType[splitType.length - 1]; | ||
} | ||
return type; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters