2016-02-26 3 views
1

Я пытаюсь отобразить страницу с возможностью прокрутки по вертикали. Я уже добавил android:fillViewPort="true", но страница просто не будет прокручиваться по вертикали. Я искал ответы и установил первый и единственный дочерний элемент прокрутки в android:layout_height="wrap_content", но все равно не удалось.Android ScrollView не имеет вертикальной прокрутки

Это 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> 
+0

Ваш Scrollview имея достаточное содержание для прокрутки? Попробуйте добавить отступ снизу = 100dp к вашему относительному макету и проверить, если она прокручивается или нет –

+0

да, определенно. ViewPager имеет довольно высокую высоту, и я могу видеть только половину содержимого ViewPager. – yodann

+1

может быть проблемой в ViewPager, который находится в ScrollView. Попробуйте удалить его и проверить горизонтальные прокрутки. – Alexander

ответ

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

Нет, он вообще не работает – yodann

+1

убедитесь, что ваш scrollview содержит только одного ребенка. –

+0

Да, это уже. единственным дочерним элементом является RelativeLayout с id parentProfileLayout. Остальное обернуто этим макетом. – yodann

0

Я не уверен, что это хорошо положить ViewPager в ScrollView. Я думаю, что ViewPager ловит события движения и ScrollView не получает никаких событий.

Если вы хотите скрыть вкладки при прокрутке содержимого в ViewPager, используйте КоординаторLayout. Там много примеров о том, как использовать КоординаторLayout. Поэтому я думаю, что вы найдете больше информации самостоятельно.

0

изменить ваш макет ребенка, т.е. RelativeLayout до LinearLayout.

0

Это некрасиво, это может быть неправильно делать, но оно работает.

В XML: Поместите фиксированную высоту dp в ViewPager (эта высота должна быть больше, чем содержимое). Контейнер содержимого должен иметь height_content height.

В Java: реализовать Runnable, который вычисляет высоту контейнера в рх и скопировать на 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; 
     } 
    } 
} 
Смежные вопросы