我的 onClickListener 在 getView 方法中工作。为什么?
My onClickListener work inside a getView method. Why?
我正在为 ListView 设置 onClickListener,一开始我在每个 activity 中都设置了它并且它起作用了。为了好玩,我想尝试在其他地方设置 onClickListener 以找到不止一种解决方案。所以我在arrayAdapter的getView方法中写了onCLickListener。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Checking if there is a View present for reusing if not inflate one.
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_view, parent, false);
}
//getting the current position of the word object in the View
final Word currentWord = getItem(position);
//getting the text view resource for setting our desired text.
TextView mivok = listItemView.findViewById(R.id.mivokTranslation);
mivok.setText(currentWord.getmMiwokTrans());
TextView defaultTran = listItemView.findViewById(R.id.defaultTranslation);
defaultTran.setText(currentWord.getmDefaultTrans());
ImageView imageView = listItemView.findViewById(R.id.image);
//Checking if the View has an Image resource, if yes then setting the correct image.
if (currentWord.checkImageResource == 0) {
imageView.setImageResource(currentWord.getmImageResource());
} else {
imageView.setVisibility(View.GONE);
}
// Set the theme color for the list item
View textContainer = listItemView.findViewById(R.id.text_container);
// Find the color that the resource ID maps to
int color = ContextCompat.getColor(getContext(), mColorResourceId);
// Set the background color of the text container View
textContainer.setBackgroundColor(color);
listItemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer = MediaPlayer.create(getContext(),currentWord.getmAudioResource());
mediaPlayer.start();
}
});
return listItemView;
}
虽然我这样做是行不通的。因为据我所知,getView 方法在我们 return 末尾的 listItemView 时在 listView 上设置了视图。但是它起作用了,clickListener 事件如何响应尚未添加的 activity。
在 getView 中,您只需为返回的视图分配一个侦听器。为什么它不起作用?分配侦听器时并不重要 - 它是匿名 class 的对象,视图 "saved" 供以后使用。
我正在为 ListView 设置 onClickListener,一开始我在每个 activity 中都设置了它并且它起作用了。为了好玩,我想尝试在其他地方设置 onClickListener 以找到不止一种解决方案。所以我在arrayAdapter的getView方法中写了onCLickListener。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Checking if there is a View present for reusing if not inflate one.
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_view, parent, false);
}
//getting the current position of the word object in the View
final Word currentWord = getItem(position);
//getting the text view resource for setting our desired text.
TextView mivok = listItemView.findViewById(R.id.mivokTranslation);
mivok.setText(currentWord.getmMiwokTrans());
TextView defaultTran = listItemView.findViewById(R.id.defaultTranslation);
defaultTran.setText(currentWord.getmDefaultTrans());
ImageView imageView = listItemView.findViewById(R.id.image);
//Checking if the View has an Image resource, if yes then setting the correct image.
if (currentWord.checkImageResource == 0) {
imageView.setImageResource(currentWord.getmImageResource());
} else {
imageView.setVisibility(View.GONE);
}
// Set the theme color for the list item
View textContainer = listItemView.findViewById(R.id.text_container);
// Find the color that the resource ID maps to
int color = ContextCompat.getColor(getContext(), mColorResourceId);
// Set the background color of the text container View
textContainer.setBackgroundColor(color);
listItemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer = MediaPlayer.create(getContext(),currentWord.getmAudioResource());
mediaPlayer.start();
}
});
return listItemView;
}
虽然我这样做是行不通的。因为据我所知,getView 方法在我们 return 末尾的 listItemView 时在 listView 上设置了视图。但是它起作用了,clickListener 事件如何响应尚未添加的 activity。
在 getView 中,您只需为返回的视图分配一个侦听器。为什么它不起作用?分配侦听器时并不重要 - 它是匿名 class 的对象,视图 "saved" 供以后使用。