在 onCreate 之外编辑 ViewGroup

Edit ViewGroup outside onCreate

我以编程方式创建了一个 ViewGroup。然后我添加两个视图,一个游戏和一个 SeekBar。但是,如果我尝试使用例如 parent.seekBar.setVisibility(View.VISIBLE) 从游戏中更改 SeekBar 属性(它有一个指向 AndroidAdapter 的指针),应用程序会崩溃。

父字段设置如下,游戏内class:

public class Simulation extends ApplicationAdapter implements GestureDetector.GestureListener{

    AndroidLauncher parent=null;

    public void setParent( AndroidLauncher nparent ){
       parent = nparent;
    }

    public void something(){
        if( parent != null ) parent.layout.removeView( parent.seekBar );
    }
}

我是不是缺少更改视图的权限之类的东西?在编辑 SeekBar 之前我有 set/change 东西吗?谢谢!

public class AndroidLauncher extends AndroidApplication {

    Simulation simul;
    ViewGroup layout;

    SeekBar seekBar;
    ShapeDrawable thumb;
    LayoutParams lp;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        simul = new Simulation();
        simul.setParent(this);
        super.onCreate(savedInstanceState);

        setContentView( R.layout.main );
        layout = (ViewGroup) findViewById( R.id.panel );

        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        config.useWakelock = true;
        config.useAccelerometer = false;
        config.useCompass = false;
        layout.addView(initializeForView(simul, config));


        seekBar = new SeekBar(this);
        seekBar.setMax(100);
        thumb = new ShapeDrawable(new OvalShape());
        thumb.setIntrinsicHeight(50);
        thumb.setIntrinsicWidth(50);
        seekBar.setThumb(thumb);
        seekBar.setProgress(1);
        seekBar.setBackground(new ColorDrawable(android.R.color.transparent));
        lp = new LayoutParams(600, 50);
        seekBar.setLayoutParams(lp);
        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            public void onStopTrackingTouch(SeekBar arg0) {
                // TODO Auto-generated method stub
                System.out.println(".....111.......");
            }

            public void onStartTrackingTouch(SeekBar arg0) {
                // TODO Auto-generated method stub
                System.out.println(".....222.......");
            }

            public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
                // TODO Auto-generated method stub
                System.out.println(".....333......." + arg1);
            }
        });

        layout.addView(seekBar);
    }
}

只有主线程可以编辑视图。要从其他 类 编辑视图,您必须使用类似 setParent(this) 的方式将引用传递给主线程 Class。应该在主线程中设置一个回调方法Class,当你想编辑视图时会调用这个方法。在这个方法中,你必须保证指令是由main/UI线程执行的,你可以使用runOnUiThread(...).

来做到这一点

检查以下示例:

public class AndroidLauncher extends AndroidApplication {

Simulation simul;
ViewGroup layout;

SeekBar seekBar;

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    simul = new Simulation();
    simul.setParent(this);


    setContentView(R.layout.main);
    layout = (ViewGroup) findViewById( R.id.panel );

    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
    config.useWakelock = true;
    config.useAccelerometer = false;
    config.useCompass = false;
    layout.addView(initializeForView(simul, config));

    seekBar = (SeekBar) findViewById(R.id.seekBar);
    seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            System.out.println( progress );
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

}

public void showSeekBar(){
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            seekBar.setVisibility( View.VISIBLE );
        }
    });

}


public void hideSeekBar(){
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            seekBar.setVisibility( View.GONE );
        }
    });

}

XML文件设置如下:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/main" android:orientation="vertical">



<FrameLayout
    android:id="@+id/panel"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</FrameLayout>


<SeekBar
    android:layout_width="406dp"
    android:layout_height="wrap_content"
    android:id="@+id/seekBar"
    android:layout_gravity="top|center_horizontal"
    android:progress="50"
    android:max="200"
    android:visibility="gone"
    android:layout_marginTop="30dp" />