[英]What is the correct approach / logic to access the activity which inflates a layout with custom view from this view?
I'm developing an application in which I have a dashboard with 4 buttons. 我正在开发一个具有4个按钮的仪表板的应用程序。 Each button starts a new Intent to a different Activity.
每个按钮为不同的活动启动一个新的Intent。 Each Activity inflates the same layout that consists of the custom view.
每个活动都会放大包含自定义视图的相同布局。 In the custom view is a ViewPager.
在自定义视图中是一个ViewPager。 In the ViewPager I want to display a strings that resides in the arrays in the mentioned above activities.
在ViewPager中,我想显示一个字符串,该字符串驻留在上述活动中的数组中。 Should I pass those arrays to the custom view and display it in the ViewPager?
我应该将这些数组传递到自定义视图并在ViewPager中显示它吗? If so how can I pass them?
如果可以,我如何通过他们? How the Custom View would know from which activity they come from?
自定义视图如何知道它们来自哪个活动? I know that the 'sender' activity will be the one that is currently running but how can I check it?
我知道“发件人”活动将是当前正在运行的活动,但是我如何检查呢? Or should I just make the arrays static and easily access them?
还是我应该使数组静态并轻松访问它们? In this case I would also need to know which activity to access.
在这种情况下,我还需要知道要访问哪个活动。 Please advise or maybe there is a better way of implementing it.
请提出建议,或者也许有更好的方法来实施它。 Thank you.
谢谢。
Intents let you start other activities , also allows you to pass primitives, primitive arrays/Lists as well as custom Parcelable
objects to other activities. 使用Intents可以启动其他活动 ,也可以将基元,基元数组/列表以及自定义的
Parcelable
对象传递给其他活动。
You can pass array data, starter activity name etc here. 您可以在此处传递数组数据,入门活动名称等。 Or you can use getCallingActivity() to know the sender activity.
或者,您可以使用getCallingActivity()来了解发送者的活动。
UPDATE: 更新:
You can pass data to custom views in onCreate()
, if You have setter methods in Custom View class. 如果在Custom View类中有setter方法,则可以在
onCreate()
中将数据传递到自定义视图。
For more complex communication, Interface your activity to its child view as shown: 对于更复杂的通信,请如图所示将您的活动与其子视图接口:
Interface: 接口:
public interface CustomViewParent {
//----add as many communication methods you want---
public String[] getData();
public void doSomething();
}
Activity: 活动:
public class MyActivity extends Activity implements CustomViewParent {
// ------------------------ INTERFACE METHODS ------------------------
// --------------------- Interface CustomViewParent ---------------------
@Override
public String[] getData() {
return new String[]{"data1","data2"};
}
@Override
public void doSomething() {
Toast.makeText(this,"Custom view called me !",Toast.LENGTH_SHORT).show();
}
// -------------------------- OTHER METHODS --------------------------
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//----here we set the parent--
((CustomView)findViewById(R.id.my_custom_view)).setParent(this);
}
}
Accept and use this interface in Custom View: 在“定制视图”中接受并使用此界面:
public class CustomView extends ViewPager {
// ------------------------------ FIELDS ------------------------------
private CustomViewParent parent;
// --------------------------- CONSTRUCTORS ---------------------------
public CustomView(Context context) {
super(context);
init();
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
// --------------------- GETTER / SETTER METHODS ---------------------
public void setParent(CustomViewParent parent) {
this.parent = parent;
}
// ---------------------------- INITIALIZE-----------------------------
private void init() {
//-----initialize/inflate custom views-----
//-----calls to parent, !! do not forget the null check !! ----
if(parent != null){
String[] data = parent.getData();
parent.doSomething();
}
//----add data from parent to Views etc---
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.