android fragment点击事件,Android Fragment onClick button Method

The others have already said that methods in onClick are searched in activities, not fragments. Nevertheless, if you really want it, it is possible.

Basically, each view has a tag (probably null). We set the root view's tag to the fragment that inflated that view. Then, it is easy to search the view parents and retrieve the fragment containing the clicked button. Now, we find out the method name and use reflection to call the same method from the retrieved fragment. Easy!

in a class that extends Fragment:

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_id, container, false);

OnClickFragments.registerTagFragment(rootView, this); // <========== !!!!!

return rootView;

}

public void onButtonSomething(View v) {

Log.d("~~~","~~~ MyFragment.onButtonSomething");

// whatever

}

all activities are derived from the same ButtonHandlingActivity:

public class PageListActivity extends ButtonHandlingActivity

ButtonHandlingActivity.java:

public class ButtonHandlingActivity extends Activity {

public void onButtonSomething(View v) {

OnClickFragments.invokeFragmentButtonHandlerNoExc(v);

//or, if you want to handle exceptions:

// try {

// OnClickFragments.invokeFragmentButtonHandler(v);

// } catch ...

}

}

It has to define methods for all xml onClick handlers.

com/example/customandroid/OnClickFragments.java:

package com.example.customandroid;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import android.app.Fragment;

import android.view.View;

public abstract class OnClickFragments {

public static class FragmentHolder {

Fragment fragment;

public FragmentHolder(Fragment fragment) {

this.fragment = fragment;

}

}

public static Fragment getTagFragment(View view) {

for (View v = view; v != null; v = (v.getParent() instanceof View) ? (View)v.getParent() : null) {

Object tag = v.getTag();

if (tag != null && tag instanceof FragmentHolder) {

return ((FragmentHolder)tag).fragment;

}

}

return null;

}

public static String getCallingMethodName(int callsAbove) {

Exception e = new Exception();

e.fillInStackTrace();

String methodName = e.getStackTrace()[callsAbove+1].getMethodName();

return methodName;

}

public static void invokeFragmentButtonHandler(View v, int callsAbove) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException {

String methodName = getCallingMethodName(callsAbove+1);

Fragment f = OnClickFragments.getTagFragment(v);

Method m = f.getClass().getMethod(methodName, new Class[] { View.class });

m.invoke(f, v);

}

public static void invokeFragmentButtonHandler(View v) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException {

invokeFragmentButtonHandler(v,1);

}

public static void invokeFragmentButtonHandlerNoExc(View v) {

try {

invokeFragmentButtonHandler(v,1);

} catch (NoSuchMethodException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

}

public static void registerTagFragment(View rootView, Fragment fragment) {

rootView.setTag(new FragmentHolder(fragment));

}

}

And the next adventure will be proguard obfuscation...

PS

It is of course up to you to design your application so that the data live in the Model rather than in Activities or Fragments (which are Controllers from the MVC, Model-View-Controller point of view). The View is what you define via xml, plus the custom view classes (if you define them, most people just reuse what already is). A rule of thumb: if some data definitely must survive the screen turn, they belong to Model.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值