您好我想使用此代码,它正在onclick方法中工作,因为(View v)但我如何在活动的其他地方使用此代码?
这是代码
ListView list= getListView();
int position = list.getPositionForView(v);
switch (position) {
case 1:
ImageView arrow = (ImageView) getView().findViewById(R.id.arrow);
arrow.setVisibility(View.VISIBLE);
break;
default:
break;
}
那么如何在这种情况下获取View v?
提前致谢!
答案 0 :(得分:0)
我有同样的问题。我不确定这种方式是最好的方式,但对我来说效果很好。创建所有视图后,创建侦听器 onLayoutChangeListener ,并将要为项目视图实现的逻辑放在所需的特定位置。
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
setOnLayoutChangeListener(getListView());
super.onViewCreated(view, savedInstanceState);
}
private void setOnLayoutChangeListener(final ListView listview) {
if (listview != null) {
listview.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
changeFirstListItemView(listview);
}
});
}
}
private void changeFirstListItemView(final ListView listview) {
if (listview != null) {
try {
int listFirstPosition = 1;
View view = listview.getChildAt(listFirstPosition);
if (view != null) {
setHiddenViewVisible(view);
}
} catch (Exception e) {
// Print some debug info.
String msg = "An error occurred while trying to set onFocusChange listener.";
Log.e(TAG, msg, e);
}
}
}
private void setHiddenButtonVisible(View listItem) {
Button button = null;
try {
buttonId = R.id.listButton;
button = (Button) listItem.findViewById(buttonId);
} catch (Resources.NotFoundException nfe) {
// Print some debug info.
}
if (button != null) {
button.setVisibility(View.VISIBLE);
// Optional, add here button listener, or something else...
}
}