将自定义适配器列表视图中的所有元素设置为Visible / Invisible

时间:2012-09-20 09:46:01

标签: android android-listview visibility custom-adapter

我正在尝试让Button更改所有Listview元素中单个ImageButton的可见性。我遇到的问题是找到正确的访问元素,因为我没有一种简单的方法来访问自定义适配器中的特定元素(我认为)

相关代码部分:

 public View getView(final int position, final View convertView, final ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
    {
        vi = inflater.inflate(R.layout.custom_row_view, null);
    }

/*
 other non important view stuff
*/
if(deleteButton != null){
        deleteButton.setVisibility(View.INVISIBLE);
        deleteButton.setTag(position);
        deleteButton.setOnClickListener(deleteListener);
    }

    return vi;
}

public void setVisibility()
{
    if(deleteButton.getVisibility() == View.INVISIBLE){
        deleteButton.setVisibility(View.VISIBLE); //Here i  want to access all elements
        Log.i("setVisibility", "Changing to "+ deleteButton.getVisibility() + " element: " + deleteButton.getTag()); 
//Logcat output: Changing to 0 element: 2


    } else if(deleteButton.getVisibility() == View.VISIBLE){
        deleteButton.setVisibility(View.INVISIBLE); 
        Log.i("setVisibility", "Changing to "+ deleteButton.getVisibility() + " element: " + deleteButton.getTag());
//Logcat output: Changing to 4 element: 2

    }
}

我在主要活动中使用的东西:

CustomAdapter listAdapter;
ListView listViewLeft;
ListView listViewRight;

OnCreate:
listAdapter=new CustomAdapter(this, link, names, names2, buttonText,context, plistArray); 
listViewLeft.setAdapter(listAdapter);
listViewRight.setAdapter(listAdapter);

我只使用以下方法调用该方法:

OnClickListener editListener = new OnClickListener() {
    public void onClick(View view) {
        listAdapter.setVisibility();
       }
 }

如果有人有任何想法,那么我真的很感激,谢谢:)

1 个答案:

答案 0 :(得分:0)

这是一个非常简单的修复。我忘了打电话了:

listAdapter.notifiDataSetChanged(); 

但每次调用该方法时,都会调用getView方法,所以我只是在自定义适配器中添加了这个方法getView

if(deleteButton != null){
        if(deleteButton.getVisibility() == View.INVISIBLE){
            deleteButton.setVisibility(View.VISIBLE);
        } else if(deleteButton.getVisibility() == View.VISIBLE){
            deleteButton.setVisibility(View.INVISIBLE); 
            }
        deleteButton.setTag(position);
        deleteButton.setOnClickListener(deleteListener);
    }

并将XML中的默认值设置为visible。 我不确定这是否是最有效的方法,但它至少可以起作用:)