我正在尝试让我的一个功能在我的所有绑定活动中都很常见。我想听听你的一些建议。
目前我的代码如下: -
public static void stateDialog(Context context, String[] stateList, final FragmentSendAlertBinding binding) {
mStateDialog = new Dialog(context);
mStateDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
mStateDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mStateDialog.show();
mStateDialog.setCancelable(false);
mStateDialog.setContentView(R.layout.dialog_select_state);
CardView cvState = (CardView) mStateDialog.findViewById(R.id.cv_state);
final ListView states = (ListView) mStateDialog.findViewById(R.id.countryList);
final TextView tvCancel = (TextView) mStateDialog.findViewById(R.id.tv_cancel);
final Animation myAnim = AnimationUtils.loadAnimation(context, R.anim.bounce);
MyBounceInterpolator interpolator = new MyBounceInterpolator(0.2, 20);
myAnim.setInterpolator(interpolator);
cvState.startAnimation(myAnim);
ArrayAdapter<String> itemList = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, stateList);
states.setAdapter(itemList);
states.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
binding.tvState.setText((String) arg0.getItemAtPosition(position));
mStateDialog.dismiss();
}
});
tvCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mStateDialog.dismiss();
}
});
}
我可以使用类似的东西:
public static void stateDialog(Context context, String[] stateList, final ViewDataBinding<T> binding) {
}
stateDialog是在我的Alert类中创建的一个函数,我只希望这个函数对我的所有Binding活动都是通用的。我不想为我的每个绑定活动创建单独的函数。
这样我的这个功能就可以用于我的所有绑定活动,而不仅仅是 FragmentSendAlertBinding
这对你来说似乎很愚蠢,但我只是想知道有没有办法实现这一点。
感谢和安培;问候 我之前从未创建过泛型类,我只是尝试
任何帮助都会得到极大的赞赏!!!。
答案 0 :(得分:1)
我建议您为其创建界面
,而不是将其设为通用interface StateDialogListener{
public void onValueSelect(String value);
}
您的方法看起来
public static void stateDialog(Context context, String[] stateList, final StateDialogListener listener) {
mStateDialog = new Dialog(context);
mStateDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
mStateDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mStateDialog.show();
mStateDialog.setCancelable(false);
mStateDialog.setContentView(R.layout.dialog_select_state);
CardView cvState = (CardView) mStateDialog.findViewById(R.id.cv_state);
final ListView states = (ListView) mStateDialog.findViewById(R.id.countryList);
final TextView tvCancel = (TextView) mStateDialog.findViewById(R.id.tv_cancel);
final Animation myAnim = AnimationUtils.loadAnimation(context, R.anim.bounce);
MyBounceInterpolator interpolator = new MyBounceInterpolator(0.2, 20);
myAnim.setInterpolator(interpolator);
cvState.startAnimation(myAnim);
ArrayAdapter<String> itemList = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, stateList);
states.setAdapter(itemList);
states.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
listener.onValueSelect((String) arg0.getItemAtPosition(position));
//binding.tvState.setText((String) arg0.getItemAtPosition(position));
mStateDialog.dismiss();
}
});
tvCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mStateDialog.dismiss();
}
});
}
最后来自你所谓的任何地方
stateDialog(this,<your stateList>, new StateDialogListener(){
@Override
public void onValueSelect(String value){
binding.tvState.setText(value);
}
});
答案 1 :(得分:0)
为绑定活动创建父类。请注意,该类应具有您将从其子类的对象中使用的所有方法。然后创建扩展父绑定类的其他子类。
class ParentBinding{
public void method1();
public void method2();
public void method3();
}
Cusom Binding class
class BindingClass1 extends ParentBinding{
@override
public void method1(){
super();
}
@override
public void method1(){
super();
}
@override
public void method1(){
super();
}
}
然后您的方法可以写成
public static void stateDialog(Context context, String[] stateList, final ParentBinding binding) {