View Pager在android中的嵌套滚动视图中不起作用

时间:2017-06-24 12:14:29

标签: android layout android-viewpager android-tablayout android-collapsingtoolbarlayout

大家好,我是android开发的新手,而且我的视图寻呼机无法正常工作。 我想要实现的是一个标签布局向上滚动以及折叠工具栏布局。非常感谢 :) 没有嵌套的滚动视图,问题是折叠工具栏没有折叠。这是代码:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            app:contentScrim="@android:color/black"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@drawable/bottom_nav_home_icon"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.1" />
        </android.support.design.widget.CollapsingToolbarLayout>

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <android.support.design.widget.TabLayout
            android:id="@+id/temp_tab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:theme="@style/TabLayoutTheme" />
        <android.support.v4.view.ViewPager
            android:id="@+id/temp_view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>


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

这适用于查看分页器但折叠工具栏没有折叠。当我尝试将其保留在嵌套滚动视图中时,折叠工具栏布局正在折叠但查看分页器不起作用。它没有滑动标签。

查看寻呼机的java代码是

import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;



public class Tabs extends AppCompatActivity {

    TabLayout tabLayout;
    ViewPager viewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_history_tabs);
        tabLayout=(TabLayout)findViewById(R.id.temp_tab);
        viewPager=(ViewPager)findViewById(R.id.temp_view_pager);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setAdapter(new PageAdapter(getSupportFragmentManager()));
    }
    class PageAdapter extends FragmentPagerAdapter{

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

        @Override
        public Fragment getItem(int position) {
            return new TabFragment1();
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position){
                case 0:return "A";
                default:return "B";
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

尝试将var animal = {}; //animal is an object //animal.constructor is a parent Object constructor function var cat = Object.create(animal); //cat is an object created using animal object //cat.constructor will also return parent Object constructor because. //To remember this just remember that right side of instanceof always needs to be callable (that is, it needs to be a function) console.log('cat instanceof animal.constructor: ' + (cat instanceof animal.constructor)); //this will return true as animal.constructor is nothing but parent Object constructor function and every object is instanceof that parent Object() console.log(' cat instanceof cat.constructor: ' + (cat instanceof cat.constructor)); //this will also return true for the same reason as mentioned above console.log('animal instanceof cat.constructor: ' + (animal instanceof cat.constructor)); //this also has the same reason 放入LinearLayout

NestedScrollView

并将<android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" android:id="@+id/nsv"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.design.widget.TabLayout android:id="@+id/temp_tab" android:layout_width="match_parent" android:layout_height="wrap_content" /> <android.support.v4.view.ViewPager android:id="@+id/temp_view_pager" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </android.support.v4.widget.NestedScrollView> 设置为android:fillViewport

NestedScrollView

答案 1 :(得分:0)

我想

android:fitsSystemWindows="true"是原因。删除此行。可以解决您的问题。