Android滑动标签切断片段和FAB不起作用



我有两个问题/问题:)

首先:我实现了滑动选项卡布局。现在,该片段用于底部的选项卡被切断:片段被切断(FAB在底部|右侧)

第二:当我按下 FAB 时,我想打开一个新活动(FAB 位于片段上)。但是当我按下它时,会打开一个空白的活动(帐户活动具有布局并且正在自行工作),当我按下后退按钮时,我退出了应用程序。

对于第一个问题:这是我在主活动(实现滑动选项卡布局的地方)的onCreate中调用的方法(我不知道您是否需要ViewPager的代码,所以我没有包含它,如果您需要它告诉我):

 private void excTasks(){
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    viewPager.setOffscreenPageLimit(4);
    setupViewPager(viewPager);
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    setupTabIcons();
}

这是 ViewPagerAdapter:

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);
        // Wenn man nur Icons will -> return null, sont das obrige verwenden
        return null;
    }
}

在这里,我添加片段:

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new NewsFragment(), "NEWS");
    adapter.addFragment(new ProfileFragment(), "PROFILE");
    adapter.addFragment(new NotificationFragment(), "NOTIF");
    adapter.addFragment(new AboutFragment(), "ABOUT");
    viewPager.setAdapter(adapter);
}

这是主活动的.xml:

<android.support.design.widget.CoordinatorLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/white"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"  />
</android.support.design.widget.CoordinatorLayout>

对于第二个问题:这是在片段(在onViewCreated中),我想要FAB(OfferActivity是按下按钮时应该打开的内容):

//FAB
    FloatingActionButton fab = (FloatingActionButton) v.findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Click action
            Intent intent = new Intent(con, OfferActivity.class);
            getActivity().startActivity(intent);
        }
    });

如前所述,OfferActivity 在一个选项卡中显示时工作正常,所以我不会包含它的代码。这是 FAB xml(在 RelativeLayout 中): 编辑:包含整个文件

<?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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">
<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@android:id/list"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:layout_weight="0"
    android:layout_marginTop="5dp"
    android:foregroundGravity="fill"/>
<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/fab_margin"
    android:layout_marginRight="@dimen/fab_margin"
    android:src="@drawable/fab_add"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:baselineAlignBottom="true"
    android:clickable="true"
    app:backgroundTint="@color/colorPrimary" />

感谢您的回答,如果这个问题已经被问过,很抱歉! :)

编辑:这是样式.xml我已经包含了整个FAB xml

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:actionBarDivider">@color/colorPrimary</item>
    <!--<item name="android:windowTranslucentStatus" tools:targetApi="kitkat">true</item>-->
</style>
<style name="MyMaterialTheme" parent="AppTheme">
    <item name="android:windowContentTransitions" tools:targetApi="lollipop">true</item>
    <item name="android:windowAllowEnterTransitionOverlap" tools:targetApi="lollipop">true</item>
    <item name="android:windowAllowReturnTransitionOverlap" tools:targetApi="lollipop">true</item>
    <item name="android:windowSharedElementEnterTransition" tools:targetApi="lollipop">@android:transition/move</item>
    <item name="android:windowSharedElementExitTransition" tools:targetApi="lollipop">@android:transition/move</item>
</style>

我认为layout_baselineAlignBottom属性的使用会产生问题。app:layout_anchor属性使我们能够指定我们希望锚定浮动操作按钮的布局,app:layout_anchorGravity指定锚点的位置。将以下浮动操作按钮 xml 放在 </android.support.design.widget.CoordinatorLayout> 之前的末尾。

<android.support.design.widget.FloatingActionButton
   android:id="@+id/fab"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginBottom="@dimen/fab_margin"
   android:layout_marginRight="@dimen/fab_margin"
   android:src="@drawable/fab_add"
   android:layout_alignParentBottom="true"
   android:layout_alignParentRight="true"
   android:baselineAlignBottom="true"
   android:clickable="true"
   app:backgroundTint="@color/colorPrimary"   
   app:layout_anchor="@id/viewpager"
   app:layout_anchorGravity="bottom|right|end"  />

请注意:"浮动操作"按钮只能与协调器布局结合使用。

编辑:好的,将您的相对布局包装在协调员布局中,然后更改 app:layout_anchor="@id/viewpager"在浮动操作按钮 XML 中app:layout_anchor="@id/listview"

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">
<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/list"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:layout_weight="0"
    android:layout_marginTop="5dp"
    android:foregroundGravity="fill"/>

</RelativeLayout>
<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:clickable="true"
    app:backgroundTint="@color/colorPrimary"
    app:layout_anchor="@id/list"
    android:src="@drawable/fab_add"
    android:layout_marginBottom="@dimen/fab_margin"
    android:layout_marginRight="@dimen/fab_margin"
    app:layout_anchorGravity="bottom|right|end"/>
</android.support.design.widget.CoordinatorLayout>
您可以将

FloatingActionButton与这样的CoordinatorLayout一起使用:

<CoordinatorLayout>
    <AppbarLayout/>
    <scrollableView/>
    <FloatingActionButton/>
</CoordinatorLayout>

像这样的东西(您可以使用支持库或在CoordinatorLayout内使用它)这是最好的方法:

<android.support.design.widget.CoordinatorLayout 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:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            app:tabIndicatorColor="@color/white"
            app:tabMode="fixed" />
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:src="@drawable/ic_add"
        app:backgroundTint="@color/ColorAccent"
        app:borderWidth="0dp"
        app:fabSize="normal"
        app:layout_anchor="@id/coordinator"
        app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>

希望 value-v21 风格搞砸了.xml。

尝试在 value-v21 样式中添加以下属性集.xml。

<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:fitsSystemWindows">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">false</item>

并将以下代码添加到 Fab 片段中。

private int getNavigationBarHeight() { 
    Resources resources = getResources();
    int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
    if (resourceId > 0) {
        return resources.getDimensionPixelSize(resourceId);
    } 
    return 0; 
} 
@Override 
public void onCreateView(Bundle savedInstanceState) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        int navBarHeight = getNavigationBarHeight();
        findViewById(R.id.base_frame).setPadding(0, 0, 0, navBarHeight);
        findViewById(R.id.menu_frame).setPadding(0, 0, 0, navBarHeight);
    } 
} 

类似的问题在这里

祝你好运。。!

试试这段代码:-

public class DeliveryTab extends Fragment {
    public static TabLayout tabLayout;
    public static ViewPager viewPager;
    public static int int_items = 3 ;
    android.support.v4.app.FragmentManager mFragmentManager;
    View view;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getActivity().setTitle("Delivery");
        View x =  inflater.inflate(com.Weal.sachin.omcom.R.layout.activity_delivery_tab,null);
        tabLayout = (TabLayout) x.findViewById(com.Weal.sachin.omcom.R.id.sliding_tabs_delivery);
        viewPager = (ViewPager) x.findViewById(com.Weal.sachin.omcom.R.id.viewpager_delivery);
        viewPager. setOffscreenPageLimit(3);
        FloatingActionButton fab = (FloatingActionButton)x.findViewById(R.id.fabdelivery);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentTransaction t = getFragmentManager().beginTransaction();
                Add_Delivery mFrag = new Add_Delivery();
                t.replace(R.id.framelayout, mFrag);
                t.commit();
            }
        });
        viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
        tabLayout.setupWithViewPager(viewPager);

        return x;
    }
    class MyAdapter extends FragmentPagerAdapter {
        com.Weal.sachin.omcom.TodayVisit TodayVisit;
        Home home;
        com.Weal.sachin.omcom.UpdatesFragment UpdatesFragment;
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }
        /**
         * Return fragment with respect to Position .
         */
        @Override
        public Fragment getItem(int position)
        {
            switch (position){
                case 0 : return new DeliveryToday();
                case 1 : return new YesterdayDelivery();
                case 2 : return new Delivery_all();
            }
            return null;
        }
        @Override
        public int getCount() {
            return int_items;
        }
        /**
         * This method returns the title of the tab according to the position.
         */
        @Override
        public CharSequence getPageTitle(int position) {
            switch (position){
                case 0 :
                    return "Today";
                case 1 :
                    return "Yesterday";
                case 2 :
                    return "Delivery All";
            }
            return null;
        }
    }
}

这里是 xml :-

    <LinearLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.Weal.sachin.omcom.TabFragment"
    android:orientation="vertical">
<android.support.design.widget.TabLayout
        android:id="@+id/sliding_tabs_delivery"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        app:tabTextColor="#d2cece"
        app:tabSelectedTextColor="#fff"
        android:background="#11977c"
        />
    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager_delivery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">
        <fragment android:name="com.Weal.sachin.omcom.TabFragment"
            android:id="@+id/tabFragment"
            android:layout_width="match_parent"
            android:layout_height="100dp" />
    </android.support.v4.view.ViewPager>
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fabdelivery"
            android:layout_width="75dp"
            android:layout_height="124dp"
            android:layout_marginTop="50dp"
            android:layout_gravity="bottom|end"
            android:backgroundTint="#11977c"
            android:layout_margin="@dimen/fab_margin"
            android:src="@drawable/addplus"/>
    </android.support.design.widget.CoordinatorLayout>
</LinearLayout>

最新更新