Android ScrollView没有垂直滚动

时间:2016-02-26 08:01:07

标签: android scrollview

我试图显示一个能够垂直滚动的页面。我已经添加了android:fillViewPort="true",但页面却没有垂直滚动。我一直在寻找一些答案,并将scrollview的第一个也是唯一的一个孩子设置为android:layout_height="wrap_content",但仍未成功。

这是xml文件。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
       android:fillViewport="true"> 
<RelativeLayout
    android:id="@+id/parentProfileLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"  
    android:background="@drawable/no_border_textfield">
   <RelativeLayout
        android:id="@+id/topProfileLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/larger_margin"
        android:paddingRight="@dimen/larger_margin"
        android:paddingTop="@dimen/larger_margin"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/photoImageView"
            android:layout_width="@dimen/default_photo_width"
            android:layout_height="@dimen/default_photo_height"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:gravity="left|center_vertical"
            android:layout_marginRight="@dimen/default_margin"
            android:background="@drawable/photo_drawable"/>
        <RelativeLayout
            android:id="@+id/doctorLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/photoImageView"
            android:orientation="vertical" 
            android:layout_marginRight="@dimen/default_margin"
            android:layout_marginLeft="@dimen/default_margin">
            <TextView
                android:id="@+id/doctorNameText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:textColor="@color/default_text_color"
                android:text="@string/empty2"
                style="@style/BoldInfoTextView"/>
            <TextView
                android:id="@+id/doctorTitleText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/doctorNameText"
                android:paddingRight="@dimen/default_padding"
                android:textColor="@color/default_text_color"
                android:text="@string/empty2"
                style="@style/BoldInfoTextView"/>       
        </RelativeLayout>    
    </RelativeLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/doctor_pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="@dimen/larger_margin"
        android:layout_marginRight="@dimen/larger_margin"
        android:layout_below="@+id/topProfileLayout">
        <android.support.v4.view.PagerTabStrip
            android:id="@+id/pager_header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:background="@color/default_table_header"
            android:paddingBottom="4dp"
            android:paddingTop="4dp"
            android:textColor="#fff" />
    </android.support.v4.view.ViewPager>
</RelativeLayout>
</ScrollView>

4 个答案:

答案 0 :(得分:0)

将滚动视图放在相对布局中,如下所示。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:fillViewport="true">

    <RelativeLayout
     android:id="@+id/parentProfileLayout"
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
    </RelativeLayout>


</ScrollView>

答案 1 :(得分:0)

我不确定将 ViewPager 放入 ScrollView 是否合适。我认为ViewPager捕获动作事件而 ScrollView 不会发生任何事件。

如果要在ViewPager中滚动内容时隐藏标签,请使用 CoordinatorLayout 。有很多关于如何使用 CoordinatorLayout 的示例。所以我认为您可以自己找到更多信息。

答案 2 :(得分:0)

将您的子级布局更改为RelativeLayoutLinearLayout

答案 3 :(得分:0)

这很难看,这可能是错的,但它确实有效。

在XML中: 在ViewPager中放置一个固定的dp高度(此高度需要大于内容)。内容的容器必须具有wrap_content高度。

在Java中: 实现一个Runnable,它计算px中容器的高度并复制到ViewPager

activity_main.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_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="br.com.brhuearts.swipeview.MainActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <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/AppTheme.PopupOverlay">

    </android.support.v7.widget.Toolbar>

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

<android.support.v4.widget.NestedScrollView
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Texto\nwwwee\nwewwererwre\nwqewewe\ndnasuid\nsjdhsiufoiifjoj"
        android:background="#00FF00"
        android:id="@+id/tv1"
        />


        <android.support.v4.view.ViewPager
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="2000dp"
            android:background="#FFFF00"
            android:layout_weight="1"
            />

</LinearLayout>

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

fragment_main.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="wrap_content"
android:orientation="vertical"
tools:context="br.com.brhuearts.swipeview.MainActivity$PlaceholderFragment">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#00FFFF"
    android:padding="20dp"
    android:id="@+id/ll">

    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/lb"
        android:background="#FF0000"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Texto\nTexto\nTexto\nTexto\nTexto\nTexto\nTexto\nTexto\nTexto\nTexto\nTexto\nTexto\nTexto\nTexto\nTexto\nTexto\nTexto\nTexto\nTexto\nTexto\nTexto\nTexto\nTexto\nTexto\nTexto\nTexto\nTextofim"
        />

</LinearLayout>

MainActivity.java

    package br.com.brhuearts.swipeview;

import android.os.Bundle;
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.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    private ViewPager mViewPager;

    private static LinearLayout ll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        mViewPager.post(new Runnable() {
            @Override
            public void run() {
                //In Runnable can pick up the height of the objects
                Log.d("TAG","_VP1__"+mViewPager.getHeight());
                Log.d("TAG","_VP2__"+mViewPager.getLayoutParams().height);

                Log.d("TAG","_LL1__"+ ll.getHeight());
                Log.d("TAG","_LL2__"+ll.getLayoutParams().height);


                //calculates the height of the container in px and copy to the ViewPager
                mViewPager.getLayoutParams().height=ll.getHeight();
            }
        });

    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            ll = (LinearLayout) rootView.findViewById(R.id.ll);
            return rootView;
        }
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            return PlaceholderFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "SECTION 1";
                case 1:
                    return "SECTION 2";
                case 2:
                    return "SECTION 3";
            }
            return null;
        }
    }
}