我正在使用下面的代码来遍历我的列表,如果List Items可见,它工作正常.
如果List是可滚动的,那么使用此代码不会访问非可见项.如何遍历所有可见+不可见项目的列表项.
for(int i=0;i<list.getCount;i++)
{
View v = list.getChildAt(i);
TextView tv= (TextView) v.findViewById(R.id.item);
Log.d("","ListItem :"+tv.getText());
}
Run Code Online (Sandbox Code Playgroud)
最近我做了这件事。假设我想隐藏列表项上的按钮。然后在列表适配器的 getView 中将该按钮添加到全局向量中。像下面这样。
Button del_btn = viewCache.getFrame();
view_vec.add(del_btn);
Run Code Online (Sandbox Code Playgroud)
这里 viewCache 是 ViewCache 类的一个对象,它的总和如下 -
class ViewCache
{
private View baseView;
private Button button;
public ViewCache(View baseView)
{
this.baseView = baseView;
}
public Button getButton() {
if(button == null) {
button = (Button) baseView.findViewById(R.id.DeleteChatFrndBtn);
}
return button;
}
}
//it is necessary sometimes because otherwise in some cases the list scroll is slow.
Run Code Online (Sandbox Code Playgroud)
现在您想在单击其他按钮时看到列表项的按钮。那么代码就像下面这样——
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.EditChatFrndBtn:
length = view_vec.size();
for(int i = 0; i < length; i++) {
Button btn = (Button) view_vec.elementAt(i);
btn.setVisibility(View.VISIBLE);
}
doneBtn.setVisibility(View.VISIBLE);
editBtn.setVisibility(View.INVISIBLE);
break;
}
}
Run Code Online (Sandbox Code Playgroud)
而不是 R.id.EditChatFrndBtn 将您的按钮 ID 放在单击上,您将不可见/可见 listItem 的按钮。
| 归档时间: |
|
| 查看次数: |
44212 次 |
| 最近记录: |