【问题标题】:coordinatorlayout not scrolling with swipeRefreshlayoutcoordinatorlayout 不使用 swipeRefreshlayout 滚动
【发布时间】:2017-05-19 13:16:44
【问题描述】:

我添加了 coordinatorlayout+viewpager+TabLayout 并添加了三个带有 viewpager 的选项卡,但滚动仅适用于第一个选项卡(最近)

不使用两个选项卡 1. 联系人,2.设置

查看所有代码,仅根据需要在此处发布的 xml 代码

homeactivity xml(其中三个片段被附加)

<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/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.HomeActivity">


<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:elevation="6dp">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentTop="true"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:elevation="0dp"
        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"
        android:layout_below="@+id/toolbar"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:elevation="0dp"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/white"
        app:tabMode="fixed"
        app:tabSelectedTextColor="#ffffff"
        app:tabTextColor="@color/white" />

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tab_layout"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

contact_fragment.xml(带有 swiperefreshlayout)

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/swipe_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <!-- place your view here -->


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview_registered"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical" />

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_marginTop="2dp"
            android:background="@color/black" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview_invite"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:scrollbars="vertical" />
    </LinearLayout>


</android.support.v4.widget.SwipeRefreshLayout>

setting_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical" />

</RelativeLayout>

家庭活动代码

public void BindView() {
    appBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("RECENT");
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);
    TabLayout tabLayout;
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.setSelected(true);
    viewPager.setCurrentItem(0);


    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            switch (tab.getPosition()) {
                case 0:
                    viewPager.setCurrentItem(0);
                    toolbar.setTitle("RECENT");
                    break;
                case 1:
                    viewPager.setCurrentItem(1);
                    toolbar.setTitle("CONTACT");
                    break;
                case 2:
                    viewPager.setCurrentItem(2);
                    toolbar.setTitle("SETTING");
                    break;
                default:
                    viewPager.setCurrentItem(0);
                    toolbar.setTitle("RECENT");
                    break;
            }
            /*if (viewPager.getCurrentItem() == 0) {
                toolbar.setTitle(viewPager.getCurrentItem());
            }*/
        }

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

        }

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

        }
    });
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            if (viewPager.getCurrentItem() == 0) {
                toolbar.setTitle("RECENT");
            }

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });


}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new RecentFragment(), "RECENT");
    adapter.addFragment(new ContactFragment(), "CONTACT");
    adapter.addFragment(new Settingfragemnt(), "SETTING");
    viewPager.setAdapter(adapter);

}

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 xml android-coordinatorlayout swiperefreshlayout


    【解决方案1】:
        <?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:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            android:fitsSystemWindows="true">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_scrollFlags="scroll|enterAlways|snap" />
    
            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabGravity="fill"
                app:tabMaxWidth="0dp"
                android:fillViewport="false" />
    
        </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>
    

    【讨论】:

    • 你能告诉我变化并解释一下吗?
    • 在 CoordinatorLayout 和 AppBarLayout 上设置 android:fitsSystemWindows="true" 我不包括您的子布局,因为我猜这是您的 ViewPager 页面。 viewPager 的高度与整个根视图的高度相同。并点击此链接gist.github.com/iPaulPro/1468510f046cb10c51ea
    • 设置 android:fitsSystemWindows="true" 与标题栏重叠的工具栏
    • 检查实现但没有工作....我只是不明白你的一切工作正常最近的片段,其代码与 setting_fragment 相同
    【解决方案2】:

    主活动:

    public class MainActivity extends AppCompatActivity {
    
        Toolbar toolbar;
        AppBarLayout appBarLayout;
        ViewPager viewPager;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            bindView();
        }
    
        public void bindView() {
            appBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);
            toolbar = (Toolbar) findViewById(R.id.toolbar);
            toolbar.setTitle("RECENT");
            setSupportActionBar(toolbar);
    
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
            viewPager = (ViewPager) findViewById(R.id.viewpager);
            setupViewPager(viewPager);
            TabLayout tabLayout;
            tabLayout = (TabLayout) findViewById(R.id.tabs);
            tabLayout.setupWithViewPager(viewPager);
            tabLayout.setSelected(true);
            viewPager.setCurrentItem(0);
    
    
            viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    
            tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    switch (tab.getPosition()) {
                        case 0:
                            viewPager.setCurrentItem(0);
                            toolbar.setTitle("RECENT");
                            break;
                        case 1:
                            viewPager.setCurrentItem(1);
                            toolbar.setTitle("CONTACT");
                            break;
                        case 2:
                            viewPager.setCurrentItem(2);
                            toolbar.setTitle("SETTING");
                            break;
                        default:
                            viewPager.setCurrentItem(0);
                            toolbar.setTitle("RECENT");
                            break;
                    }
                /*if (viewPager.getCurrentItem() == 0) {
                    toolbar.setTitle(viewPager.getCurrentItem());
                }*/
                }
    
                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
    
                }
    
                @Override
                public void onTabReselected(TabLayout.Tab tab) {
    
                }
            });
            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    
                }
    
                @Override
                public void onPageSelected(int position) {
                    if (viewPager.getCurrentItem() == 0) {
                        toolbar.setTitle("RECENT");
                    }
    
                }
    
                @Override
                public void onPageScrollStateChanged(int state) {
    
                }
            });
    
    
        }
    
        private void setupViewPager(ViewPager viewPager) {
            ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
            adapter.addFragment(new FragmentOne(), "RECENT");
            adapter.addFragment(new Fragmenttwo(), "CONTACT");
            adapter.addFragment(new FragmentThree(), "SETTING");
            viewPager.setAdapter(adapter);
    
        }
    
        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);
            }
        }
    }
    

    activity_main:

        <?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"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/main_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:elevation="6dp">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_alignParentTop="true"
                android:background="?attr/colorPrimary"
                android:minHeight="?attr/actionBarSize"
    
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:elevation="0dp"
                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"
                android:layout_below="@+id/toolbar"
                android:background="?attr/colorPrimary"
                android:minHeight="?attr/actionBarSize"
                app:elevation="0dp"
                app:tabGravity="fill"
                app:tabIndicatorColor="#FFF"
                app:tabMode="fixed"
                app:tabSelectedTextColor="#ffffff"
                app:tabTextColor="#FFF" />
    
        </android.support.design.widget.AppBarLayout>
    
        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tab_layout"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
    </android.support.design.widget.CoordinatorLayout>
    

    片段一:

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

    fragment_one:

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="nvojnvg"/>
    
    </LinearLayout>
    

    片段二:

       public class Fragmenttwo extends Fragment {
    
        public Fragmenttwo(){
    
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            final View oneFra = inflater.inflate(R.layout.contact_fragment, container, false);
    
    
            return oneFra;
        }
    
    }
    

    contact_fragment xml:

        <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
        <!-- place your view here -->
    
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerview_registered"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scrollbars="vertical" />
    
            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:layout_marginTop="2dp"
                android:background="#CCC" />
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerview_invite"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="2dp"
                android:scrollbars="vertical" />
        </LinearLayout>
    
    
    </android.support.v4.widget.SwipeRefreshLayout>
    

    片段三:

        public class FragmentThree extends Fragment {
    
        public FragmentThree(){
    
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            final View oneFra = inflater.inflate(R.layout.setting_fragment, container, false);
    
    
            return oneFra;
        }
    
    }
    

    setting_fragment xml:

        <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical" />
    
    </RelativeLayout>
    

    改变你的主题风格:

     android:theme="@style/AppTheme.Base"
    
        <style name="AppTheme.Base" 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="windowNoTitle">true</item>
            <item name="windowActionBar">false</item>
            <item name="android:textColorPrimary">#1E1E1E</item>
            <item name="colorControlNormal">#1E1E1E</item>
            <item name="colorControlActivated">#BCBCBC</item>
            <item name="android:splitMotionEvents">false</item>
            <item name="android:windowEnableSplitTouch">false</item>
        </style>
    

    【讨论】:

    • 确保你必须导入 :import android.support.v4.app.Fragment;
    • 这在我的模拟器中运行良好仍然出现问题?
    • 你能告诉我你在检查什么问题吗?
    • 当您使用 Contact Fragment 滑动时,您的应用程序崩溃了,并且 coodinatorlayout+viewpager+TabLayout 并添加了三个带有 viewpager 的选项卡,但滚动仅适用于第一个选项卡
    • 我遇到的问题根本没有崩溃......正如问题中提到的那样我已经添加了 coodinatorlayout+viewpager+TabLayout 并添加了三个带有 viewpager 的选项卡,但滚动仅适用于第一个选项卡(最近)不使用两个选项卡 1. 联系人,2. 设置 重写问题:折叠,滚动无法在联系人中工作,设置片段仅在最近的片段中起作用
    猜你喜欢
    • 2019-09-17
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 2015-09-18
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多