如何将选项卡侦听器添加到选项卡
Posted
技术标签:
【中文标题】如何将选项卡侦听器添加到选项卡【英文标题】:How to add tab listener to the tabs 【发布时间】:2019-12-12 23:40:23 【问题描述】:所以我想在我的应用程序中添加相机意图..但是在我的应用程序中,当您单击“打开相机”按钮时,它会打开相机,但我希望相机在我单击照片选项卡时立即出现..请有人帮助我,我将非常感激。
标签
/**
* 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));
照片片段
公共类 PhotoFragment 扩展片段 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
【问题讨论】:
【参考方案1】:试试这个:
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)
);
【讨论】:
@Harris 很高兴它做到了!快乐编码:)【参考方案2】:为了提高相机的速度,您可以在清单中使用以下代码
android:hardwareAccelerated="true"
对于标签监听器,你可以试试这个
topTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
@Override
public void onTabSelected(TabLayout.Tab tab)
if (tab.getPosition == 2/*your camera tab position*/ )
//do somthing
@Override
public void onTabUnselected(TabLayout.Tab tab)
@Override
public void onTabReselected(TabLayout.Tab tab)
);
【讨论】:
谢谢你的朋友,如果有帮助请投我一票以上是关于如何将选项卡侦听器添加到选项卡的主要内容,如果未能解决你的问题,请参考以下文章