带有活动的选项卡上的ViewPager +标签

时间:2018-03-18 13:58:19

标签: android android-activity android-viewpager android-tablayout

我有三个关于活动的标签:“相机”,“画廊”,“在帖子中添加” 我会把相机碎片整个屏幕。屏幕截图显示了我要做的一切。

这是我的应用: screenshot

我想让它像whatsapp(actionbar隐藏): whatsapp screenshot 或Facebook(如果像whatsapp太难了):facebook screenshot

GalleryActivity代码:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery);
        galleryToolbar = findViewById(R.id.galleryToolbar);
        viewPagerGallery = findViewById(R.id.viewPagerGallery);
        galleryTabLayout = findViewById(R.id.galleryTabLayout);
        galleryFragment = new RecyclerGalleryFragment();
        addedPhotosFragment = new AddedPhotosFragment();
        setSupportActionBar(galleryToolbar);


        galleryTabLayout.addTab(galleryTabLayout.newTab().setText("Camera"));
        galleryTabLayout.addTab(galleryTabLayout.newTab().setText("Gallery"));
        galleryTabLayout.addTab(galleryTabLayout.newTab().setText("Added in posts"));
        galleryTabLayout.setTabGravity(TabLayout.MODE_FIXED);

        tabsGalleryAdapter = new TabsGalleryAdapter(getSupportFragmentManager(),
                galleryTabLayout.getTabCount());

        viewPagerGallery.setAdapter(tabsGalleryAdapter);
        viewPagerGallery.setCurrentItem(1);
        viewPagerGallery.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(galleryTabLayout));
        galleryTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPagerGallery.setCurrentItem(tab.getPosition());
            }

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

            }

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

            }
        });
    }

TabsGalleryAdapter代码:

public class TabsGalleryAdapter extends FragmentPagerAdapter {

    int mNoOfTabs;

    public TabsGalleryAdapter(FragmentManager fm) {
        super(fm);
    }

    public TabsGalleryAdapter(FragmentManager fm, int numberOfTabs) {
        super(fm);
        mNoOfTabs = numberOfTabs;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new CameraGalleryFragment();
            case 1:
                return new RecyclerGalleryFragment();
            case 2:
                return new AddedPhotosFragment();
        }
        return null;
    }

    @Override
    public int getCount() {
        return mNoOfTabs;
    }
}

GalleryActivity布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.stagap.stagap.chooseimage.GalleryActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/galleryToolbar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:titleTextColor="#fff"
        app:title="Choose a photo" />

    <android.support.design.widget.TabLayout
        android:id="@+id/galleryTabLayout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/galleryToolbar"
        app:tabIndicatorColor="@android:color/transparent"
        app:tabSelectedTextColor="@color/colorPrimary"
        app:tabMode="scrollable"/>

    <View
        android:id="@+id/galleryDivider"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        style="@style/Divider"
        app:layout_constraintTop_toBottomOf="@+id/galleryTabLayout"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPagerGallery"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/galleryDivider">

    </android.support.v4.view.ViewPager>


</android.support.constraint.ConstraintLayout>

和CameraFragment布局:

<FrameLayout 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="com.stagap.stagap.chooseimage.camera.CameraGalleryFragment">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <FrameLayout
            android:id="@+id/cameraLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>


        <ImageButton
            android:id="@+id/takePhotoBtn"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginBottom="30dp"
            android:background="@android:color/transparent"
            android:scaleType="centerCrop"
            android:src="@drawable/done"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

    </android.support.constraint.ConstraintLayout>

</FrameLayout>

也许有人有点想法?请反馈,祝你有愉快的一天

2 个答案:

答案 0 :(得分:1)

您可以在活动中使用回调,无论何时导航到该片段,都要将Camera Fragment放在您的案例中。在回调方法中,设置tabLayout.setVisibility(View.GONE),类似于其他一些片段位置值tabLayout.setVisibility(View.VISIBLE)

答案 1 :(得分:0)

我使用AppbarLayout的底部在onPageScrolled()的{​​{1}}的{​​{1}}回调中,使用其translationY属性来翻译ViewPager,以解决此问题值。

请参阅此答案https://stackoverflow.com/a/54286160/8114428

或访问此存储库以获取更多帮助https://github.com/goody-h/ResidingTab