GLSurfaceView.Renderer无法使用android.support.v4.view.ViewPager

问题描述 投票:0回答:1

我有一个奇怪的问题。当我尝试添加

新的GLSurfaceView.Renderer()

我的活动,因为我想获得渲染器的信息

glGetString(GL10.GL_RENDERER)

它不起作用。如果我隐藏ViewPager视图,它将显示信息。任何想法如何解决这一问题?我尝试在创建glsurface后将viewpager上的可见性切换为可见,但它无法再次运行。它仅适用于viewpager视图不可见,这不是一个选项。这是我的代码的一部分:

 public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private ViewPagerAdapter adapter;
    private GLSurfaceView mGLSurfaceView;
    private LinearLayout rlRoot;
    GLSurfaceView.Renderer mGLRenderer;

    private void setupGL() {
        mGLRenderer = new GLSurfaceView.Renderer() {
            public void onDrawFrame(GL10 gl10) {
            }

            public void onSurfaceChanged(GL10 gl10, int i, int i2) {
            }

            public void onSurfaceCreated(GL10 gl10, EGLConfig eGLConfig) {
                if (gl10 != null) {
                    gl10.glClearColor(8.0f, 8.0f, 8.0f, 0.0f);

                    Log.d(TAG, "onSurfaceCreated: " + gl10.glGetString(GL10.GL_RENDERER));

                    MainActivity.this.runOnUiThread(new Runnable() {
                        public void run() {
                            if (MainActivity.this.mGLSurfaceView != null) {
                                ViewGroup viewGroup = (ViewGroup) MainActivity.this.mGLSurfaceView.getParent();
                                if (viewGroup != null) {
                                    viewGroup.removeView(MainActivity.this.mGLSurfaceView);
                                }
                            }
                        }
                    });
                }
            }
        };

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Paper.init(this);

        setupGL();
        rlRoot = findViewById(R.id.linear);
        mGLSurfaceView = new GLSurfaceView(MainActivity.this);
        mGLSurfaceView.setRenderer(mGLRenderer);
        rlRoot.addView(mGLSurfaceView);
        detectOpenGLESGPUInfo();

        init();
    }

    private void detectOpenGLESGPUInfo() {

        ViewGroup viewGroup = (ViewGroup) findViewById(R.id.linear);
        if (viewGroup != null) {
            this.mGLSurfaceView = new GLSurfaceView(this);
            this.mGLSurfaceView.setEGLContextClientVersion(2);
        }
    }

    public void init() {
        String language = Paper.book().read("language");

        if (language == null) {
            Paper.book().write("language", "en");
            language = "en";
        }

        Context context = LocaleHelper.setLocale(this, language);
        Resources resources = context.getResources();


        tabLayout = findViewById(R.id.tab_layout);
        viewPager = findViewById(R.id.viewpager);

        adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new FragmentCPU(), resources.getString(R.string.cpu));
        adapter.addFragment(new FragmentCamera(), resources.getString(R.string.camera_fragment));
        adapter.addFragment(new FragmentBattery(), resources.getString(R.string.battery));
        adapter.addFragment(new FragmentDisplay(), resources.getString(R.string.display));
        adapter.addFragment(new FragmentSensors(), resources.getString(R.string.sensors));
        Log.d(TAG, "init: " + adapter);

        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setVisibility(View.VISIBLE);
    }

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        app:tabGravity="center"
        app:tabIndicatorColor="@color/colorAccent"
        app:tabMode="scrollable" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v4.view.ViewPager>

</LinearLayout>
java android android-fragments android-viewpager glsurfaceview
1个回答
0
投票

我通过添加另一个布局来修复它:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <LinearLayout
            android:id="@+id/root"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            app:tabGravity="center"
            app:tabIndicatorColor="@color/colorAccent"
            app:tabMode="scrollable" />

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:visibility="visible"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </android.support.v4.view.ViewPager>

    </LinearLayout>

    </RelativeLayout>
© www.soinside.com 2019 - 2024. All rights reserved.