GLSurfaceView.Renderer不适用于android.support.v4.view.ViewPager

时间:2019-04-13 12:35:36

标签: java android android-fragments android-viewpager glsurfaceview

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

  

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

1 个答案:

答案 0 :(得分: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>