Java 在片段中重新加载android视图
我正在使用Android Eclipse进行一个项目,在这个项目中,用户可以保存和上传带有图像的笔记 我有一个片段,其中包含一个文本视图和一些缩略图。用户可以使用相机添加其他图像,也可以通过查看、单击单独活动中的图像并将其删除来删除图像 我的问题是刷新片段的布局以反映已删除的图像。目前正在调用以下函数来处理此问题:Java 在片段中重新加载android视图,java,android,eclipse,android-fragments,view,Java,Android,Eclipse,Android Fragments,View,我正在使用Android Eclipse进行一个项目,在这个项目中,用户可以保存和上传带有图像的笔记 我有一个片段,其中包含一个文本视图和一些缩略图。用户可以使用相机添加其他图像,也可以通过查看、单击单独活动中的图像并将其删除来删除图像 我的问题是刷新片段的布局以反映已删除的图像。目前正在调用以下函数来处理此问题: public ViewGroup removeNoteImageFromView(String location) { List<String> imageLin
public ViewGroup removeNoteImageFromView(String location) {
List<String> imageLinks = new ArrayList<String>();
for (String string : imageLocations) {
if (string.equalsIgnoreCase(location)) {
break;
}
imageLinks.add(string);
}
//REMOVE ALL THE IMAGEVIEWS from the Linear Layout
LinearLayout linear = (LinearLayout) rootView.findViewById(R.id.imageContainer);
if (linear.getChildCount() != 0) {
linear.removeAllViewsInLayout();
linear.refreshDrawableState();
linear.postInvalidate();
}
imageLocations.addAll(imageLinks);
//Add them all back from the new array
String root = Environment.getExternalStorageDirectory().toString();
for (String string : imageLinks) {
Bitmap myBitmap = BitmapFactory.decodeFile((new File(root +"/saved_images/"+string)).getAbsolutePath());
addImage(myBitmap);
}
return linear;
}
这段代码所做的就是隐藏linear.removeAllViewsInLayout调用的所有缩略图。然而,下面两行是我认为会在屏幕上重新加载布局的内容,但它们似乎没有任何效果
请注意,我已经尝试了linear.invalidate以及postInvalidate,但仍然一无所获
正确的图像将从设备中删除,因为这将在其他地方处理,因此当我通过在菜单中重新选择它返回到该片段时,所有内容都将正确显示。替换此:
linear.refreshDrawableState();
linear.postInvalidate();
为此:
linear.requestLayout();
好吧,我终于明白了 我觉得有点愚蠢,因为代码工作正常,但根路径不正确,因此找不到要添加回视图的图像
感谢您的回复和帮助。对不起,我的新朋友,我只正确地使用Java几个星期。我仍然不担心:/此代码毫无意义,因为您已将imageLocations中的字符串添加到imageLinks,即imageLinks.addstring;然后再次读取相同字符串到imageLocations imageLocations.addAllimageLinks;在阅读之前,您似乎跳过了清除列表。@humblerookie问题是我对android/java开发还不熟悉,这是我的第一个项目,在我之前有几个开发人员参与过,所以大部分代码都不是我的:/Hey我理解,但是,为什么要从listimagelocations本身向列表imagelocations添加另一个字符串。我正在思考上面代码使用的这个逻辑。只需通过代码观察您对imageLocation列表所做的操作就足够了。只有以前的开发人员才能回答这个问题,我想我暗示的是,您需要执行removeAll而不是addAll,即imageLocations.addAllimageLinks到imageLocations.removeAllimageLinks和for String:imageLinks到for String:imageLocations我无法得到这一点来改变。我试着将我所有的代码从RemoveNoteMageFromView放到这个视图中,并调用它,但没有区别。这是我需要做的还是我是个白痴?
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Your code is here...
// for refresh view use..
convertView.invalidate();
}