lac*_*ker 107 android android-listview
我需要找出使用a显示的列表中一个元素的像素位置ListView.看起来我应该得到一个TextView然后使用getTop(),但我无法弄清楚如何获得一个子视图ListView.
更新:对于a,子ViewGroup项与列表中的项目不一一对应ListView.相反,他们ViewGroup的孩子只对应那些现在可见的视图.因此getChildAt(),对内部的索引进行操作,ViewGroup并且不一定与ListView使用的列表中的位置有任何关系.
Joe*_*Joe 217
请参阅:Android ListView:获取可见项目的数据索引 并结合上面Feet的部分答案,可以给你类似的东西:
int wantedPosition = 10; // Whatever position you're looking for
int firstPosition = listView.getFirstVisiblePosition() - listView.getHeaderViewsCount(); // This is the same as child #0
int wantedChild = wantedPosition - firstPosition;
// Say, first visible position is 8, you want position 10, wantedChild will now be 2
// So that means your view is child #2 in the ViewGroup:
if (wantedChild < 0 || wantedChild >= listView.getChildCount()) {
Log.w(TAG, "Unable to get view for desired position, because it's not being displayed on screen.");
return;
}
// Could also check if wantedPosition is between listView.getFirstVisiblePosition() and listView.getLastVisiblePosition() instead.
View wantedView = listView.getChildAt(wantedChild);
Run Code Online (Sandbox Code Playgroud)
好处是你不会迭代ListView的子节点,这可能会影响性能.
Kal*_*mah 17
此代码更易于使用:
View rowView = listView.getChildAt(viewIndex);//The item number in the List View
if(rowView != null)
{
// Your code here
}
Run Code Online (Sandbox Code Playgroud)
小智 5
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
View v;
int count = parent.getChildCount();
v = parent.getChildAt(position);
parent.requestChildFocus(v, view);
v.setBackground(res.getDrawable(R.drawable.transparent_button));
for (int i = 0; i < count; i++) {
if (i != position) {
v = parent.getChildAt(i);
v.setBackground(res.getDrawable(R.drawable.not_clicked));
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
基本上,创建两个drawable - 一个是透明的,另一个是所需的颜色.请求焦点在单击的位置(定义的int位置)并更改所述行的颜色.然后遍历父级int position,并相应地更改所有其他行.这说明了用户ListView多次点击的时间.这是通过自定义布局完成的listview.(非常简单,只是一个新的布局文件,带有ListView- 不设置可聚焦或可点击!)无需自定义适配器 - 使用阵列适配器
| 归档时间: |
|
| 查看次数: |
131747 次 |
| 最近记录: |