Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何刷新GridView?_Android_Xml_Gridview_Layout - Fatal编程技术网

Android 如何刷新GridView?

Android 如何刷新GridView?,android,xml,gridview,layout,Android,Xml,Gridview,Layout,我有一个GridView,它与Google教程非常相似,只是我想在运行时添加ImageView(通过子活动)。结果是可以的,但是视图的布局是混乱的:GridView没有填充其父视图的内容,我该怎么做才能正确地设计它 下面是添加子项的代码: public void initializeWorkbench(GridView gv, Vector<String> items) { Prototype.workbench.setDimension(screenWidth, d

我有一个GridView,它与Google教程非常相似,只是我想在运行时添加ImageView(通过子活动)。结果是可以的,但是视图的布局是混乱的: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());
    }
}
public void initializeWorkbench(GridView gv,向量项){
Prototype.workbench.setDimension(屏幕宽度、分隔器.height()+工作台harea.height());
Prototype.workbench.activateWorkbench();
//这可以正确地测量工作台
Log.d(Prototype.TAG,“工作台宽度:+Prototype.workbench.getMeasuredWidth());
// 320
Log.d(Prototype.TAG,“workbench height:+Prototype.workbench.getMeasuredHeight());
// 30
ImageAdapter imgAdapter=新的ImageAdapter(this.getContext(),items);
gv.设置适配器(imgAdapter);
gv.测量(屏幕宽度、屏幕高度);
gv.requestLayout();
gv.forceLayout();
Log.d(Prototype.TAG,“gv-width:+gv.getMeasuredWidth());
// 22
Log.d(Prototype.TAG,“gv高度:+gv.getMeasuredHeight());
// 119
Prototype.workbench.setDimension(屏幕宽度、分隔器.height()+工作台harea.height());
}
}
激活工作台,在工作台中设置尺寸和测量(网格视图上方的线性布局):

public void activateWorkbench(){
if(this.equals(Prototype.workbench)){
此设置方向(垂直);
show=true;
度量();
}
}
公共void setDimension(int w,int h){
宽度=w;
高度=h;
this.setLayoutParams(新的LinearLayout.LayoutParams(宽度、高度));
这个。使无效();
}
私人无效措施(){
if(this.getOrientation()==LinearLayout.VERTICAL){
int h=0;
int w=0;
这是被测儿童(0,0);
for(int i=0;i
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);
                }
            }
        }
    }
}
公共ImageAdapter(上下文c、向量项){ mContext=c; 布尔值mExternalStorageAvailable=false; 布尔值mExternalStorageWriteable=false; String state=Environment.getExternalStorageState(); if(环境、介质、安装等于(状态)){ //我们可以读写媒体 mExternalStorageAvailable=mExternalStorageWriteable=true; }else if(Environment.MEDIA\u MOUNTED\u READ\u ONLY.equals(state)){ //我们只能阅读媒体 mExternalStorageAvailable=true; mExternalStorageWriteable=false; }否则{ //还有一些问题。它可能是许多其他州中的一个,但是 //我们所需要的 //要知道,我们既不会读也不会写 mExternalStorageAvailable=mExternalStorageWriteable=false; } if(mExternalStorageAvailable&&mExternalStorageWriteable){ 用于(字符串项:项){ 文件f=新文件(项目); 如果(f.exists()){ 试一试{ FileInputStream fis=新的FileInputStream(f); 位图b=BitmapFactory.decodeStream(fis); 位图。添加(b); 添加(f); }catch(filenotfounde异常){ 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>

您正在将
GridView
放置在
com.unimelb.pt3.ui.TransparentPanel
内部,它是
10px

  • 你不应该使用
    px
    ,你应该使用
    dp
  • com.unimelb.pt3.ui.TransparentPanel的
    android:layout\u height=“10px”
    更改为
    android:layout\u height=“fill\u parent”

GridView有一个invalidateViews()方法

调用此方法时:“所有要重建和重绘的视图。”


我认为这就是您需要的:)

您必须首先告诉适配器通知数据已更改,然后再次将适配器设置为网格

adapter.notifyDataChanged();
grid.setAdapter(adapter);
@斯纳格内弗飞人

你们两个一起得到了。应该是:

adapter.notifyDataChanged();
grid.invalidateViews();
这将标记适配器其数据已更改,然后在调用invalidateViews()方法后将其传播到网格

很高兴我发现了这个问题,因为我不知道如何在网格渲染后将项目添加到网格中。

这可能会有所帮助。 在对项目执行删除操作后,我刷新书籍图像缩略图的网格视图。 使用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);

这些答案实际上都不适用于我,我不得不把它们全部混合在一起。要实际更新GridView,需要执行以下操作:

 adapter.notifyDataChanged();
 grid.invalidateViews();
 grid.setAdapter(adapter);
希望这能帮助那些无法使用其他解决方案的人。

在适配器中:

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();
class MAdapter扩展了BaseAdapter{
列出移动对象;
...
公共无效clearAdapter(){
mObjects.clear();
}
public void addNewValues(列出移动对象){
this.mObjects=mObjects;
}
...
}
adapter.clearAdapter();//清除旧价值观
adapter.addNewValues(MObjects);
adapter.notifyDataChanged();
可能仅仅因为适配器的数据过时而无法工作(例如,如果活动或片段未被销毁并且一直位于后堆栈中)

所以,如果首先刷新数据,那么之后它就会工作。

您应该
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();
adapter.notifyDataChanged();