gpt4 book ai didi

android - ViewPager在包含相机的滑动上显示黑屏

转载 作者:行者123 更新时间:2023-12-03 16:05:43 25 4
gpt4 key购买 nike

发生的情况是,当“ Activity ”运行时,它会显示相机片段,而当我在查看分页器上滑动其他片段时,它将显示全黑屏幕。我无法访问其他片段的内容。

这是我的 View Pager的布局。

<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="lifeline.learn.com.suggestions.WallOfTheApp">

<android.support.design.widget.TabLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/tabLayout"/>

<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:animateLayoutChanges="true"
android:background="@drawable/backgroundcorner"
android:layout_height="match_parent"
android:layout_below="@+id/tabLayout"/> </RelativeLayout>

这是该 Activity 的类别
public class WallOfTheApp extends AppCompatActivity {

ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_wall_of_the_app);
requestPermissions (new String[]{Manifest.permission.CAMERA}, 10);
TabLayout tabLayout = findViewById (R.id.tabLayout);
viewPager = findViewById (R.id.viewPager);
//Button cameraBtn = findViewById (R.id.camer_button);
tabLayout.addTab (tabLayout.newTab ().setText ("MainFragment"));
tabLayout.addTab (tabLayout.newTab ().setText ("PrfileInfo"));
tabLayout.addTab (tabLayout.newTab ().setText ("Camera"));
PagerAdapter adapter = new PageAdapter (getSupportFragmentManager (), tabLayout.getTabCount ());
viewPager.setAdapter (adapter);
viewPager.setOffscreenPageLimit (tabLayout.getTabCount ());
viewPager.setCurrentItem (1);
//viewPager.setPageTransformer (false, new PageTransformerAnimation ());
viewPager.setOnPageChangeListener (new TabLayout.TabLayoutOnPageChangeListener (tabLayout));
tabLayout.setOnTabSelectedListener (new TabLayout.OnTabSelectedListener () {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem (tab.getPosition ());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});
}

@Override
public void onBackPressed() {
if(viewPager.getCurrentItem () != 1){
viewPager.setCurrentItem (1);
}else{
super.onBackPressed ();
}
}
}

这是Camera.java片段
    public class Camera extends android.support.v4.app.Fragment {
Context context;
private android.hardware.Camera mCamera;
private SurfaceHolders surfaceHolders;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate (R.layout.fragment_camera, container, false);
context = getActivity ();
if(checkCameraHardware (context)){
try {
surfaceHolders = new SurfaceHolders (context,mCamera);
FrameLayout preview = view.findViewById(R.id.camera_layout);
preview.addView(surfaceHolders);
}catch (Exception e){
Toast.makeText (context, e.getMessage (), Toast.LENGTH_SHORT).show ();
}
}
return view;
}

private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
return true;
} else {
return false;
}
}

public static android.hardware.Camera getCameraInstance(){
android.hardware.Camera c = null;
try {
c = android.hardware.Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
}
return c;
}

@Override
public void onDestroyView() {
super.onDestroyView();
if(mCamera != null)
mCamera.release();
}
}

这是表面支架类
public class SurfaceHolders extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private android.hardware.Camera mCamera;

public SurfaceHolders(Context context, android.hardware.Camera camera) {
super(context);
mCamera = camera;
mHolder = getHolder();
mHolder.addCallback(this);
}

public void surfaceCreated(SurfaceHolder holder) {
try {
if(mCamera != null) {
mCamera = Camera.open();
mCamera.setPreviewDisplay (holder);
mCamera.startPreview();
mCamera.setDisplayOrientation (90);
}
} catch (IOException e) {

}
}

public void surfaceDestroyed(SurfaceHolder holder) {
if(mCamera != null){
mCamera.stopPreview ();
mCamera.release ();
mCamera = null;
}
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

if (mHolder.getSurface() == null){
return;
}
try {
mCamera.stopPreview();
} catch (Exception e){

}
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.e ("Error",e.getMessage ());
}
}
}

这是相机片段的布局
    <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:elevation="10dp"
tools:context="lifeline.learn.com.suggestions.Camera">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/camera_layout">
</FrameLayout>
</FrameLayout>

What I found is that the fragment is there on swipe but it is under a screen of black color.

最佳答案

您可以检查此链接:TabLayout and ViewPager

简单的例子:

public class FragmentOne extends Fragment {

public FragmentOne() {
// Required empty public constructor
}

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_one, container, false);
}

}


public class MainActivity extends AppCompatActivity {

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

ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

// Add Fragments to adapter one by one
adapter.addFragment(new FragmentOne(), "FRAG1");
adapter.addFragment(new FragmentTwo(), "FRAG2");
adapter.addFragment(new FragmentThree(), "FRAG3");
viewPager.setAdapter(adapter);

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


}




class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}

@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}

@Override
public int getCount() {
return mFragmentList.size();
}

public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}

@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}

关于android - ViewPager在包含相机的滑动上显示黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48944149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com