简体   繁体   English

Android ScrollView没有垂直滚动

[英]Android ScrollView doesn't have vertical scroll

I'm trying to display a page with ability to scroll vertically. 我正在尝试显示具有垂直滚动能力的页面。 I've already added the android:fillViewPort="true" , but the page just won't scroll vertically. 我已经添加了android:fillViewPort="true" ,但是页面不会垂直滚动。 I've been searching for some answers and have set the first and only child of scrollview to android:layout_height="wrap_content" , but still not succeeded. 我一直在寻找一些答案,并将scrollview的第一个也是唯一的子级设置为android:layout_height="wrap_content" ,但仍然没有成功。

Here is the xml file. 这是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>

Put that scrollview in relative layout like below. 将该滚动视图放在相对布局中,如下所示。

<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>

I'm not sure that it's good put ViewPager into ScrollView . 我不确定将ViewPager放入ScrollView是否很好。 I think that ViewPager catches motion events and ScrollView doesn't get any events. 我认为ViewPager捕获运动事件,而ScrollView没有得到任何事件。

If you want to hide tabs when scrolling content in ViewPager, use CoordinatorLayout . 如果要在ViewPager中滚动内容时隐藏选项卡,请使用CoordinatorLayout There many examples about how to use CoordinatorLayout . 关于如何使用CoordinatorLayout的例子很多。 So I think that you find more information by yourself. 因此,我认为您可以自己找到更多信息。

将您的子布局(即RelativeLayout更改为LinearLayout

This is ugly, it may be wrong to do, but it works. 这很丑陋,做起来可能是错误的,但是可以。

In XML: Place a fixed dp height in your ViewPager (this height needs be larger that the content). 在XML中:在ViewPager中放置一个固定的dp高度(此高度需要大于内容的高度)。 The container of the contents must have wrap_content height. 内容容器必须具有wrap_content高度。

In Java: Implement a Runnable which calculates the height of the container in px and copy to the ViewPager 在Java中:实现一个Runnable,它以px为单位计算容器的高度并将其复制到ViewPager

activity_main.xml 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 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 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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM