如何刷新GridView的?GridView

2023-09-12 07:30:16 作者:回頭已無路

我有一个GridView这类似于谷歌教程pretty的,但我想补充上运行时ImageViews(通过一个子活动)。结果是正常的,但鉴于布局搞砸:?GridView控件不填写其父的内容,做什么我必须做设计得当

下面加入了孩子们的code:

 公共无效initializeWorkbench(GridView的GV,矢量<串GT;项目){
        Prototype.workbench.setDimension(屏幕宽度,divider.height()+ workbenchArea.height());
        Prototype.workbench.activateWorkbench();
        //这个正确测量工作台
        Log.d(Prototype.TAG,工作台宽:+ Prototype.workbench.getMeasuredWidth());
        // 320
        Log.d(Prototype.TAG,工作台高度:+ Prototype.workbench.getMeasuredHeight());
        // 30
        ImageAdapter imgAdapter =新ImageAdapter(this.getContext(),项目);
        gv.setAdapter(imgAdapter);
        gv.measure(屏幕宽度,screenHeight);
        gv.requestLayout();
        gv.forceLayout();
        Log.d(Prototype.TAG,GV宽:+ gv.getMeasuredWidth());
        // 22
        Log.d(Prototype.TAG,GV高度:+ gv.getMeasuredHeight());
        // 119
        Prototype.workbench.setDimension(屏幕宽度,divider.height()+ workbenchArea.height());
    }
}
 

activateWorkbench,setDimension并测量工作台(的LinearLayout上面的GridView控件):

 公共无效activateWorkbench(){
    如果(this.equals(Prototype.workbench)){
        this.setOrientation(垂直);
        秀= TRUE;
        测量();
    }
}

公共无效setDimension(INT W,INT高){
    宽= W;
    高度= H;
    this.setLayoutParams(新LinearLayout.LayoutParams(宽度,高度));
    this.invalidate();
}

私人无效措施(){
    如果(this.getOrientation()== LinearLayout.VERTICAL){
        INT H = 0;
        INT瓦特= 0;
        this.measureChildren(0,0);
        的for(int i = 0; I< this.getChildCount();我++){
            视图v = this.getChildAt(ⅰ);
            H + = v.getMeasuredHeight();
            W =(W< v.getMeasuredWidth())? v.getMeasuredWidth():W;
        }
        如果(this.equals(Prototype.tagarea))
            身高=(H<高)?高度:H;
        如果(this.equals(Prototype.tagarea))
            宽度=(W<宽)?宽度:W;
    }
    this.setMeasuredDimension(宽度,高度);
}
 
GridView1更新问题.点击更新没有实现数据的更改,还是原来的值.....怎么回事呢

在ImageAdapter构造:

 公共ImageAdapter(上下文C,矢量<串GT;项目){
    mContext = C;

    布尔mExternalStorageAvailable = FALSE;
    布尔mExternalStorageWriteable = FALSE;
    字符串状态= Environment.getExternalStorageState();

    如果(Environment.MEDIA_MOUNTED.equals(州)){
        //我们可以读取和写入的媒体
        mExternalStorageAvailable = mExternalStorageWriteable = TRUE;
    }否则,如果(Environment.MEDIA_MOUNTED_READ_ONLY.equals(州)){
        //我们只能读取介质
        mExternalStorageAvailable = TRUE;
        mExternalStorageWriteable = FALSE;
    } 其他 {
        //别的东西是错误的。这可能是很多其他的国家之一,但
        //我们所需要的
        //知道的是,我们既不能读也不能写
        mExternalStorageAvailable = mExternalStorageWriteable = FALSE;
    }

    如果(mExternalStorageAvailable&安培;&安培; mExternalStorageWriteable){
        对于(字符串项目:项目){
            文件F =新的文件(项目);
            如果(f.exists()){
                尝试 {
                    的FileInputStream FIS =新的FileInputStream(F);
                    位图B = BitmapFactory.de codeStream(FIS);
                    bitmaps.add(B);
                    files.add(F);
                }赶上(FileNotFoundException异常E){
                    Log.e(Prototype.TAG,,E);
                }
            }
        }
    }
}
 

和XML布局:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
                机器人:方向=垂直
                机器人:重力=底

                机器人:以下属性来=0px
                机器人:paddingTop =0px
                机器人:paddingRight =0px​​>

    < com.unimelb.pt3.ui.TransparentPanel
            机器人:ID =@ + ID /工作台
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =10px的
            机器人:paddingTop =0px
            机器人:以下属性来=0px
            机器人:paddingBottom会=0px
            机器人:paddingRight =0px​​>

        < GridView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            机器人:ID =@ + ID / GridView控件
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            机器人:columnWidth中=90dp
            机器人:为numColumns =auto_fit
            机器人:verticalSpacing =10dp
            机器人:horizo​​ntalSpacing =10dp
            机器人:stretchMode =columnWidth中
            机器人:重力=中心/>

    < /com.unimelb.pt3.ui.TransparentPanel>

< / LinearLayout中>
 

解决方案

在GridView有invalidateViews()方法。

在调用此方法:所有的意见进行重建,并重新绘制。 http://developer.android.com/reference/android/widget/GridView.html

我想这是你所需要的:)

I have a GridView which is pretty similar to the Google tutorial, except that I want to add the ImageViews on runtime (via a subactivity). The results are okay, but the layout of the View is messed up: The GridView doesn't fill the content of its parent, what do I have to do to design it properly?

Here the code of adding the children:

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());
    }
}

activateWorkbench, setDimension and measure in the workbench (LinearLayout above the GridView):

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);
}

The ImageAdapter constructor:

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);
                }
            }
        }
    }
}

And the xml layout:

<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>

解决方案

the GridView has an invalidateViews() method.

when you call this method: "all the views to be rebuilt and redrawn." http://developer.android.com/reference/android/widget/GridView.html

i think this is what you need:)