将surfaceview添加到片段

时间:2016-09-09 15:16:30

标签: android android-fragments android-viewpager surfaceview android-tablayout

我有以下自定义SurfaceView class

public class Maze extends SurfaceView implements SurfaceHolder.Callback

在我的fragment class上,这就是我目前将Maze添加到RelativeLayout容器的方式。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_1, container, false);
        maze = new Maze(this.getContext());
        ((RelativeLayout) rootView.findViewById(R.id.gridmapLayout)).addView(maze);
        return rootView;
    }

以某种方式添加SurfaceView会将背景重置为黑色(请参阅xml文件的评论)。

fragment_1.xml

<RelativeLayout 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="@drawable/background"> // this gets removed.

    <RelativeLayout
        android:id="@+id/gridmapLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </RelativeLayout>
</RelativeLayout>

我得到的另一个问题是我正在实施viewPagerTabLayout来导航Fragment。如果SurfaceView附加到fragment1,当我尝试通过标签导航到fragment2时,不会加载fragment2 UI。通过滑动浏览viewPager可以正常工作。不确定这里发生了什么。

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

        setContentView(R.layout.activity_main);

        viewPager = (ViewPager) findViewById(R.id.viewPager);
        viewPager.setAdapter(new CustomAdapater(getSupportFragmentManager(), getApplicationContext()));

        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        tabLayout.setupWithViewPager(viewPager);

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

        });

    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }


    private class CustomAdapater extends FragmentPagerAdapter {

        private String fragments[] = {"Control", "Log", "Connection"};

        public CustomAdapater(FragmentManager supportFragmentManager, Context applicationContext) {
            super(supportFragmentManager);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new Fragment1();
                case 1:
                    return new Fragment2();
                case 2:
                    return new Fragment3();
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return fragments.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return fragments[position];
        }
    }
}

0 个答案:

没有答案