android - Fragment 中的 TextureView 从不调用 onSurfaceTextureAvailable

标签 android android-fragments

我已经看到很多在主 Activity 中使用 TextureView 的示例,但我正在尝试将其放入 Fragment 中。

我已经创建了最简单的示例,它的 onCreateView 被调用,onActivityCreated 也被调用,但是 onSurfaceTextureAvailable 在传回 TextureView 后没有被调用。

我错过了什么?

谢谢 G

public class FullscreenActivity extends Activity {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_fullscreen);
    }
}

public class TextureViewFragment  extends Fragment
implements TextureView.SurfaceTextureListener {

    private TextureView         mTextureView;

     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
     }

     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

          mTextureView = new TextureView(getActivity());
          mTextureView.setSurfaceTextureListener(this);
          mTextureView.setOpaque(false);    
         return mTextureView;
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
         // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width,
        int height) {
         // TODO Auto-generated method stub
     }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
       // TODO Auto-generated method stub   
    }
}


activity_fullscreen.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:background="#0099cc"
             tools:context=".FullscreenActivity" >

     <fragment class="com.example.test.TextureViewFragment"
               android:id="@+id/graphTextureView"
               android:layout_width="0px" android:layout_height="match_parent" />
 </FrameLayout>

最佳答案

这是因为 TextureView 必须具有非零大小并且可见。根据评论,layout_width 设置为 0px。将其更改为非零值。

(老问题,但为后代发布答案)

关于android - Fragment 中的 TextureView 从不调用 onSurfaceTextureAvailable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15320175/

相关文章:

android - 如何在android中显示从一个 Activity 移动到另一个 Activity 的进度条

java - 我无法在Android Studio上运行该应用程序

java - 更改自定义适配器内容

Android setTheme() 不改变 ActionBar 和状态栏

java - ListFragment 支持库 GetSupportFragmentManager undefined

Android 即时应用程序 : How to test an Instant app after deploying?

android - AChartEngine 图表不会显示?

java - 从新的 Runnable 类更新主线程

android - SwipeRefreshLayout 干扰 ViewPager 中的 ScrollView

java - RecyclerView 在运行应用程序时未显示