Detailed explanation of the example of adding monitoring method for controls in Android ListView
Regarding ListView, it is a common control in Android. In ListView, we usually need a template. This template does not refer to the module, but configures the things displayed in ListView. When we were working on the project today, we found that we wanted to add an ImageView listening method, but found that it crashed. Maybe it was because we hadn't moved for a long time. I forgot that we could not call other xml files controls directly in the xml file of the main UI, even if the ListView uses this xml file.
[Error demonstration]:
The control called ImageView is a ListView control, which is called directly in the non-main View java class file, and it crashes directly. I didn't know about this problem before.
msg=(ImageView).findViewById(); (new OnClickListener() { } });
[How to get it]:
public class CallListen implements OnItemClickListener{ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { msg=(ImageView)(); (new OnClickListener() { @Override public void onClick(View v) { } } } }
Use the control's listening method in the ListView listening method. The most important thing is to click ListView to get the parent class View of all controls inside.
In this way, we can get our control through msg=(ImageView)();. The view here is crucial, and there is a knowledge involved. What if my activity setcontentview is not the View I want to use the control at the moment?
The function of LayoutInflater is to instantiate the layout's xml layout file into a View class object.
There are three ways to obtain LayoutInflater:
LayoutInflater inflater=(LayoutInflater)(Context.LAYOUT_INFLATER_SERVICE); View layout = (, null); LayoutInflater inflater = (context); (This method is essentially the first method,Refer to the source code) View layout = (, null); LayoutInflater inflater = getLayoutInflater();(existActivityCan be used in,Actually it isViewUnder subclasswindowA function of) View layout = (, null);
At this time, you may be confused:
setContentView andInflate
the difference:
Once setContentView() is called, layout will immediately display the UI; and inflate will only form a Layout to an object implemented as a view class, and use setContentView(view) to display it when necessary. Generally, the interface is displayed through setContentView() in the activity, but if you set the control layout in a non-activity, it requires LayoutInflater to load dynamically.
The above is an example of adding events to Android ListView. If you have any questions, please leave a message or go to the community of this site to communicate and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!