我的应用中有一个ListView。它使用名为file_row.xml
的XML加载每一行,其中包含:
我想按一下actionBar按钮,右边的所有图标都将不可见。 我在第一行得到了这个结果,但我不能用其余的。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.file_explorer, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//ID icons
aDel = (ImageView)findViewById(R.id.aDelete);
aShare = (ImageView)findViewById(R.id.aShare);
aView = (ImageView)findViewById(R.id.aDetails);
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
return true;
case R.id.menu_delete:
aDel.setVisibility(View.INVISIBLE);
aShare.setVisibility(View.INVISIBLE);
aView.setVisibility(View.INVISIBLE);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
问题是我的ListView(一个用于行)的所有'删除图标'都具有相同的名称(R.id.aDelete),因为所有都是从file_row.xml
加载setAdapter()
的{{1}} }} 方法。我不明白所有图标是否都具有相同的名称,因为当我放置aDel.setVisibility(View.INVISIBLE);
时它们不可见,而第一个图标只是这样做。
答案 0 :(得分:2)
试试这个。你必须浏览listview
内的所有视图,然后设置所需的视图。
例如:
for( i=0;i<listView.getChildCount();i++){
listView.getChildAt(i).findViewById(R.id.aDelete).setVisibility(View.INVISIBLE);
}
根据您的意愿使用View.INVISIBLE或View.GONE
答案 1 :(得分:1)
如果我理解你的话,你想点击一个按钮并切换显示没有图标的列表视图项目吗?
您的方法可能会失败,因为您要求活动找到带有findViewById()
的图标,并且它将返回带有该ID的第一个图标。但是每个列表项都有一个带有该ID的图标。
切换到for循环并查找所有内容将无法正常工作,因为listview会动态创建列表项视图。每当项目进入屏幕上时,就会创建(或回收)列表项视图。
您必须制作自己的自定义listadapter,根据您当前的设置创建带有可见或不可见图标的列表项视图。以下是从ArrayAdapter<String>
扩展的示例。我不知道你正在使用什么类型的适配器,但你可以继承它并覆盖getView()
方法,根据你用菜单项处理程序切换的一些设置来设置图标可见性。
在您的活动类中应该看起来像这样:
<强>更新强> 将showIcons从活动移动到适配器。
private ListView listView;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new MyAdapter(this, getYourData());
listView = (ListView) findViewById(R.id.yourListView);
listView.setAdapter(adapter);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
return true;
case R.id.menu_delete:
adapter.hideIcons();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private class MyAdapter extends ArrayAdapter<String> {
private boolean showIcons = true;
public MyAdapter(Context context, List<String> objects) {
super(context, R.layout.yourItemView, objects);
}
public void hideIcons() {
// toggle setting to not show icons
showIcons = false;
// trigger recreating the list items, this time with inivisble icons
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// get the view through the normal ArrayAdapter mechanism
View view = super.getView(position, convertView, parent);
// set the icon visibility according to your the setting
ImageView aDel = (ImageView) item.findViewById(R.id.aDelete);
aDel.setVisibility(showIcons ? View.VISIBLE : View.INVISIBLE);
// do the other images as well
}
}
答案 2 :(得分:-1)
如果您使用任何自定义适配器,请在那里设置标准。如果您仍然感到困惑,请发布适配器类的完整代码,以便我可以尝试更好地指导