在自定义视图中覆盖空方法并在 MainActivity 中处理它
override empty method in custom view and handle it in MainActivity
为了简单起见,我制作了一个自定义日历视图 (RelativeLayout)。事件(linearLayouts)放在里面。当我单击该事件时,我希望能够做一些事情,但我希望这件事在我的 MainActivity 中完成。换句话说,我想覆盖自定义视图中的空方法并在我的 MainActivity 中处理它。
eventView.setTag(event.getDatabaseId());
eventView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d("test", "" + v.getTag());
onEventClick((int) v.getTag());
}
});
eventView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
onEventLongClick((int) v.getTag());
return false;
}
});
}
...
public void onEventClick(int eventId) {
}
public void onEventLongClick(int eventId) {
}
所以在我的 MainActivity 中我实例化了我的视图:
cv = ((CalendarView)findViewById(R.id.calendar_view));
我想做类似的事情:
cv = ((CalendarView)findViewById(R.id.calendar_view)) {
@Override
public void onEventClick(int eventId) {
// here I will fetch the data and display a dialog
}
@Override
public void onEventLongClick(int eventId) {
// here I will fetch the data and display a dialog
}
};
我希望能够在其他地方使用我的日历,所以这就是为什么我不想在我的自定义视图中绑定点击功能
我试着把我的观点抽象化,但也没成功。
编辑:eventView 在 viewpager 中
您应该在自定义视图上使用 Interface
。
In its most common form, an interface is a group of related methods with empty bodies. You can find more detailed instructions here
所以在你的情况下,我假设你有一个 RelativeLayout
作为你的自定义视图
public class MyCalendarLayout extends RelativeLayouts{
//Your constructor methods
public interface MyEventListener {
public void onEventClicked();
public void onEventLongClicked();
}
private MyEventListener myEventListener;
public void setListener(MyEventListener myEventListener) {
this.myEventListener= myEventListener;
}
}
然后在你的 Activity
上你可以像这样使用它
myCustomView.setListener(new MyEventListener() {
@Override
public void onEventClicked() {
// TODO Auto-generated method stub
}
@Override
public void onEventLongClicked() {
// TODO Auto-generated method stub
}
});
或者您可以使用
YourActivity extends Activity implements MyEventListener
你可以打电话给
myCustomView.setListener(this);
在你的 activity 上,让句柄覆盖你的 activity 方法。
为了简单起见,我制作了一个自定义日历视图 (RelativeLayout)。事件(linearLayouts)放在里面。当我单击该事件时,我希望能够做一些事情,但我希望这件事在我的 MainActivity 中完成。换句话说,我想覆盖自定义视图中的空方法并在我的 MainActivity 中处理它。
eventView.setTag(event.getDatabaseId());
eventView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d("test", "" + v.getTag());
onEventClick((int) v.getTag());
}
});
eventView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
onEventLongClick((int) v.getTag());
return false;
}
});
}
...
public void onEventClick(int eventId) {
}
public void onEventLongClick(int eventId) {
}
所以在我的 MainActivity 中我实例化了我的视图:
cv = ((CalendarView)findViewById(R.id.calendar_view));
我想做类似的事情:
cv = ((CalendarView)findViewById(R.id.calendar_view)) {
@Override
public void onEventClick(int eventId) {
// here I will fetch the data and display a dialog
}
@Override
public void onEventLongClick(int eventId) {
// here I will fetch the data and display a dialog
}
};
我希望能够在其他地方使用我的日历,所以这就是为什么我不想在我的自定义视图中绑定点击功能
我试着把我的观点抽象化,但也没成功。
编辑:eventView 在 viewpager 中
您应该在自定义视图上使用 Interface
。
In its most common form, an interface is a group of related methods with empty bodies. You can find more detailed instructions here
所以在你的情况下,我假设你有一个 RelativeLayout
作为你的自定义视图
public class MyCalendarLayout extends RelativeLayouts{
//Your constructor methods
public interface MyEventListener {
public void onEventClicked();
public void onEventLongClicked();
}
private MyEventListener myEventListener;
public void setListener(MyEventListener myEventListener) {
this.myEventListener= myEventListener;
}
}
然后在你的 Activity
上你可以像这样使用它
myCustomView.setListener(new MyEventListener() {
@Override
public void onEventClicked() {
// TODO Auto-generated method stub
}
@Override
public void onEventLongClicked() {
// TODO Auto-generated method stub
}
});
或者您可以使用
YourActivity extends Activity implements MyEventListener
你可以打电话给
myCustomView.setListener(this);
在你的 activity 上,让句柄覆盖你的 activity 方法。