什么是onItemClick中的父级和视图?

Fen*_*nil 2 java android onitemclicklistener onitemclick

我很难理解以下方法.在文档中,方法描述如下:

public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id)


Parameters:
parent      The AdapterView where the click happened.
view        The view within the AdapterView that was clicked (this will be a view provided by the adapter)
position     The position of the view in the adapter.
id           The row id of the item that was clicked.
Run Code Online (Sandbox Code Playgroud)

我理解最后两个,但无法理解parent这里有什么,为什么view需要?

如果有人有一个很好的解释,那么请让我理解.

Nad*_*haj 5

AdapterView可以是ListView,GridView,Spinner等.这在Java中称为泛型.您可以在代码中使用parent来对整个视图执行某些操作.例如,如果您使用的是ListView,则可以通过以下代码行隐藏整个ListView:

parent.setVisibility(View.GONE);
Run Code Online (Sandbox Code Playgroud)

视图引用AdapterView中的特定项.在ListView中,它是行.因此,您可以通过说出以下内容来获取对行内TextView的引用:

TextView myTextView = (TextView) view.findViewById(R.id.textView1);
String text = myTextView.getText().toString();
Run Code Online (Sandbox Code Playgroud)

  • 是的,它是父视图中的子视图,所以父视图是像listView这样的大视图,它的子视图是较小的视图,如父视图中的TextView或ImageView,在此示例中是ListView (2认同)