我有一个与Google教程非常相似的GridView,除了我想在运行时添加ImageViews(通过子活动)。结果还可以,但View的布局搞砸了:GridView没有填充其父级的内容,我需要做些什么来正确设计它?
这里是添加孩子的代码:
public void initializeWorkbench(GridView gv, Vector<String> items) {
Prototype.workbench.setDimension(screenWidth, divider.height()+workbenchArea.height());
Prototype.workbench.activateWorkbench();
// this measures the workbench correctly
Log.d(Prototype.TAG, "workbench width: "+Prototype.workbench.getMeasuredWidth());
// 320
Log.d(Prototype.TAG, "workbench height: "+Prototype.workbench.getMeasuredHeight());
// 30
ImageAdapter imgAdapter = new ImageAdapter(this.getContext(), items);
gv.setAdapter(imgAdapter);
gv.measure(screenWidth, screenHeight);
gv.requestLayout();
gv.forceLayout();
Log.d(Prototype.TAG, "gv width: "+gv.getMeasuredWidth());
// 22
Log.d(Prototype.TAG, "gv height: "+gv.getMeasuredHeight());
// 119
Prototype.workbench.setDimension(screenWidth, divider.height()+workbenchArea.height());
}
}
在工作台中激活Workbench,setDimension和measure(GridView上方的LinearLayout):
public void activateWorkbench() {
if(this.equals(Prototype.workbench)) {
this.setOrientation(VERTICAL);
show = true;
measure();
}
}
public void setDimension(int w, int h) {
width = w;
height = h;
this.setLayoutParams(new LinearLayout.LayoutParams(width, height));
this.invalidate();
}
private void measure() {
if (this.getOrientation() == LinearLayout.VERTICAL) {
int h = 0;
int w = 0;
this.measureChildren(0, 0);
for (int i = 0; i < this.getChildCount(); i++) {
View v = this.getChildAt(i);
h += v.getMeasuredHeight();
w = (w < v.getMeasuredWidth()) ? v.getMeasuredWidth() : w;
}
if (this.equals(Prototype.tagarea))
height = (h < height) ? height : h;
if (this.equals(Prototype.tagarea))
width = (w < width) ? width : w;
}
this.setMeasuredDimension(width, height);
}
ImageAdapter构造函数:
public ImageAdapter(Context c, Vector<String> items) {
mContext = c;
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but
// all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
if (mExternalStorageAvailable && mExternalStorageWriteable) {
for (String item : items) {
File f = new File(item);
if (f.exists()) {
try {
FileInputStream fis = new FileInputStream(f);
Bitmap b = BitmapFactory.decodeStream(fis);
bitmaps.add(b);
files.add(f);
} catch (FileNotFoundException e) {
Log.e(Prototype.TAG, "", e);
}
}
}
}
}
xml布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="bottom"
android:paddingLeft="0px"
android:paddingTop="0px"
android:paddingRight="0px">
<com.unimelb.pt3.ui.TransparentPanel
android:id="@+id/workbench"
android:layout_width="fill_parent"
android:layout_height="10px"
android:paddingTop="0px"
android:paddingLeft="0px"
android:paddingBottom="0px"
android:paddingRight="0px">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />
</com.unimelb.pt3.ui.TransparentPanel>
</LinearLayout>
答案 0 :(得分:41)
GridView有一个invalidateViews()方法。
当你调用这个方法时:“所有要重建和重绘的视图。” http://developer.android.com/reference/android/widget/GridView.html
我认为这就是你所需要的:)
答案 1 :(得分:33)
您必须首先告诉适配器通知数据已更改并再次将适配器设置为网格
adapter.notifyDataChanged();
grid.setAdapter(adapter);
答案 2 :(得分:18)
这可能会有所帮助。 在对项目执行删除后刷新书籍图像缩略图的网格视图。 使用adapter.notifyDataChanged();如上所述对我不起作用,因为它在我的适配器中被调用。
//this is a call that retrieves cached data.
//your constructor can be designed and used without it.
final Object data = getLastNonConfigurationInstance();
我基本上只需重新加载适配器并将其绑定到同一视图。
//reload the adapter
adapter = new BooksAdapter(MyBooks.this, MyBooks.this, data, show_collection );
grid.invalidateViews();
grid.setAdapter(adapter);
答案 3 :(得分:10)
@flyerz @snagnever
你们一起得到它。它应该是:
adapter.notifyDataChanged();
grid.invalidateViews();
这将标记其数据已更改的适配器,然后在调用invalidateViews()方法后将其传播到网格。
很高兴我发现了这个问题,因为我无法弄清楚如何在渲染后将项目添加到网格中。
答案 4 :(得分:9)
这些答案中没有一个对我有用,我不得不将它们全部混合在一起。要实际让GridView更新,您需要这样做:
adapter.notifyDataChanged();
grid.invalidateViews();
grid.setAdapter(adapter);
希望这可以帮助任何无法获得其他解决方案的人。
答案 5 :(得分:5)
您不应该致电invalidateViews
和setAdapter
来刷新grid view
。保持grid view
更新不是一个好主意,如果以这种方式更新,则会花费大量时间和内存。
如果您有机会查看getView
方法,您会看到convertView
只创建一次。当您致电notifyDataChanged
时,它会更新此视图。而如果您致电invalidateViews
,则会重新创建以前创建的视图。这不是一个好的解决方案。
致电getView
时,系统会调用notifyDataChanged
方法。所以你的getView方法应该类似于下面的代码。
public List list;
public class SimpleItemViewHolder extends Object
{
public TextView textView;
public ImageView imageView;
}
public View getView(int position, View convertView, ViewGroup parent){
View itemView = convertView;
SimpleItemViewHolder viewHolder;
if(convertView==null)
{
viewHolder = (SimpleItemViewHolder)itemView.getTag();
}else{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.layout_name, null);
TextView labelField = (TextView) itemView.findViewById(R.id.label_field);
labelField.setText(list.get(position).Name);
//Typeface boldFont = Typeface.createFromAsset(context.getAssets(), "fonts/Font-Bold.ttf");
//labelField.setTypeface(boldFont);
ImageView imageView = (ImageView) itemView.findViewById(R.id.image_view);
//Bitmap bitmap = init your bitmap here;
//imageView.setImageBitmap(bitmap);
viewHolder = new SimpleItemViewHolder();
viewHolder.imageView = imageView;
viewHolder.textView = labelField;
itemView.setTag(viewHolder);
}
//don't create new views, instead use previous ones and update them.
viewHolder.textView.setText(list.get(position).Name);
//viewHolder.imageView.setImageBitmap(your_bitmap);
return itemView;
}
答案 6 :(得分:2)
adapter.notifyDataChanged();
可能不起作用,因为适配器的数据是陈旧的(如果活动或片段没有被销毁,并且例如已经坐在后面的堆栈中)。
因此,如果首先刷新数据,那么它将在工作之后。
答案 7 :(得分:0)
您将GridView
放在com.unimelb.pt3.ui.TransparentPanel
高10px
内。
px
,而应使用dp
。com.unimelb.pt3.ui.TransparentPanel
的{{1}}更改为android:layout_height="10px"
答案 8 :(得分:0)
在适配器中:
class MAdapter extends BaseAdapter{
List<Objects> mObjects;
...
public void clearAdapter(){
mObjects.clear();
}
public void addNewValues(List<Objects> mObjects){
this.mObjects = mObjects;
}
...
}
adapter.clearAdapter(); // clear old values
adapter.addNewValues(MObjects);
adapter.notifyDataChanged();