gpt4 book ai didi

android - 如何将选项卡监听器添加到选项卡

转载 作者:行者123 更新时间:2023-11-29 00:53:23 25 4
gpt4 key购买 nike

所以我想在我的应用程序中添加相机 Intent 。但是在我的应用程序中,当您单击“打开相机”按钮时,它会打开相机,但我希望相机在我单击“照片”选项卡时立即出现。请如果有人帮助我,我将不胜感激。

制表符

/**
* return the current tab number
* 0 = Gallery_Fragment
* 1 = Photo_Fragment
* @return
*/
public int getCurrentTabNumber(){
return mViewPager.getCurrentItem();
}

/**
* setup viewpager for manager the tabs
*/
private void setupViewPager(){
SectionsPagerAdapter adapter = new SectionsPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new GalleryFragment());
adapter.addFragment(new PhotoFragment());

mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(adapter);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabsBottom);
tabLayout.setupWithViewPager(mViewPager);




tabLayout.getTabAt(0).setText(getString(R.string.gallery));
tabLayout.getTabAt(1).setText(getString(R.string.photo));
tabLayout.setTabTextColors(getResources().getColor(R.color.white), getResources().getColor(R.color.white));
}

照片 fragment

公共(public)类 PhotoFragment 扩展 fragment { private static final String TAG = "PhotoFragment";

//constant
private static final int PHOTO_FRAGMENT_NUM =1;
private static final int GALLERY_FRAGMENT_NUM =2;
private static final int CAMERA_REQUEST_CODE = 5;


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_photo, container, false);
Log.d(TAG, "onCreateView: started.");

Button btn = (Button) view.findViewById(R.id.btnLaunchCamera);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "onClick: Launching Camera");

if (((ShareActivity)getActivity()).getCurrentTabNumber()== PHOTO_FRAGMENT_NUM){
if (((ShareActivity)getActivity()).checkPermissions(Permissions.CAMERA_PERMISSIONS[0])){
Log.d(TAG, "onClick: starting camera");
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);

}else{
Intent intent = new Intent (getActivity(), ShareActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

}

}
}
});
return view;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == CAMERA_REQUEST_CODE){
Log.d(TAG, "onActivityResult: done taking a photo.");
Log.d(TAG, "onActivityResult: attempting to navigate tofinal share screen");
//navigate to the final share screen to publish photo

}
}

最佳答案

试试这个:

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
if (tab.getPosition() == YOUR_CAMERA_TAB_POSITION) {
// do camera logic here
}
}

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

}

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

}
});

关于android - 如何将选项卡监听器添加到选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57358748/

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