[英]android fragment - how to inflate the layout and then make the UI functions
I have a Fragment
instance, and I would like to be able to implement click listeners and other attributes after the fragment is loaded and shown on screen. 我有一个
Fragment
实例,我希望能够在片段加载并在屏幕上显示后实现点击侦听器和其他属性。 How can I achieve this? 我怎样才能做到这一点? Where should I be doing this implenentation?
我应该在哪里做这个实施?
Try the following: 请尝试以下方法:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.rap_prog_fields, container, false);
TextView tv = (TextView) v.findViewById(R.id.text_view_1);
Button b = (Button) v.findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//do stuff
}
});
//the rest of your views... in the same manner
return v;
}
You can also find your views after returning from onCreateView
like such: 从
onCreateView
返回后,您还可以找到您的视图,如下所示:
public void onStart() {
super.onStart();
Button b = (Button) getView().findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//do stuff
}
});
//Everything else...
}
TO complete actions once the view is displayed and on the screen you need to implement the following: 要在显示视图后完成操作,您需要在屏幕上实现以下操作:
final ViewTreeObserver observer= button.getViewTreeObserver(); //you need an anchor view here that will be drawn via xml
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//do stuff here.....
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.